# Application file for Gradio App for OpenAI Model
import gradio as gr
import time
import datetime
import os
from lc_base.chain import openai_chain
# global time_diff, model_name, search_type
time_diff = 0
model_name="gpt-3.5-turbo-1106"
search_type = "stuff"
input_question = ""
model_response = ""
user_feedback = ""
dir = os.path.join("outputs", "combined", "policy_eu_asia_usa", "faiss_index")
# dir = os.path.join("outputs", "policy", "1", "faiss_index")
title = """
ResearchBuddy
"""
description = """
This is a GPT based Research Buddy to assist in navigating new research topics.
"""
def save_api_key(api_key):
os.environ['OPENAI_API_KEY'] = str(api_key)
return f"API Key saved in the environment: {api_key}"
def user(user_message, history):
return "", history + [[user_message, None]]
def respond(message, chat_history):
global time_diff, model_response, input_question
question = str(message)
chain = openai_chain(inp_dir=dir)
start_time = time.time()
output = chain.get_response(query=question, k=10, model_name=model_name, type=search_type)
print(output)
# Update global variables to log
time_diff = time.time() - start_time
model_response = output
input_question = question
bot_message = output
chat_history.append((message, bot_message))
time.sleep(2)
return " ", chat_history
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", neutral_hue="slate")) as chat:
gr.HTML(title)
api_key_input = gr.Textbox(lines=1, label="Enter your OpenAI API Key")
api_key_input_submit = api_key_input.submit(save_api_key, [api_key_input])
chatbot = gr.Chatbot(height=750)
msg = gr.Textbox(label="Send a message", placeholder="Send a message",
show_label=False, container=False)
with gr.Row():
with gr.Column():
gr.Examples([
["Explain these documents to me in simpler terms."],
["What does these documents talk about?"],
], inputs=msg, label= "Click on any example to copy in the chatbox"
)
msg.submit(respond, [msg, chatbot], [msg, chatbot])
gr.HTML(description)
chat.queue()
chat.launch()