Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 streamlit as st
|
| 9 |
+
|
| 10 |
+
#REPO_ID = "Xenova/gpt-3.5-turbo"
|
| 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 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key="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")
|
| 19 |
+
|
| 20 |
+
# Initialize global variable for conversation memory
|
| 21 |
+
if 'buffer_memory' not in st.session_state:
|
| 22 |
+
st.session_state.buffer_memory= ConversationBufferWindowMemory(k=3)
|
| 23 |
+
|
| 24 |
+
conversation = ConversationChain(
|
| 25 |
+
llm=llm,
|
| 26 |
+
memory=st.session_state.buffer_memory,
|
| 27 |
+
verbose=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
context = """
|
| 31 |
+
You act as a chatbot to interact with users on their questions about traditional chinese medicine (TCM). \
|
| 32 |
+
Welcome the user in a friendly way.
|
| 33 |
+
You respond in a short and 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 |
+
Tag categories: location, product. \
|
| 80 |
+
Text: I want to buy/get [Po Chai Pills](product). \
|
| 81 |
+
If a user wants to buy/get a product, suggest that \
|
| 82 |
+
he/she can consider buying/getting from https://www.amazon.sg/s?k=product+name \
|
| 83 |
+
For example, if a user wants to buy Po Chai Pills, suggest \
|
| 84 |
+
he/she can consider buying/getting from https://www.amazon.sg/s?k=po+chai+pills \
|
| 85 |
+
Examples of product names:
|
| 86 |
+
Ointment/Hong You/Feng You/Fengyou
|
| 87 |
+
Liquorice/Gan cao/Gancao
|
| 88 |
+
Chrysanthemum/Ju hua/Juhua
|
| 89 |
+
Goji berry/wolfberry/Gou Qi Zi/Gouqizi
|
| 90 |
+
Red dates/Jujubes/Hong Zao/Hongzao
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
prompt_template = PromptTemplate.from_template(
|
| 94 |
+
'''system role :{context} \
|
| 95 |
+
user:{query}\
|
| 96 |
+
assistance:
|
| 97 |
+
''')
|
| 98 |
+
|
| 99 |
+
# Define Streamlit Interface
|
| 100 |
+
if query:
|
| 101 |
+
formatquery= prompt_template.format(context=context, query=query)
|
| 102 |
+
response = conversation.run(formatquery)
|
| 103 |
+
st.session_state.requests.append(query)
|
| 104 |
+
st.session_state.responses.append(response)
|
| 105 |
+
|
| 106 |
+
if st.session_state['responses']:
|
| 107 |
+
|
| 108 |
+
for i in range(len(st.session_state['responses'])-1, -1, -1):
|
| 109 |
+
message(st.session_state['requests'][i], is_user=True, key=str(i) + '_user')
|
| 110 |
+
message(st.session_state["responses"][i], key=str(i))
|
| 111 |
+
|
| 112 |
+
# Launch Streamlit Interface
|
| 113 |
+
!streamlit run app.py
|
| 114 |
+
|
| 115 |
+
# gr.load("models/ksh-nyp/llama-2-7b-chat-TCMKB").launch()
|