Spaces:
Runtime error
Runtime error
| import os | |
| import openai | |
| import gradio as gr | |
| from chat import Conversation | |
| system_prompt_foodie = """您是一名美食家,帮助别人了解美食的问题。您的回答需要满足以下要求: | |
| 1. 回答要使用中文。 | |
| 2. 回答要控制在100字以内。""" | |
| conv = Conversation(system_prompt_foodie, max_tokens=1024) | |
| with gr.Blocks(title="ChatGPT 助手") as demo: | |
| openai_api_key = gr.Textbox(label="OpenAI API Key") | |
| chatbot = gr.Chatbot(elem_id="chatbot").style(height=500) | |
| msg = gr.Textbox(show_label=False, placeholder="Please enter your question...").style(container=False) | |
| clear = gr.Button("Clear") | |
| def set_openai_api_key(openai_api_key): | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| if openai_api_key: | |
| openai.api_key = openai_api_key | |
| openai_api_key.change(set_openai_api_key, openai_api_key) | |
| def ask(message, chat_history): | |
| if openai.api_key is None: | |
| chat_history.append((message, "Please paste your OpenAI key to use")) | |
| else: | |
| bot_message = conv.ask(message) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| msg.submit(ask, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: conv.reset(), None, chatbot, queue=False) | |
| demo.launch() | |