Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
from langchain.chains import ConversationChain
|
5 |
+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
REPO_ID = "mathslearn/llama-2-7b-chat-TCMKB"
|
11 |
+
|
12 |
+
# Load the model and tokenizer from Hugging Face's model hub
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(REPO_ID)
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
|
15 |
+
|
16 |
+
llm = ChatOpenAI(model=model, tokenizer=tokenizer)
|
17 |
+
|
18 |
+
if 'buffer_memory' not in st.session_state:
|
19 |
+
st.session_state.buffer_memory = ConversationBufferWindowMemory(k=8)
|
20 |
+
#st.session_state.buffer_memory = ConversationBufferWindowMemory(k=3)
|
21 |
+
|
22 |
+
conversation = ConversationChain(
|
23 |
+
llm=llm,
|
24 |
+
memory=st.session_state.buffer_memory,
|
25 |
+
verbose=True
|
26 |
+
)
|
27 |
+
|
28 |
+
context = """
|
29 |
+
You act as a chatbot to interact with users on their questions about traditional chinese medicine (TCM). \
|
30 |
+
Welcome the user in a friendly way and state this disclaimer: \
|
31 |
+
It is important to note that a chatbot is not a substitute for medical advice \
|
32 |
+
from a qualified healthcare professional. \
|
33 |
+
You respond in a short, very friendly style. \
|
34 |
+
For each text, mark NER tags. \
|
35 |
+
Tag categories: location, product \
|
36 |
+
Text: I want to visit a clinic in [Ang Mo Kio](location). \
|
37 |
+
If a user says he/she wants to visit a traditional chinese medicine (TCM) clinic, \
|
38 |
+
ask for which location he/she wants to visit. \
|
39 |
+
After user replies with location, show in chat window the Google Map from this link \
|
40 |
+
https://www.google.com/maps/search/tcm+clinics+at+location+name \
|
41 |
+
For example, if a user wants to visit a clinic in Ang Mo Kio, \
|
42 |
+
show in chat window the Google Map from this link \
|
43 |
+
https://www.google.com/maps/search/tcm+clinics+at+ang+mo+kio \
|
44 |
+
Examoples of location names:
|
45 |
+
North:
|
46 |
+
Sembawang
|
47 |
+
Woodlands
|
48 |
+
Yishun
|
49 |
+
North-East:
|
50 |
+
Ang Mo Kio
|
51 |
+
Hougang
|
52 |
+
Punggol
|
53 |
+
Sengkang
|
54 |
+
Serangoon
|
55 |
+
East:
|
56 |
+
Bedok
|
57 |
+
Pasir Ris
|
58 |
+
Tampines
|
59 |
+
West:
|
60 |
+
Bukit Batok
|
61 |
+
Bukit Panjang
|
62 |
+
Choa Chu Kang
|
63 |
+
Clementi
|
64 |
+
Jurong East
|
65 |
+
Jurong West
|
66 |
+
Tengah
|
67 |
+
Central:
|
68 |
+
Bishan
|
69 |
+
Bukit Merah
|
70 |
+
Bukit Timah
|
71 |
+
Central Area
|
72 |
+
Geylang
|
73 |
+
Kallang
|
74 |
+
Whampoa
|
75 |
+
Marine Parade
|
76 |
+
Queenstown
|
77 |
+
Toa Payoh
|
78 |
+
For each text, mark NER tags. \
|
79 |
+
Text: I want to buy/get [Po Chai Pills](product). \
|
80 |
+
If a user wants to buy/get a product, suggest that \
|
81 |
+
he/she can consider buying/getting from https://www.amazon.sg/s?k=product+name \
|
82 |
+
For example, if a user wants to buy Po Chai Pills, suggest \
|
83 |
+
he/she can consider buying/getting from https://www.amazon.sg/s?k=po+chai+pills \
|
84 |
+
Examples of product names:
|
85 |
+
Ointment/Hong You/Feng You/Fengyou
|
86 |
+
Liquorice/Gan cao/Gancao
|
87 |
+
Chrysanthemum/Ju hua/Juhua
|
88 |
+
Goji berry/wolfberry/Gou Qi Zi/Gouqizi
|
89 |
+
Red dates/Jujubes/Hong Zao/Hongzao
|
90 |
+
"""
|
91 |
+
|
92 |
+
prompt_template = PromptTemplate.from_template('''system role :{context} \
|
93 |
+
user:{query}\
|
94 |
+
assistance:
|
95 |
+
''')
|
96 |
+
|
97 |
+
# Define Gradio Interface
|
98 |
+
iface = gr.Interface(
|
99 |
+
fn=lambda query: conversation.run(prompt_template.format(context=context, query=query)),
|
100 |
+
inputs=gr.Textbox(),
|
101 |
+
outputs=gr.Textbox(),
|
102 |
+
live=True,
|
103 |
+
capture_session=True
|
104 |
+
)
|
105 |
+
|
106 |
+
# Launch Gradio Interface
|
107 |
+
iface.launch()
|
108 |
+
|
109 |
+
# gr.load("models/ksh-nyp/llama-2-7b-chat-TCMKB").launch()
|