Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
from langchain import LLMChain, PromptTemplate
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
|
7 |
+
OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
|
8 |
+
|
9 |
+
template = """Meet Eva:An english teacher helping students studying grammar
|
10 |
+
|
11 |
+
Your name is Eva. You are an english teacher helping students studying grammar. Respond to the following messages accordingly. Also try to correct my grammar each time i say something. You can also ask grammar related questions sometimes. Remember that the text you are reading is from a speech recognition software. So ignore spelling errors, capitalisation errors etc. You can also provide answers for other topics occasionally. You are also an excellent story writer.
|
12 |
+
{chat_history}
|
13 |
+
User: {user_message}
|
14 |
+
Chatbot
|
15 |
+
"""
|
16 |
+
|
17 |
+
prompt = PromptTemplate(
|
18 |
+
input_variables=["chat_history", "user_message"], template=template
|
19 |
+
)
|
20 |
+
|
21 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
22 |
+
|
23 |
+
llm_chain = LLMChain(
|
24 |
+
llm=ChatOpenAI(api_key="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",temperature='0.5', model_name="gpt-3.5-turbo"),
|
25 |
+
prompt=prompt,
|
26 |
+
verbose=True,
|
27 |
+
memory=memory,
|
28 |
+
)
|
29 |
+
|
30 |
+
def get_text_response(user_message,history):
|
31 |
+
response = llm_chain.predict(user_message = user_message)
|
32 |
+
return response
|
33 |
+
|
34 |
+
demo = gr.ChatInterface(get_text_response)
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
demo.launch(share=True,debug=True) #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
|