Dracomoth's picture
Update app.py
07565fb
import gradio as gr
import os
import time
import instructions
from envConfig import envConfiguration
from indexer import documentIndexer
from conversation import conversationAgent
from exporter import exportAgent
#******************************************************************************************************************************************************************
chat_types = ["gpt-3.5-turbo", "gpt-3.5-turbo-1106", "gpt-4-1106-preview", "gpt-4-32k"]
configuration = envConfiguration()
conversation_agent = conversationAgent()
counter_text = "<small>Current buffer has {} tokens</small>"
#******************************************************************************************************************************************************************
configuration_error = "The settings have not been properly set. Please go to the **Settings** tab and set the proper values."
conversation_error = "There is currently no conversation to export."
#******************************************************************************************************************************************************************
def saveConfiguration(openai_key, openai_model, openai_temp, pinecone_key, pinecone_env, pinecone_index):
configuration.set_config(openai_key, openai_model, openai_temp, pinecone_key, pinecone_env, pinecone_index)
gr.Info("Configuration saved successfully")
#******************************************************************************************************************************************************************
def generateIndexes(files):
if not configuration.is_ready():
gr.Warning(configuration_error)
return
indexer = documentIndexer(configuration)
for file in files:
indexer.create_index(file)
gr.Info("Documents indexed correctly")
#******************************************************************************************************************************************************************
def indexMemory():
if not configuration.is_ready():
gr.Warning(configuration_error)
return
if not conversation_agent.is_ready():
gr.Info("There is currently no data to index")
return
indexer = documentIndexer(configuration)
memory = conversation_agent.get_memory()
indexer.index_memory(memory.buffer_as_str)
conversation_agent.clear_memory()
gr.Info("Memory indexed correctly")
#******************************************************************************************************************************************************************
def clearChatHistory():
if conversation_agent.is_ready():
conversation_agent.clear_memory()
return[]
#******************************************************************************************************************************************************************
#def clearIndexes():
# indexer = documentIndexer(configuration)
#
# indexer.clear_indexes
#
# gr.Info("Document indexes cleared correctly")
#
# return []
#******************************************************************************************************************************************************************
def exportDocs(template_pdf_files):
if not configuration.is_ready():
gr.Warning(configuration_error)
return []
if not conversation_agent.is_ready():
gr.Warning(conversation_error)
return []
exporter_agent = exportAgent(configuration, conversation_agent.get_memory().copy(deep = True))
generated_files = exporter_agent.do_export(template_pdf_files)
return generated_files
#******************************************************************************************************************************************************************
def sendPrompt(prompt, history):
if not configuration.is_ready():
gr.Warning(configuration_error)
return prompt, history
else:
history = history + [(prompt, None)]
return "", history
#******************************************************************************************************************************************************************
def receiveResponse(history):
if configuration.is_ready():
if not conversation_agent.is_ready():
conversation_agent.create_conversation(configuration)
response = conversation_agent.do_conversation(history[-1][0])
history[-1][1] = response
return history
#******************************************************************************************************************************************************************
def updateTokenCount():
if configuration.is_ready():
if not conversation_agent.is_ready():
conversation_agent.create_conversation(configuration)
token_count = conversation_agent.get_tokens()
return counter_text.format(token_count)
#******************************************************************************************************************************************************************
#Main Form
with gr.Blocks() as form:
gr.Markdown("<h1>Open AI Setting Creator</h1>")
with gr.Accordion("Instructions", open = False):
with gr.Accordion("Prerequisites", open = False):
gr.Markdown(instructions.prerequisites_text)
with gr.Accordion("Discussion", open = False):
gr.Markdown(instructions.discussion_text)
with gr.Accordion("Data Input", open = False):
gr.Markdown(instructions.input_text)
with gr.Accordion("Data Output", open = False):
gr.Markdown(instructions.output_text)
with gr.Accordion("Settings", open = False):
gr.Markdown(instructions.settings_text)
with gr.Tab("Discussion"):
chatbot = gr.Chatbot()
counter = gr.Markdown("<small>Current buffer has 0 tokens</small>")
with gr.Row():
prompt = gr.Textbox(label="Prompt", placeholder="Type your prompt here and hit Enter ", scale = 8, container = False)
submit_btn = gr.Button("Send", scale = 1, variant = "primary")
clear_btn = gr.Button("Clear", scale = 1)
index_btn = gr.Button("Index Memory", scale = 1)
with gr.Tab("Data Input"):
with gr.Row():
source_files = gr.Files(file_count="multiple", file_types = [".html", ".htm"])
savedb_btn = gr.Button("Save to Database")
with gr.Tab("Data Output"):
with gr.Row():
template_files = gr.Files(file_count="multiple", file_types = [".html", ".htm"])
generated_files = gr.Files(file_count="multiple", interactive = False)
generate_btn = gr.Button("Generate")
with gr.Tab("Settings"):
with gr.Row(variant = "panel"):
with gr.Column():
gr.Markdown("Open AI settings")
openai_type = gr.Dropdown(chat_types, label = "Model")
openai_temp = gr.Slider(0, 1, 0.5, step= 0.1, label = "Temperature", interactive = True)
openai_key = gr.Textbox(label="API key", type="password")
with gr.Row(variant = "panel"):
with gr.Column():
gr.Markdown("Pinecone settings")
pinecone_env = gr.Textbox(label = "Environment Name")
pinecone_index = gr.Textbox(label = "Index Name")
pinecone_key = gr.Textbox(label="API key", type="password")
save_btn = gr.Button("Save Settings")
save_btn.click(saveConfiguration, [openai_key,openai_type,openai_temp,pinecone_key,pinecone_env,pinecone_index], outputs=None)
savedb_btn.click(generateIndexes, source_files, None)
prompt.submit(sendPrompt, [prompt, chatbot], [prompt, chatbot]).then(receiveResponse, chatbot, chatbot).then(updateTokenCount, None, counter, show_progress = False)
submit_btn.click(sendPrompt, [prompt, chatbot], [prompt, chatbot]).then(receiveResponse, chatbot, chatbot).then(updateTokenCount, None, counter, show_progress = False)
clear_btn.click(clearChatHistory, None, chatbot).then(updateTokenCount, None, counter, show_progress = False)
index_btn.click(indexMemory, None, None).then(updateTokenCount, None, counter, show_progress = False)
generate_btn.click(exportDocs, template_files, generated_files)
form.launch(debug=True)