-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
47 lines (34 loc) · 1.17 KB
/
web.py
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
38
39
40
41
42
43
44
45
46
47
from typing import List
import gradio as gr
from llm import LLM
from settings import MODEL, QWEN_URL, DASHSCOPE_API_KEY
from agent.agent_router import AgentRouter
from agent.schema import ResponseStatus
llm = LLM(model=MODEL, base_url=QWEN_URL, api_key=DASHSCOPE_API_KEY)
router = AgentRouter(llm=llm)
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def reset():
router.reset()
return None
def user(query: str, history: List):
return "", history + [[query, None]]
def bot(history):
history[-1][1] = ""
for output in router(history[-1][0]):
if output[0] == ResponseStatus.IMG:
history += [[None, (output[1],)]]
elif output[0] == ResponseStatus.STR:
if type(history[-1][1]) is tuple:
history += [[None, output[1]]]
else:
history[-1][1] += output[1]
yield history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(reset, None, chatbot, queue=False)
demo.queue()
demo.launch()