Soma Dhavala commited on
Commit
6f1fa04
·
1 Parent(s): 506b771
Files changed (2) hide show
  1. app.py +48 -4
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,7 +1,51 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
1
+
2
+
3
  import gradio as gr
4
+ import openai
5
+ import os
6
+
7
+
8
+
9
+
10
+ api_key = os.getenv('OPEN_API_KEY')
11
+ openai.api_key = api_key
12
+
13
+
14
+ global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in English. I can answer your questions about farming in India. Ask me anything!"}]
15
+
16
+
17
+
18
+ def user(user_message, history):
19
+ global global_history
20
+ history = history + [[user_message, None]]
21
+ global_history = global_history+[{"role": "user", "content": user_message}]
22
+ print(history)
23
+ print(global_history)
24
+ return "", history
25
+
26
+ def get_chatgpt_response(history):
27
+ output = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=history)
28
+ history.append({"role": "assistant", "content": output.choices[0].message.content})
29
+ return output.choices[0].message.content, history
30
+
31
+ def bot(history):
32
+ global global_history
33
+ response, global_history = get_chatgpt_response(global_history)
34
+ history[-1][1] = response
35
+ return history
36
+
37
+ def clear_history(lang = "English"):
38
+ global global_history
39
+ global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in {}. I can answer your questions about farming in India. Ask me anything!".format(lang)}]
40
+ return None
41
+
42
+
43
+ with gr.Blocks(title="Ag GPT Demo") as demo:
44
+ chatbot = gr.Chatbot()
45
+ msg = gr.Textbox()
46
+ clear = gr.Button("Clear")
47
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
48
+ clear.click(clear_history, None, chatbot, queue=False)
49
 
 
 
50
 
51
+ demo.launch(share=True)
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai==0.27.4
2
+ gradio==3.21.0