Peiran commited on
Commit
cacf5e4
·
1 Parent(s): a6f3d3f

Add two-agent CV arena UI.

Browse files
Files changed (1) hide show
  1. app.py +116 -59
app.py CHANGED
@@ -1,64 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
 
64
  demo.launch()
 
1
+ # import gradio as gr
2
+ # from huggingface_hub import InferenceClient
3
+
4
+ # """
5
+ # For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ # """
7
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+
9
+
10
+ # def respond(
11
+ # message,
12
+ # history: list[tuple[str, str]],
13
+ # system_message,
14
+ # max_tokens,
15
+ # temperature,
16
+ # top_p,
17
+ # ):
18
+ # messages = [{"role": "system", "content": system_message}]
19
+
20
+ # for val in history:
21
+ # if val[0]:
22
+ # messages.append({"role": "user", "content": val[0]})
23
+ # if val[1]:
24
+ # messages.append({"role": "assistant", "content": val[1]})
25
+
26
+ # messages.append({"role": "user", "content": message})
27
+
28
+ # response = ""
29
+
30
+ # for message in client.chat_completion(
31
+ # messages,
32
+ # max_tokens=max_tokens,
33
+ # stream=True,
34
+ # temperature=temperature,
35
+ # top_p=top_p,
36
+ # ):
37
+ # token = message.choices[0].delta.content
38
+
39
+ # response += token
40
+ # yield response
41
+
42
+
43
+ # """
44
+ # For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
+ # """
46
+ # demo = gr.ChatInterface(
47
+ # respond,
48
+ # additional_inputs=[
49
+ # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
+ # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
+ # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
+ # gr.Slider(
53
+ # minimum=0.1,
54
+ # maximum=1.0,
55
+ # value=0.95,
56
+ # step=0.05,
57
+ # label="Top-p (nucleus sampling)",
58
+ # ),
59
+ # ],
60
+ # )
61
+
62
+
63
+ # if __name__ == "__main__":
64
+ # demo.launch()
65
+
66
  import gradio as gr
67
+ from PIL import Image
68
+
69
+ # —— 在这里根据你自己的 Agent 框架实现这个函数 ——
70
+ def run_agent_on_image(original_img: Image.Image, prompt: str, agent_name: str) -> Image.Image:
71
+ """
72
+ 调用指定 agent(模型/工具)处理图片并返回结果
73
+ original_img: PIL Image
74
+ prompt: 用户输入的描述
75
+ agent_name: 在下拉框里选的模型名称
76
+ """
77
+ # 示例逻辑(请替换为真正的 agent 调用)
78
+ # if agent_name == "Model A":
79
+ # return model_a.process(original_img, prompt)
80
+ # elif agent_name == "Model B":
81
+ # return model_b.process(original_img, prompt)
82
+ return original_img # TODO: 删除这行
83
+
84
+ # 可选:把可用的 agent 列表写成一个文件或者直接在这里列出
85
+ MODEL_CHOICES = ["Model A", "Model B", "Model C"]
86
+
87
+
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown("## CV Agent Arena 🎨🤖\n上传一张图片,输入处理指令,然后选两个不同的 agent 比较结果。")
90
+
91
+ with gr.Row():
92
+ # 左侧:原图 + prompt
93
+ with gr.Column():
94
+ original = gr.Image(type="pil", label="Upload Original Image")
95
+ prompt = gr.Textbox(lines=2, placeholder="e.g. ‘Make it look like a sunny day’", label="Prompt")
96
+ # 右侧:选择两个 agent
97
+ with gr.Column():
98
+ agent1 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 1")
99
+ agent2 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 2")
100
+
101
+ # 处理按钮
102
+ run_btn = gr.Button("Run Agents")
103
+
104
+ with gr.Row():
105
+ # 左侧输出:Agent1 结果
106
+ with gr.Column():
107
+ out1 = gr.Image(type="pil", label="Agent 1 Output")
108
+ # 右侧输出:Agent2 结果
109
+ with gr.Column():
110
+ out2 = gr.Image(type="pil", label="Agent 2 Output")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ # 按钮绑定
113
+ run_btn.click(
114
+ fn=lambda img, p, a1, a2: (run_agent_on_image(img, p, a1), run_agent_on_image(img, p, a2)),
115
+ inputs=[original, prompt, agent1, agent2],
116
+ outputs=[out1, out2],
117
+ )
118
 
119
  if __name__ == "__main__":
120
+ demo.queue() # 支持异步队列,提高并发
121
  demo.launch()