12345678910111213141516171819202122232425262728293031323334353637383940 |
- from fastapi import FastAPI, Request
- import uvicorn
- from datetime import datetime, time, date
- import gradio as gr
- from .agent import agent_framework, agent_main
- CUSTOM_PATH = "/agent_bot"
- app = FastAPI()
- @app.get("/index")
- def root():
- return {"message": "hello world!!!"}
- with gr.Blocks() as demo:
- chatbot = gr.Chatbot(show_label=False, height=600, avatar_images=["https://hong.greatdk.com/_next/image?url=%2Fself.jpg&w=1920&q=75", "https://hong.greatdk.com/_next/image?url=%2Fnpc.jpg&w=1920&q=75"])
- with gr.Row() as input_row:
- msg_textbox = gr.Textbox(show_label=False, scale=15)
- submit_btn = gr.Button(value="发送", scale=1)
- clear_btn = gr.ClearButton([msg_textbox, chatbot],value="清除会话")
- def respond(message, chat_history):
- print(f"message:{message}, strpped:{message.strip()}")
- print(f"history:{chat_history}")
- if len(message.strip()) <= 0 :
- print(f"message is empty, ignore the message")
- return "", chat_history
-
- init_agent = agent_main.AgentMain()
- af = agent_framework.AgentFramework(init_agent=init_agent)
- bot_message = af.run(message.strip(), chat_history)
- chat_history.append((message.strip(), bot_message))
- return "", chat_history
- msg_textbox.submit(respond, [msg_textbox, chatbot], [msg_textbox, chatbot]) # 在输入框按回车也进行发送
- submit_btn.click(respond, [msg_textbox, chatbot], [msg_textbox, chatbot]) # 绑定按钮点击函数
-
- fastapp = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH)
- if __name__=='__main__':
- uvicorn.run(fastapp, host="0.0.0.0", port=8000)
|