Spaces:
Running
Running
Peiran
commited on
Commit
·
c2986fa
1
Parent(s):
366ea29
Update Human as Judge
Browse files- app.py +199 -109
- data/metadata.csv +0 -0
app.py
CHANGED
@@ -1,119 +1,209 @@
|
|
1 |
-
|
2 |
-
|
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 |
-
# ——
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def run_agent_on_image(original_img: Image.Image, prompt: str, agent_name: str) -> Image.Image:
|
71 |
"""
|
72 |
-
|
73 |
-
original_img: PIL Image
|
74 |
-
prompt: 用户输入的描述
|
75 |
-
agent_name: 在下拉框里选的模型名称
|
76 |
"""
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
with gr.Blocks() as demo:
|
89 |
-
gr.Markdown("## CV Agent Arena 🎨🤖\nUpload the image you want to process, provide your requirements, select two Agents, and click 'Run Agents' to compare the results!")
|
90 |
-
|
91 |
-
with gr.Row():
|
92 |
-
with gr.Column():
|
93 |
-
original = gr.Image(type="pil", label="Upload Original Image")
|
94 |
-
prompt = gr.Textbox(lines=2, placeholder="e.g. ‘Make it look like a sunny day’", label="Prompt")
|
95 |
-
with gr.Column():
|
96 |
-
agent1 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 1")
|
97 |
-
agent2 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 2")
|
98 |
-
|
99 |
-
# 处理按钮
|
100 |
-
run_btn = gr.Button("Run Agents")
|
101 |
-
|
102 |
-
with gr.Row():
|
103 |
-
# 左侧输出:Agent1 结果
|
104 |
-
with gr.Column():
|
105 |
-
out1 = gr.Image(type="pil", label="Agent 1 Output")
|
106 |
-
# 右侧输出:Agent2 结果
|
107 |
-
with gr.Column():
|
108 |
-
out2 = gr.Image(type="pil", label="Agent 2 Output")
|
109 |
-
|
110 |
-
# 按钮绑定
|
111 |
-
run_btn.click(
|
112 |
-
fn=lambda img, p, a1, a2: (run_agent_on_image(img, p, a1), run_agent_on_image(img, p, a2)),
|
113 |
-
inputs=[original, prompt, agent1, agent2],
|
114 |
-
outputs=[out1, out2],
|
115 |
-
)
|
116 |
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
demo.queue()
|
119 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, uuid, csv, random
|
2 |
+
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# —— 1. 环境 & 文件准备 ——
|
7 |
+
os.environ["GRADIO_SSR_MODE"] = "False" # 关掉 SSR
|
8 |
+
|
9 |
+
# 确保 data 目录及子目录存在
|
10 |
+
os.makedirs("data/images", exist_ok=True)
|
11 |
+
|
12 |
+
# metadata 文件:保存每次 run 的原图、prompt、agent、结果路径
|
13 |
+
METADATA_FILE = "data/metadata.csv"
|
14 |
+
if not os.path.exists(METADATA_FILE):
|
15 |
+
with open(METADATA_FILE, "w", newline="", encoding="utf-8") as f:
|
16 |
+
writer = csv.DictWriter(f, fieldnames=[
|
17 |
+
"id","original_path","prompt",
|
18 |
+
"agent1","img1_path","agent2","img2_path"
|
19 |
+
])
|
20 |
+
writer.writeheader()
|
21 |
+
|
22 |
+
# evaluations 文件:保存 judge 提交的评分
|
23 |
+
EVAL_FILE = "data/evaluations.csv"
|
24 |
+
if not os.path.exists(EVAL_FILE):
|
25 |
+
with open(EVAL_FILE, "w", newline="", encoding="utf-8") as f:
|
26 |
+
writer = csv.DictWriter(f, fieldnames=[
|
27 |
+
"record_id","timestamp","task",
|
28 |
+
"a1_follow","a1_creativity","a1_finesse",
|
29 |
+
"a2_follow","a2_creativity","a2_finesse"
|
30 |
+
])
|
31 |
+
writer.writeheader()
|
32 |
+
|
33 |
+
|
34 |
+
# —— 2. Agent 处理 & 保存到库 ——
|
35 |
def run_agent_on_image(original_img: Image.Image, prompt: str, agent_name: str) -> Image.Image:
|
36 |
"""
|
37 |
+
TODO: 这里替换为你自己调用 HuggingFace API 或本地模型的逻辑
|
|
|
|
|
|
|
38 |
"""
|
39 |
+
return original_img
|
40 |
+
|
41 |
+
def save_to_library(orig_img, prompt, a1, a2, img1, img2):
|
42 |
+
"""把这一组 original+prompt+两个 agent 的结果存到本地 data/ 文件夹,并在 metadata.csv 记录"""
|
43 |
+
rec_id = uuid.uuid4().hex
|
44 |
+
# 保存原图
|
45 |
+
orig_path = f"data/images/{rec_id}_orig.png"
|
46 |
+
orig_img.save(orig_path)
|
47 |
+
# 保存两张结果图(文件名中空格替换为下划线)
|
48 |
+
img1_path = f"data/images/{rec_id}_{a1.replace(' ','_')}.png"
|
49 |
+
img2_path = f"data/images/{rec_id}_{a2.replace(' ','_')}.png"
|
50 |
+
img1.save(img1_path)
|
51 |
+
img2.save(img2_path)
|
52 |
+
# 追加到 metadata.csv
|
53 |
+
with open(METADATA_FILE, "a", newline="", encoding="utf-8") as f:
|
54 |
+
writer = csv.DictWriter(f, fieldnames=[
|
55 |
+
"id","original_path","prompt","agent1","img1_path","agent2","img2_path"
|
56 |
+
])
|
57 |
+
writer.writerow({
|
58 |
+
"id": rec_id,
|
59 |
+
"original_path": orig_path,
|
60 |
+
"prompt": prompt,
|
61 |
+
"agent1": a1,
|
62 |
+
"img1_path": img1_path,
|
63 |
+
"agent2": a2,
|
64 |
+
"img2_path": img2_path
|
65 |
+
})
|
66 |
+
|
67 |
+
def generate_and_store(orig_img, prompt, a1, a2):
|
68 |
+
"""处理+保存+返回两张结果图给 Gradio 显示"""
|
69 |
+
out1 = run_agent_on_image(orig_img, prompt, a1)
|
70 |
+
out2 = run_agent_on_image(orig_img, prompt, a2)
|
71 |
+
save_to_library(orig_img, prompt, a1, a2, out1, out2)
|
72 |
+
return out1, out2
|
73 |
+
|
74 |
+
|
75 |
+
# —— 3. 从库中随机抽取 ——
|
76 |
+
def load_random_record():
|
77 |
+
"""从 metadata.csv 随机选一条,返回 record_id、原图、prompt、两张处理图的路径"""
|
78 |
+
with open(METADATA_FILE, "r", encoding="utf-8") as f:
|
79 |
+
rows = list(csv.DictReader(f))
|
80 |
+
if not rows:
|
81 |
+
# 库空时提示
|
82 |
+
return "", None, "No records in library", None, None
|
83 |
+
rec = random.choice(rows)
|
84 |
+
return (
|
85 |
+
rec["id"],
|
86 |
+
rec["original_path"],
|
87 |
+
rec["prompt"],
|
88 |
+
rec["img1_path"],
|
89 |
+
rec["img2_path"]
|
90 |
+
)
|
91 |
|
92 |
|
93 |
+
# —— 4. 保存评测结果 ——
|
94 |
+
def save_evaluation(record_id, task,
|
95 |
+
a1_follow, a1_creativity, a1_finesse,
|
96 |
+
a2_follow, a2_creativity, a2_finesse):
|
97 |
+
"""把打分连同 record_id 和 task 存到 evaluations.csv"""
|
98 |
+
with open(EVAL_FILE, "a", newline="", encoding="utf-8") as f:
|
99 |
+
writer = csv.DictWriter(f, fieldnames=[
|
100 |
+
"record_id","timestamp","task",
|
101 |
+
"a1_follow","a1_creativity","a1_finesse",
|
102 |
+
"a2_follow","a2_creativity","a2_finesse"
|
103 |
+
])
|
104 |
+
writer.writerow({
|
105 |
+
"record_id": record_id,
|
106 |
+
"timestamp": datetime.now().isoformat(),
|
107 |
+
"task": task,
|
108 |
+
"a1_follow": a1_follow,
|
109 |
+
"a1_creativity": a1_creativity,
|
110 |
+
"a1_finesse": a1_finesse,
|
111 |
+
"a2_follow": a2_follow,
|
112 |
+
"a2_creativity": a2_creativity,
|
113 |
+
"a2_finesse": a2_finesse
|
114 |
+
})
|
115 |
+
return "✅ Evaluation submitted!"
|
116 |
+
|
117 |
+
|
118 |
+
# —— 5. Gradio UI ——
|
119 |
+
MODEL_CHOICES = ["Model A", "Model B", "Model C"]
|
120 |
+
TASK_CHOICES = [
|
121 |
+
"Image Restoration",
|
122 |
+
"Image Enhancement",
|
123 |
+
"Domain & Style Transfer",
|
124 |
+
"Semantic-Aware Editing",
|
125 |
+
"Image Composition & Expansion",
|
126 |
+
"Face & Appeal Editing",
|
127 |
+
"Steganography & Security Handling"
|
128 |
+
]
|
129 |
+
|
130 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
with gr.Tabs():
|
133 |
+
|
134 |
+
# ——— Tab 1: Agent Arena ———
|
135 |
+
with gr.TabItem("Agent Arena"):
|
136 |
+
gr.Markdown("## CV Agent Arena 🎨🤖")
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column():
|
139 |
+
original = gr.Image(type="pil", label="Upload Original Image")
|
140 |
+
prompt = gr.Textbox(lines=2, label="Prompt",
|
141 |
+
placeholder="e.g. Make it look like a sunny day")
|
142 |
+
with gr.Column():
|
143 |
+
agent1 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 1")
|
144 |
+
agent2 = gr.Dropdown(choices=MODEL_CHOICES, label="Select Agent 2")
|
145 |
+
run_btn = gr.Button("Run Agents")
|
146 |
+
with gr.Row():
|
147 |
+
out1 = gr.Image(type="pil", label="Agent 1 Output")
|
148 |
+
out2 = gr.Image(type="pil", label="Agent 2 Output")
|
149 |
+
|
150 |
+
run_btn.click(
|
151 |
+
fn=generate_and_store,
|
152 |
+
inputs=[original, prompt, agent1, agent2],
|
153 |
+
outputs=[out1, out2],
|
154 |
+
show_api=False
|
155 |
+
)
|
156 |
+
|
157 |
+
|
158 |
+
# ——— Tab 2: Human as Judge ———
|
159 |
+
with gr.TabItem("Human as Judge"):
|
160 |
+
# 隐藏状态:保存本次抽到的 record_id
|
161 |
+
record_id_state = gr.State("")
|
162 |
+
|
163 |
+
task_dropdown = gr.Dropdown(choices=TASK_CHOICES, label="Task Category")
|
164 |
+
judge_orig = gr.Image(label="Original Image")
|
165 |
+
judge_prompt = gr.Textbox(label="Prompt", interactive=False)
|
166 |
+
judge_out1 = gr.Image(label="Agent 1 Result")
|
167 |
+
judge_out2 = gr.Image(label="Agent 2 Result")
|
168 |
+
|
169 |
+
# 当用户选 Task(或切换到此页)时,随机抽 record
|
170 |
+
task_dropdown.change(
|
171 |
+
fn=load_random_record,
|
172 |
+
inputs=[],
|
173 |
+
outputs=[record_id_state, judge_orig, judge_prompt, judge_out1, judge_out2],
|
174 |
+
show_api=False
|
175 |
+
)
|
176 |
+
|
177 |
+
gr.Markdown("### 请对两张处理图分别打分(0–5)")
|
178 |
+
with gr.Row():
|
179 |
+
with gr.Column():
|
180 |
+
gr.Markdown("#### Agent 1 Evaluation")
|
181 |
+
a1_follow = gr.Radio([0,1,2,3,4,5], label="Follow Prompt")
|
182 |
+
a1_creativity = gr.Radio([0,1,2,3,4,5], label="Creativity")
|
183 |
+
a1_finesse = gr.Radio([0,1,2,3,4,5], label="Finesse/Detail")
|
184 |
+
with gr.Column():
|
185 |
+
gr.Markdown("#### Agent 2 Evaluation")
|
186 |
+
a2_follow = gr.Radio([0,1,2,3,4,5], label="Follow Prompt")
|
187 |
+
a2_creativity = gr.Radio([0,1,2,3,4,5], label="Creativity")
|
188 |
+
a2_finesse = gr.Radio([0,1,2,3,4,5], label="Finesse/Detail")
|
189 |
+
|
190 |
+
submit_btn = gr.Button("Submit Evaluation")
|
191 |
+
submit_status = gr.Textbox(label="Status", interactive=False)
|
192 |
+
|
193 |
+
submit_btn.click(
|
194 |
+
fn=save_evaluation,
|
195 |
+
inputs=[
|
196 |
+
record_id_state, task_dropdown,
|
197 |
+
a1_follow, a1_creativity, a1_finesse,
|
198 |
+
a2_follow, a2_creativity, a2_finesse
|
199 |
+
],
|
200 |
+
outputs=[submit_status],
|
201 |
+
show_api=False
|
202 |
+
)
|
203 |
+
|
204 |
demo.queue()
|
205 |
+
demo.launch(
|
206 |
+
share=False,
|
207 |
+
show_api=False,
|
208 |
+
ssr_mode=False
|
209 |
+
)
|
data/metadata.csv
ADDED
File without changes
|