Spaces:
Build error
Build error
Commit ·
39ee2a2
1
Parent(s): d7c892b
Update app.py
Browse files1234518 unmodified lines2425262728293031323334353637383940414243444515 unmodified lines61626364656667686970717273747576777879808182838485import gradio as grimport openai# Initial instructions for the assistantinitial_instructions = {18 unmodified lines}# Initialize the conversation history with initial instructionsconversation_history = [initial_instructions]def setup_openai(api_key):openai.api_key = api_keyreturn "API Key Set Successfully!"def ask_joe(api_key, text):global conversation_history# set up the api_keysetup_openai(api_key)# Add the user's message to the conversation historyconversation_history.append({"role": "user","content": text})# Use the conversation history as the input to the model15 unmodified lines# Write the conversation history to a filewith open('conversation_history.txt', 'a') as f:f.write(f'User: {text}\n')f.write(f'AI: {model_message}\n')return model_messagedef reset_conversation(api_key):global conversation_history# Reset the conversation history with initial instructionsconversation_history = [initial_instructions]return "Conversation cleared."inputs_chat = gr.inputs.Textbox(lines=2, placeholder='Enter your question here')iface_chat = gr.Interface(fn=ask_joe,inputs=[gr.inputs.Textbox(label="OpenAI API Key"), inputs_chat],outputs=gr.outputs.Textbox(label="Joe's Response"))iface_reset = gr.Interface(fn=reset_conversation,inputs=gr.inputs.Textbox(label="OpenAI API Key"),outputs=gr.outputs.Textbox(label="Reset Status"))iface_chat.launch()iface_reset.launch()12345618 unmodified lines252627282930313233343536373839404142434415 unmodified lines606162636465666768697071727374757677787980818283848586import gradio as grimport openaiimport time# Initial instructions for the assistantinitial_instructions = {18 unmodified lines}# Initialize the conversation history with initial instructionsconversation_history = [["system", initial_instructions["content"]]]def setup_openai(api_key):openai.api_key = api_keyreturn "API Key Set Successfully!"def ask_joe(api_key, chat_history):# set up the api_keysetup_openai(api_key)message = chat_history[-1][0] # get the last user message# Add the user's message to the conversation historyconversation_history.append({"role": "user","content": message})# Use the conversation history as the input to the model15 unmodified lines# Write the conversation history to a filewith open('conversation_history.txt', 'a') as f:f.write(f'User: {message}\n')f.write(f'AI: {model_message}\n')time.sleep(1) # sleep for a bit for better UI experiencereturn chat_history + [[message, model_message]]def reset_conversation(api_key):global conversation_history# Reset the conversation history with initial instructionsconversation_history = [["system", initial_instructions["content"]]]return []with gr.Blocks() as demo:chatbot = gr.Chatbot()api_key = gr.Textbox(default="your-openai-api-key", label="OpenAI API Key")api_key.submit(setup_openai, outputs=gr.Text(label="API Key Status"))chatbot.submit(ask_joe, inputs=[api_key, chatbot], outputs=chatbot)reset_button = gr.Button("Reset Conversation")reset_button.click(reset_conversation, inputs=api_key, outputs=chatbot, queue=False)demo.launch()
app.py+21-20