File size: 4,914 Bytes
9be878d
 
 
 
 
 
 
 
9766d09
e93af3d
9766d09
5e501c7
c0e52f1
 
 
5ea2f5b
 
 
 
c0e52f1
 
5ea2f5b
 
c0e52f1
5ea2f5b
c0e52f1
5ea2f5b
 
 
9766d09
e65e5d4
6fac63c
 
0f5132d
e65e5d4
543a677
0f5132d
e65e5d4
5455ee9
cf77319
d210fff
e93af3d
9766d09
 
 
 
 
9be878d
 
 
 
 
 
 
 
 
f10dada
9be878d
 
 
d544e23
 
9be878d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23eb1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#from transformers import AutoModelForCausalLM, AutoTokenizer

from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate

import streamlit as st
from streamlit_chat import message
from PIL import Image

st.title("Nexus TCM Chatbot")
#query = st.text_input("Query: ", key="input")

#https://discuss.streamlit.io/t/clear-input-box-after-hitting-enter/33824/2
#https://discuss.streamlit.io/t/how-do-you-clear-the-input-box-after-hitting-enter/45691/4
#https://stackoverflow.com/questions/77093266/how-to-clear-input-field-after-hitting-enter-in-streamlit
if "my_text" not in st.session_state:
    st.session_state.my_text = ""

def submit():
    st.session_state.my_text = st.session_state.widget
    st.session_state.widget = ""

st.text_input("Enter text here", key="widget", on_change=submit)

query = st.session_state.my_text

st.write(query)

# Set the logo URL
#logo_url = "https://huggingface.co/spaces/mathslearn/chatbot_test_streamlit/blob/main/logo.jpeg"
logo_url = "https://huggingface.co/spaces/mathslearn/chatbot_test_streamlit/resolve/main/logo.jpeg"

# Set the desired logo height
logo_height = 25

# Display the logo with adjusted column width
#st.image(logo_url, caption="Nexus TCM Chatbot Logo", use_column_width=True)
#st.image(logo_url, use_column_width=True)
st.sidebar.image(logo_url, caption="Disclaimer: The chatbot is not a substitute for medical advice from a qualified healthcare professional.", use_column_width=True)

if 'responses' not in st.session_state:
    st.session_state['responses'] = []

if 'requests' not in st.session_state:
    st.session_state['requests'] = []

#REPO_ID = "Xenova/gpt-3.5-turbo"

# Load the model and tokenizer from Hugging Face's model hub
#model = AutoModelForCausalLM.from_pretrained(REPO_ID)
#tokenizer = AutoTokenizer.from_pretrained(REPO_ID)

#llm = ChatOpenAI(model=model, tokenizer=tokenizer)

llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key="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")

# Initialize global variable for conversation memory
if 'buffer_memory' not in st.session_state:
    st.session_state.buffer_memory= ConversationBufferWindowMemory(k=8)
    #st.session_state.buffer_memory= ConversationBufferWindowMemory(k=3)

conversation = ConversationChain(
    llm=llm,
    memory=st.session_state.buffer_memory,
    verbose=True
)

context = """ 
You act as a chatbot to interact with users on their questions about traditional chinese medicine (TCM). \
Welcome the user in a friendly way.
You respond in a short and friendly style. \
For each text, mark NER tags. \
Tag categories: location, product. \
Text: I want to visit a clinic in [Ang Mo Kio](location). \
If a user says he/she wants to visit a traditional chinese medicine (TCM) clinic, \
ask for which location he/she wants to visit. \
After user replies with location, show in chat window the Google Map from this link \
https://www.google.com/maps/search/tcm+clinics+at+location+name \
For example, if a user wants to visit a clinic in Ang Mo Kio, \
show in chat window the Google Map from this link \
https://www.google.com/maps/search/tcm+clinics+at+ang+mo+kio \
Examoples of location names:
North:
Sembawang
Woodlands
Yishun
North-East:
Ang Mo Kio
Hougang
Punggol
Sengkang
Serangoon
East:
Bedok
Pasir Ris
Tampines
West:
Bukit Batok
Bukit Panjang
Choa Chu Kang
Clementi
Jurong East
Jurong West
Tengah
Central:
Bishan
Bukit Merah
Bukit Timah
Central Area
Geylang
Kallang
Whampoa
Marine Parade
Queenstown
Toa Payoh
For each text, mark NER tags. \
Tag categories: location, product. \
Text: I want to buy/get [Po Chai Pills](product). \
If a user wants to buy/get a product, suggest that \
he/she can consider buying/getting from https://www.amazon.sg/s?k=product+name \
For example, if a user wants to buy Po Chai Pills, suggest \
he/she can consider buying/getting from https://www.amazon.sg/s?k=po+chai+pills \
Examples of product names:
Ointment/Hong You/Feng You/Fengyou
Liquorice/Gan cao/Gancao
Chrysanthemum/Ju hua/Juhua
Goji berry/wolfberry/Gou Qi Zi/Gouqizi
Red dates/Jujubes/Hong Zao/Hongzao
"""

prompt_template = PromptTemplate.from_template(
    '''system role :{context} \
        user:{query}\
        assistance: 
        ''')

# Define Streamlit Interface
if query:
    formatquery= prompt_template.format(context=context, query=query)
    response = conversation.run(formatquery)
    st.session_state.requests.append(query)
    st.session_state.responses.append(response)

if st.session_state['responses']:

    for i in range(len(st.session_state['responses'])-1, -1, -1):
        message(st.session_state['requests'][i], is_user=True, key=str(i) + '_user')
        message(st.session_state["responses"][i], key=str(i))

# gr.load("models/ksh-nyp/llama-2-7b-chat-TCMKB").launch()