File size: 1,308 Bytes
e0c4e40
 
 
c0c29d2
e0c4e40
988d147
 
 
e0c4e40
 
 
988d147
 
 
e0c4e40
 
988d147
 
 
 
 
 
 
e0c4e40
988d147
fe80fb7
988d147
 
 
 
e0c4e40
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()