CVAgentArena / app.py
Peiran
Add two-agent CV arena UI.
cacf5e4
raw
history blame
3.97 kB
# import gradio as gr
# from huggingface_hub import InferenceClient
# """
# 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
# """
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# def respond(
# message,
# history: list[tuple[str, str]],
# system_message,
# max_tokens,
# temperature,
# top_p,
# ):
# messages = [{"role": "system", "content": system_message}]
# for val in history:
# if val[0]:
# messages.append({"role": "user", "content": val[0]})
# if val[1]:
# messages.append({"role": "assistant", "content": val[1]})
# messages.append({"role": "user", "content": message})
# response = ""
# for message in client.chat_completion(
# messages,
# max_tokens=max_tokens,
# stream=True,
# temperature=temperature,
# top_p=top_p,
# ):
# token = message.choices[0].delta.content
# response += token
# yield response
# """
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
# """
# demo = gr.ChatInterface(
# respond,
# additional_inputs=[
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
# gr.Slider(
# minimum=0.1,
# maximum=1.0,
# value=0.95,
# step=0.05,
# label="Top-p (nucleus sampling)",
# ),
# ],
# )
# if __name__ == "__main__":
# demo.launch()
import gradio as gr
from PIL import Image
# —— 在这里根据你自己的 Agent 框架实现这个函数 ——
def run_agent_on_image(original_img: Image.Image, prompt: str, agent_name: str) -> Image.Image:
"""
调用指定 agent(模型/工具)处理图片并返回结果
original_img: PIL Image
prompt: 用户输入的描述
agent_name: 在下拉框里选的模型名称
"""
# 示例逻辑(请替换为真正的 agent 调用)
# if agent_name == "Model A":
# return model_a.process(original_img, prompt)
# elif agent_name == "Model B":
# return model_b.process(original_img, prompt)
return original_img # TODO: 删除这行
# 可选:把可用的 agent 列表写成一个文件或者直接在这里列出
MODEL_CHOICES = ["Model A", "Model B", "Model C"]
with gr.Blocks() as demo:
gr.Markdown("## CV Agent Arena 🎨🤖\n上传一张图片,输入处理指令,然后选两个不同的 agent 比较结果。")
with gr.Row():
# 左侧:原图 + prompt
with gr.Column():
original = gr.Image(type="pil", label="Upload Original Image")
prompt = gr.Textbox(lines=2, placeholder="e.g. ‘Make it look like a sunny day’", label="Prompt")
# 右侧:选择两个 agent
with gr.Column():
agent1 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 1")
agent2 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 2")
# 处理按钮
run_btn = gr.Button("Run Agents")
with gr.Row():
# 左侧输出:Agent1 结果
with gr.Column():
out1 = gr.Image(type="pil", label="Agent 1 Output")
# 右侧输出:Agent2 结果
with gr.Column():
out2 = gr.Image(type="pil", label="Agent 2 Output")
# 按钮绑定
run_btn.click(
fn=lambda img, p, a1, a2: (run_agent_on_image(img, p, a1), run_agent_on_image(img, p, a2)),
inputs=[original, prompt, agent1, agent2],
outputs=[out1, out2],
)
if __name__ == "__main__":
demo.queue() # 支持异步队列,提高并发
demo.launch()