import gradio as gr import utils from api import AutoAPI, get_openai_api_key import os, shutil import json FILE_DIR = os.path.dirname(os.path.abspath(__file__)) OUTPUT_DIR = os.path.join(os.path.dirname(FILE_DIR), "auto_gpt_workspace") if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) CSS = """ #chatbot {font-family: monospace;} #files .generating {display: none;} #files .min {min-height: 0px;} """ with gr.Blocks(css=CSS) as app: with gr.Column() as setup_pane: gr.Markdown(f""" #
{utils.format_directory(OUTPUT_DIR)}
""", every=3, elem_id="files"
)
download_btn = gr.Button("Download All Files")
chat_history = gr.State([[None, None]])
api = gr.State(None)
def start(open_ai_key, ai_name, ai_role, top_5_goals):
auto_api = AutoAPI(open_ai_key, ai_name, ai_role, top_5_goals)
return gr.Column.update(visible=False), gr.Column.update(visible=True), auto_api
def bot_response(chat, api):
messages = []
for message in api.get_chatbot_response():
messages.append(message)
chat[-1][1] = "\n".join(messages) + "..."
yield chat
chat[-1][1] = "\n".join(messages)
yield chat
def send_message(count, chat, api, message="Y"):
if message != "Y":
count = 1
for i in range(count):
chat.append([message, None])
yield chat, count - i
api.send_message(message)
for updated_chat in bot_response(chat, api):
yield updated_chat, count - i
def activate_inputs():
return {
yes_btn: gr.Button.update(interactive=True),
consecutive_yes: gr.Slider.update(interactive=True),
custom_response: gr.Textbox.update(interactive=True),
}
def deactivate_inputs():
return {
yes_btn: gr.Button.update(interactive=False),
consecutive_yes: gr.Slider.update(interactive=False),
custom_response: gr.Textbox.update(interactive=False),
}
start_btn.click(
start,
[open_ai_key, ai_name, ai_role, top_5_goals],
[setup_pane, main_pane, api],
).then(bot_response, [chat_history, api], chatbot).then(
activate_inputs, None, [yes_btn, consecutive_yes, custom_response]
)
yes_btn.click(
deactivate_inputs, None, [yes_btn, consecutive_yes, custom_response]
).then(
send_message, [consecutive_yes, chat_history, api], [chatbot, consecutive_yes]
).then(
activate_inputs, None, [yes_btn, consecutive_yes, custom_response]
)
custom_response.submit(
deactivate_inputs, None, [yes_btn, consecutive_yes, custom_response]
).then(
send_message,
[consecutive_yes, chat_history, api, custom_response],
[chatbot, consecutive_yes],
).then(
activate_inputs, None, [yes_btn, consecutive_yes, custom_response]
)
def download_all_files():
shutil.make_archive("outputs", "zip", OUTPUT_DIR)
download_btn.click(download_all_files).then(None, _js=utils.DOWNLOAD_OUTPUTS_JS)
gr.Markdown(
""" ###