abdullahmeda commited on
Commit
9d5b6cb
1 Parent(s): 3a56c3c

initial commit

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.chains import ConversationChain
5
+ from langchain.memory import ConversationBufferMemory
6
+
7
+
8
+ def respond(openai_api_key, message, buffer_memory, chat_history):
9
+ conversation = ConversationChain(
10
+ llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo'),
11
+ memory = buffer_memory,
12
+ openai_api_key = openai_api_key
13
+ )
14
+ response = conversation.predict(input=message)
15
+ chat_history.append([message, response])
16
+ return "", buffer_memory, chat_history
17
+
18
+
19
+ with gr.Blocks() as demo:
20
+ # with gr.Column():
21
+ with gr.Group(visible=True) as primary_settings:
22
+ with gr.Row():
23
+ openai_key = gr.Textbox(
24
+ label="OpenAI Key",
25
+ type="password",
26
+ placeholder="sk-a83jv6fn3x8ndm78b5W..."
27
+ )
28
+ model = gr.Dropdown(
29
+ ["gpt-4", "gpt-4-32k",
30
+ "gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-instruct",
31
+ "text-davinci-002", "text-davinci-003"],
32
+ label="OpenAI Model",
33
+ value="gpt-3.5-turbo",
34
+ interactive=True
35
+ )
36
+ # with gr.Accordion("Advances Settings"):
37
+ # gr.Dropdown(
38
+ # [-1, 1, 5, 10, 25], label="Conversation Buffer (k)"
39
+ # )
40
+ with gr.Group() as chat:
41
+ memory = gr.State(ConversationBufferMemory())
42
+ chatbot = gr.Chatbot(label='Chatbot')
43
+ with gr.Row():
44
+ query = gr.Textbox(
45
+ container=False,
46
+ show_label=False,
47
+ placeholder='Type a message...',
48
+ scale=10,
49
+ )
50
+ submit = gr.Button('Submit',
51
+ variant='primary',
52
+ scale=1,
53
+ min_width=0)
54
+
55
+ # Event Handling
56
+ query.submit(respond, [openai_key, query, memory, chatbot], [query, memory, chatbot])
57
+ submit.click(respond, [openai_key, query, memory, chatbot], [query, memory, chatbot])
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch()