| import openai | |
| import os | |
| import gradio as gr | |
| from common.conversation import * | |
| openai.api_key = os.environ.get("OPENAI_API_KEY") | |
| if __name__ == '__main__': | |
| prompt = """你是城市生活北京有线公司的职员,用中文回答大家的问题。你的回答需要满足以下要求: | |
| 1. 你的回答必须是中文 | |
| 2. 回答限制在100个字以内""" | |
| conv = Conversation(prompt, 5) | |
| def predict(input, history=[]): | |
| history.append(input) | |
| response = conv.ask(input) | |
| history.append(response) | |
| responses = [(u, b) for u, b in zip(history[::2], history[1::2])] | |
| return responses, history | |
| with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo: | |
| chatbot = gr.Chatbot(elem_id="chatbot") | |
| state = gr.State([]) | |
| with gr.Row(): | |
| txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) | |
| txt.submit(predict, [txt, state], [chatbot, state]) | |
| demo.launch() |