AgGPT / app.py
Soma Dhavala
add gpt
6f1fa04
raw
history blame
1.53 kB
import gradio as gr
import openai
import os
api_key = os.getenv('OPEN_API_KEY')
openai.api_key = api_key
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!"}]
def user(user_message, history):
global global_history
history = history + [[user_message, None]]
global_history = global_history+[{"role": "user", "content": user_message}]
print(history)
print(global_history)
return "", history
def get_chatgpt_response(history):
output = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=history)
history.append({"role": "assistant", "content": output.choices[0].message.content})
return output.choices[0].message.content, history
def bot(history):
global global_history
response, global_history = get_chatgpt_response(global_history)
history[-1][1] = response
return history
def clear_history(lang = "English"):
global global_history
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)}]
return None
with gr.Blocks(title="Ag GPT Demo") as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
clear.click(clear_history, None, chatbot, queue=False)
demo.launch(share=True)