Spaces:
Build error
Build error
Commit ·
6762a06
1
Parent(s): a729af0
Update app.py
Browse files3 unmodified lines456789103 unmodified lines1415161718192021222324259 unmodified lines35363738394041424344454 unmodified lines5051525354555657585960615 unmodified lines67686970717273743 unmodified linesimport openaifrom dotenv import load_dotenvload_dotenv()openai.api_key = os.getenv("OPENAI_API_KEY")import gradio as gr3 unmodified lines# Function to add user's text to chat historydef add_user_text(chat_history, user_text):print('user_text_from_typing: ', user_text)global messagesmessages += [{"role":'user', 'content': user_text}]chat_history = chat_history + [(user_text, None)]return chat_history, gr.update(value="", interactive=False)# Function for the bot to respond9 unmodified linesmessages=messages,)bot_text = bot_response["choices"][0]["message"]["content"]print("bot_text: ", bot_text)messages = messages + [{"role":'assistant', 'content': bot_text}]print(chat_history)chat_history[-1][1] = ""# Yield the chat history with the bot's response character by character4 unmodified lines# Create a Gradio interfacewith gr.Blocks() as demo:openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..")model_choice = gr.Dropdown(label="Model Options", choices=['gpt-3.5-turbo', 'gpt-4'],info = "For better results choose GPT4, for faster and cheaper responses, GPT3.5")clear_btn = gr.Button("Clear for Restart")chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)with gr.Box():user_text = gr.Textbox(show_label=False,placeholder="Enter text and press enter",5 unmodified linesbot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(lambda: gr.update(interactive=True), None, [user_text], queue=False)clear_btn.click(lambda: None, None, chat_history, queue=False)if __name__ == "__main__":demo.queue()demo.launch()3 unmodified lines45678910111213143 unmodified lines181920212223242526272829303132339 unmodified lines434445464748495051525354554 unmodified lines60616263646566676869707172737475767778795 unmodified lines85868788899091929394953 unmodified linesimport openaifrom dotenv import load_dotenv# Load environment variables from .env fileload_dotenv()# Set OpenAI API key from environment variableopenai.api_key = os.getenv("OPENAI_API_KEY")import gradio as gr3 unmodified lines# Function to add user's text to chat historydef add_user_text(chat_history, user_text):# Print the user's text from typingprint('user_text_from_typing: ', user_text)global messages# Add user's text to the messages list with 'user' rolemessages += [{"role":'user', 'content': user_text}]# Add user's text to the chat historychat_history = chat_history + [(user_text, None)]# Return updated chat history and update the display without interactionreturn chat_history, gr.update(value="", interactive=False)# Function for the bot to respond9 unmodified linesmessages=messages,)bot_text = bot_response["choices"][0]["message"]["content"]# Print the bot's responseprint("bot_text: ", bot_text)# Add bot's response to the messages list with 'assistant' rolemessages = messages + [{"role":'assistant', 'content': bot_text}]# Clear the last entry in the chat historychat_history[-1][1] = ""# Yield the chat history with the bot's response character by character4 unmodified lines# Create a Gradio interfacewith gr.Blocks() as demo:# Textbox for OpenAI GPT API Keyopenai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..")# Dropdown menu for selecting the modelmodel_choice = gr.Dropdown(label="Model Options", choices=['gpt-3.5-turbo', 'gpt-4'])# Button to clear the chat history and restartclear_btn = gr.Button("Clear for Restart")# Chat history displaychat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)with gr.Box():# Textbox for user inputuser_text = gr.Textbox(show_label=False,placeholder="Enter text and press enter",5 unmodified linesbot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(lambda: gr.update(interactive=True), None, [user_text], queue=False)# Clear button click eventclear_btn.click(lambda: None, None, chat_history, queue=False)if __name__ == "__main__":# Queue the Gradio interfacedemo.queue()# Launch the Gradio interfacedemo.launch()
app.py+23-2