Spaces:
Build error
Build error
Commit ·
18e2b8f
1
Parent(s): 39ee2a2
Update app.py
Browse files30 unmodified lines3132333435363738394041424344457 unmodified lines5354555657585960616263646566676869707172737475767778798081828384858630 unmodified linesopenai.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 modelresponse = openai.ChatCompletion.create(7 unmodified linesmodel_message = response.choices[0].message['content'].strip()# Add the model's message to the conversation historyconversation_history.append({"role": "assistant","content": model_message})# 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()30 unmodified lines3132333435363738394041427 unmodified lines5051525354555657585960616263646566676869707172737430 unmodified linesopenai.api_key = api_keyreturn "API Key Set Successfully!"def ask_joe(api_key, message):# set up the api_keysetup_openai(api_key)# Add the user's message to the conversation historyconversation_history.append(["user", message])# Use the conversation history as the input to the modelresponse = openai.ChatCompletion.create(7 unmodified linesmodel_message = response.choices[0].message['content'].strip()# Add the model's message to the conversation historyconversation_history.append(["assistant", model_message])# Write the conversation history to a filewith open('conversation_history.txt', 'w') as f:for role, content in conversation_history:f.write(f'{role.capitalize()}: {content}\n')time.sleep(1) # sleep for a bit for better UI experiencereturn model_messagedef reset_conversation(api_key):global conversation_history# Reset the conversation history with initial instructionsconversation_history = [["system", initial_instructions["content"]]]return "Conversation reset"api_key = gr.inputs.Textbox(label="OpenAI API Key")message = gr.inputs.Textbox(label="Your question")output_message = gr.outputs.Textbox(label="Joe's answer")reset_interface = gr.Interface(fn=reset_conversation, inputs=api_key, outputs=output_message)ask_joe_interface = gr.Interface(fn=ask_joe, inputs=[api
app.py+14-26