Spaces:
Running
Running
Peiran
commited on
Commit
·
e2b9c18
1
Parent(s):
c2986fa
更新app.py,删除metadata.csv,添加test.py
Browse files- app.py +325 -104
- data/metadata.csv +0 -0
- test.py +4 -0
app.py
CHANGED
@@ -7,115 +7,330 @@ import gradio as gr
|
|
7 |
os.environ["GRADIO_SSR_MODE"] = "False" # 关掉 SSR
|
8 |
|
9 |
# 确保 data 目录及子目录存在
|
10 |
-
os.makedirs("data/images", exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
writer = csv.DictWriter(f, fieldnames=[
|
17 |
"id","original_path","prompt",
|
18 |
"agent1","img1_path","agent2","img2_path"
|
19 |
])
|
20 |
writer.writeheader()
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
writer = csv.DictWriter(f, fieldnames=[
|
27 |
-
"
|
28 |
"a1_follow","a1_creativity","a1_finesse",
|
29 |
"a2_follow","a2_creativity","a2_finesse"
|
30 |
])
|
31 |
writer.writeheader()
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
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 |
-
"
|
|
|
|
|
56 |
])
|
57 |
-
writer.
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
"
|
63 |
-
"
|
64 |
-
"
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
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 |
-
"
|
101 |
"a1_follow","a1_creativity","a1_finesse",
|
102 |
"a2_follow","a2_creativity","a2_finesse"
|
103 |
])
|
104 |
-
writer.
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
"
|
110 |
-
"a1_finesse"
|
111 |
-
"a2_follow"
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
MODEL_CHOICES = ["Model A", "Model B", "Model C"]
|
120 |
TASK_CHOICES = [
|
121 |
"Image Restoration",
|
@@ -157,53 +372,59 @@ with gr.Blocks() as demo:
|
|
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 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
168 |
|
169 |
-
#
|
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 |
-
|
197 |
a1_follow, a1_creativity, a1_finesse,
|
198 |
a2_follow, a2_creativity, a2_finesse
|
199 |
],
|
200 |
-
outputs=[
|
|
|
|
|
|
|
201 |
show_api=False
|
202 |
)
|
|
|
203 |
|
204 |
demo.queue()
|
205 |
demo.launch(
|
206 |
share=False,
|
207 |
show_api=False,
|
208 |
ssr_mode=False
|
209 |
-
)
|
|
|
7 |
os.environ["GRADIO_SSR_MODE"] = "False" # 关掉 SSR
|
8 |
|
9 |
# 确保 data 目录及子目录存在
|
10 |
+
os.makedirs("data/images/task0/orig_imgs", exist_ok=True)
|
11 |
+
os.makedirs("data/images/task0/processed_imgs", exist_ok=True)
|
12 |
+
os.makedirs("data/images/task1/orig_imgs", exist_ok=True)
|
13 |
+
os.makedirs("data/images/task1/processed_imgs", exist_ok=True)
|
14 |
+
os.makedirs("data/images/task2/orig_imgs", exist_ok=True)
|
15 |
+
os.makedirs("data/images/task2/processed_imgs", exist_ok=True)
|
16 |
+
os.makedirs("data/images/task3/orig_imgs", exist_ok=True)
|
17 |
+
os.makedirs("data/images/task3/processed_imgs", exist_ok=True)
|
18 |
+
os.makedirs("data/images/task4/orig_imgs", exist_ok=True)
|
19 |
+
os.makedirs("data/images/task4/processed_imgs", exist_ok=True)
|
20 |
+
os.makedirs("data/images/task5/orig_imgs", exist_ok=True)
|
21 |
+
os.makedirs("data/images/task5/processed_imgs", exist_ok=True)
|
22 |
+
os.makedirs("data/images/task6/orig_imgs", exist_ok=True)
|
23 |
+
os.makedirs("data/images/task6/processed_imgs", exist_ok=True)
|
24 |
|
25 |
+
# 在文件开头添加必要的目录创建
|
26 |
+
os.makedirs("data/evaluations", exist_ok=True)
|
27 |
+
os.makedirs("data/metadatas", exist_ok=True)
|
28 |
+
|
29 |
+
meta0 = "data/metadatas/meta0.csv"
|
30 |
+
meta1 = "data/metadatas/meta1.csv"
|
31 |
+
meta2 = "data/metadatas/meta2.csv"
|
32 |
+
meta3 = "data/metadatas/meta3.csv"
|
33 |
+
meta4 = "data/metadatas/meta4.csv"
|
34 |
+
meta5 = "data/metadatas/meta5.csv"
|
35 |
+
meta6 = "data/metadatas/meta6.csv"
|
36 |
+
|
37 |
+
if not os.path.exists(meta0):
|
38 |
+
with open(meta0, "w", newline="", encoding="utf-8") as f:
|
39 |
+
writer = csv.DictWriter(f, fieldnames=[
|
40 |
+
"id","original_path","prompt",
|
41 |
+
"agent1","img1_path","agent2","img2_path"
|
42 |
+
])
|
43 |
+
writer.writeheader()
|
44 |
+
|
45 |
+
if not os.path.exists(meta1):
|
46 |
+
with open(meta1, "w", newline="", encoding="utf-8") as f:
|
47 |
+
writer = csv.DictWriter(f, fieldnames=[
|
48 |
+
"id","original_path","prompt",
|
49 |
+
"agent1","img1_path","agent2","img2_path"
|
50 |
+
])
|
51 |
+
writer.writeheader()
|
52 |
+
|
53 |
+
if not os.path.exists(meta2):
|
54 |
+
with open(meta2, "w", newline="", encoding="utf-8") as f:
|
55 |
+
writer = csv.DictWriter(f, fieldnames=[
|
56 |
+
"id","original_path","prompt",
|
57 |
+
"agent1","img1_path","agent2","img2_path"
|
58 |
+
])
|
59 |
+
writer.writeheader()
|
60 |
+
|
61 |
+
if not os.path.exists(meta3):
|
62 |
+
with open(meta3, "w", newline="", encoding="utf-8") as f:
|
63 |
+
writer = csv.DictWriter(f, fieldnames=[
|
64 |
+
"id","original_path","prompt",
|
65 |
+
"agent1","img1_path","agent2","img2_path"
|
66 |
+
])
|
67 |
+
writer.writeheader()
|
68 |
+
|
69 |
+
if not os.path.exists(meta4):
|
70 |
+
with open(meta4, "w", newline="", encoding="utf-8") as f:
|
71 |
+
writer = csv.DictWriter(f, fieldnames=[
|
72 |
+
"id","original_path","prompt",
|
73 |
+
"agent1","img1_path","agent2","img2_path"
|
74 |
+
])
|
75 |
+
writer.writeheader()
|
76 |
+
|
77 |
+
if not os.path.exists(meta5):
|
78 |
+
with open(meta5, "w", newline="", encoding="utf-8") as f:
|
79 |
+
writer = csv.DictWriter(f, fieldnames=[
|
80 |
+
"id","original_path","prompt",
|
81 |
+
"agent1","img1_path","agent2","img2_path"
|
82 |
+
])
|
83 |
+
writer.writeheader()
|
84 |
+
|
85 |
+
if not os.path.exists(meta6):
|
86 |
+
with open(meta6, "w", newline="", encoding="utf-8") as f:
|
87 |
writer = csv.DictWriter(f, fieldnames=[
|
88 |
"id","original_path","prompt",
|
89 |
"agent1","img1_path","agent2","img2_path"
|
90 |
])
|
91 |
writer.writeheader()
|
92 |
|
93 |
+
eval0 = "data/evaluations/eval0.csv"
|
94 |
+
eval1 = "data/evaluations/eval1.csv"
|
95 |
+
eval2 = "data/evaluations/eval2.csv"
|
96 |
+
eval3 = "data/evaluations/eval3.csv"
|
97 |
+
eval4 = "data/evaluations/eval4.csv"
|
98 |
+
eval5 = "data/evaluations/eval5.csv"
|
99 |
+
eval6 = "data/evaluations/eval6.csv"
|
100 |
+
|
101 |
+
|
102 |
+
if not os.path.exists(eval0):
|
103 |
+
with open(eval0, "w", newline="", encoding="utf-8") as f:
|
104 |
writer = csv.DictWriter(f, fieldnames=[
|
105 |
+
"timestamp", "record_id",
|
106 |
"a1_follow","a1_creativity","a1_finesse",
|
107 |
"a2_follow","a2_creativity","a2_finesse"
|
108 |
])
|
109 |
writer.writeheader()
|
110 |
|
111 |
+
if not os.path.exists(eval1):
|
112 |
+
with open(eval1, "w", newline="", encoding="utf-8") as f:
|
113 |
+
writer = csv.DictWriter(f, fieldnames=[
|
114 |
+
"timestamp", "record_id",
|
115 |
+
"a1_follow","a1_creativity","a1_finesse",
|
116 |
+
"a2_follow","a2_creativity","a2_finesse"
|
117 |
+
])
|
118 |
+
writer.writeheader()
|
119 |
|
120 |
+
if not os.path.exists(eval2):
|
121 |
+
with open(eval2, "w", newline="", encoding="utf-8") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
writer = csv.DictWriter(f, fieldnames=[
|
123 |
+
"timestamp", "record_id",
|
124 |
+
"a1_follow","a1_creativity","a1_finesse",
|
125 |
+
"a2_follow","a2_creativity","a2_finesse"
|
126 |
])
|
127 |
+
writer.writeheader()
|
128 |
+
|
129 |
+
if not os.path.exists(eval3):
|
130 |
+
with open(eval3, "w", newline="", encoding="utf-8") as f:
|
131 |
+
writer = csv.DictWriter(f, fieldnames=[
|
132 |
+
"timestamp", "record_id",
|
133 |
+
"a1_follow","a1_creativity","a1_finesse",
|
134 |
+
"a2_follow","a2_creativity","a2_finesse"
|
135 |
+
])
|
136 |
+
writer.writeheader()
|
137 |
+
|
138 |
+
if not os.path.exists(eval4):
|
139 |
+
with open(eval4, "w", newline="", encoding="utf-8") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
writer = csv.DictWriter(f, fieldnames=[
|
141 |
+
"timestamp", "record_id",
|
142 |
"a1_follow","a1_creativity","a1_finesse",
|
143 |
"a2_follow","a2_creativity","a2_finesse"
|
144 |
])
|
145 |
+
writer.writeheader()
|
146 |
+
|
147 |
+
if not os.path.exists(eval5):
|
148 |
+
with open(eval5, "w", newline="", encoding="utf-8") as f:
|
149 |
+
writer = csv.DictWriter(f, fieldnames=[
|
150 |
+
"timestamp", "record_id",
|
151 |
+
"a1_follow","a1_creativity","a1_finesse",
|
152 |
+
"a2_follow","a2_creativity","a2_finesse"
|
153 |
+
])
|
154 |
+
writer.writeheader()
|
155 |
+
|
156 |
+
if not os.path.exists(eval6):
|
157 |
+
with open(eval6, "w", newline="", encoding="utf-8") as f:
|
158 |
+
writer = csv.DictWriter(f, fieldnames=[
|
159 |
+
"timestamp", "record_id",
|
160 |
+
"a1_follow","a1_creativity","a1_finesse",
|
161 |
+
"a2_follow","a2_creativity","a2_finesse"
|
162 |
+
])
|
163 |
+
writer.writeheader()
|
164 |
+
|
165 |
+
|
166 |
+
def run_agent_on_image(original_img: Image.Image, prompt: str, agent_name: str) -> Image.Image:
|
167 |
+
if original_img is None:
|
168 |
+
raise ValueError("Input image cannot be None")
|
169 |
+
if not prompt or prompt.strip() == "":
|
170 |
+
raise ValueError("Prompt cannot be empty")
|
171 |
+
return original_img # TODO: implement actual agent processing
|
172 |
+
|
173 |
+
def save_to_library(task_id, orig_img, prompt, a1, a2, img1, img2):
|
174 |
+
try:
|
175 |
+
if any(img is None for img in [orig_img, img1, img2]):
|
176 |
+
raise ValueError("All images must be valid")
|
177 |
+
if not prompt or prompt.strip() == "":
|
178 |
+
raise ValueError("Prompt cannot be empty")
|
179 |
+
|
180 |
+
orig_id = uuid.uuid4().hex
|
181 |
+
|
182 |
+
orig_path = f"data/images/task{task_id}/orig_imgs/{orig_id}.png"
|
183 |
+
img1_path = f"data/images/task{task_id}/processed_imgs/{orig_id}_a1.png"
|
184 |
+
img2_path = f"data/images/task{task_id}/processed_imgs/{orig_id}_a2.png"
|
185 |
+
|
186 |
+
# 使用 try-except 处理图片保存
|
187 |
+
try:
|
188 |
+
orig_img.save(orig_path)
|
189 |
+
img1.save(img1_path)
|
190 |
+
img2.save(img2_path)
|
191 |
+
except Exception as e:
|
192 |
+
raise IOError(f"Failed to save images: {str(e)}")
|
193 |
+
|
194 |
+
# 使用 try-except 处理 CSV 写入
|
195 |
+
try:
|
196 |
+
with open(f"data/metadatas/meta{task_id}.csv", "a", newline="", encoding="utf-8") as f:
|
197 |
+
writer = csv.DictWriter(f, fieldnames=[
|
198 |
+
"id","original_path", "prompt",
|
199 |
+
"agent1","img1_path","agent2","img2_path"
|
200 |
+
])
|
201 |
+
writer.writerow({
|
202 |
+
"id": orig_id,
|
203 |
+
"original_path": orig_path,
|
204 |
+
"prompt": prompt,
|
205 |
+
"agent1": a1,
|
206 |
+
"img1_path": img1_path,
|
207 |
+
"agent2": a2,
|
208 |
+
"img2_path": img2_path
|
209 |
+
})
|
210 |
+
except Exception as e:
|
211 |
+
# 如果写入CSV失败,清理已保存的图片
|
212 |
+
for path in [orig_path, img1_path, img2_path]:
|
213 |
+
if os.path.exists(path):
|
214 |
+
os.remove(path)
|
215 |
+
raise IOError(f"Failed to write metadata: {str(e)}")
|
216 |
+
|
217 |
+
except Exception as e:
|
218 |
+
raise Exception(f"Error in save_to_library: {str(e)}")
|
219 |
+
|
220 |
+
def generate_and_store(task_id, orig_img, prompt, a1, a2):
|
221 |
+
try:
|
222 |
+
if orig_img is None:
|
223 |
+
return None, None
|
224 |
+
if not prompt or prompt.strip() == "":
|
225 |
+
return None, None
|
226 |
+
if a1 == a2:
|
227 |
+
return None, None # 不允许选择相同的Agent
|
228 |
+
|
229 |
+
out1 = run_agent_on_image(orig_img, prompt, a1)
|
230 |
+
out2 = run_agent_on_image(orig_img, prompt, a2)
|
231 |
+
save_to_library(task_id, orig_img, prompt, a1, a2, out1, out2)
|
232 |
+
return out1, out2
|
233 |
+
except Exception as e:
|
234 |
+
print(f"Error in generate_and_store: {str(e)}")
|
235 |
+
return None, None
|
236 |
+
|
237 |
+
|
238 |
+
def load_random_record(task_id):
|
239 |
+
try:
|
240 |
+
# 检查文件是否存在
|
241 |
+
meta_file = f"data/metadatas/meta{task_id}.csv"
|
242 |
+
if not os.path.exists(meta_file):
|
243 |
+
return "", None, "Metadata file not found", None, None
|
244 |
+
|
245 |
+
# 读取所有记录
|
246 |
+
with open(meta_file, "r", encoding="utf-8") as f:
|
247 |
+
all_records = list(csv.DictReader(f))
|
248 |
+
|
249 |
+
if not all_records:
|
250 |
+
return "", None, "No records in library", None, None
|
251 |
+
|
252 |
+
# 读取最近5分钟内的评测记录
|
253 |
+
recent_evaluated_ids = set()
|
254 |
+
current_time = datetime.now()
|
255 |
+
|
256 |
+
eval_file = f"data/evaluations/eval{task_id}.csv"
|
257 |
+
if os.path.exists(eval_file):
|
258 |
+
try:
|
259 |
+
with open(eval_file, "r", encoding="utf-8") as f:
|
260 |
+
eval_records = list(csv.DictReader(f))
|
261 |
+
|
262 |
+
for record in eval_records:
|
263 |
+
try:
|
264 |
+
eval_time = datetime.fromisoformat(record["timestamp"])
|
265 |
+
time_diff = (current_time - eval_time).total_seconds() / 60
|
266 |
+
|
267 |
+
if time_diff <= 5:
|
268 |
+
recent_evaluated_ids.add(record["record_id"])
|
269 |
+
except ValueError:
|
270 |
+
# 跳过无效的时间戳
|
271 |
+
continue
|
272 |
+
except Exception as e:
|
273 |
+
print(f"Error reading evaluation file: {str(e)}")
|
274 |
+
|
275 |
+
available_records = [r for r in all_records if r["id"] not in recent_evaluated_ids]
|
276 |
+
|
277 |
+
if not available_records:
|
278 |
+
return "", None, "All available records have been recently evaluated", None, None
|
279 |
+
|
280 |
+
rec = random.choice(available_records)
|
281 |
+
|
282 |
+
# 验证图片文件是否存在
|
283 |
+
for path in [rec["original_path"], rec["img1_path"], rec["img2_path"]]:
|
284 |
+
if not os.path.exists(path):
|
285 |
+
return "", None, f"Image file not found: {path}", None, None
|
286 |
+
|
287 |
+
return (
|
288 |
+
rec["id"],
|
289 |
+
rec["original_path"],
|
290 |
+
rec["prompt"],
|
291 |
+
rec["img1_path"],
|
292 |
+
rec["img2_path"]
|
293 |
+
)
|
294 |
+
except Exception as e:
|
295 |
+
return "", None, f"Error loading record: {str(e)}", None, None
|
296 |
+
|
297 |
+
|
298 |
+
def save_evaluation(task_id, record_id,
|
299 |
+
a1_follow, a1_creativity, a1_finesse,
|
300 |
+
a2_follow, a2_creativity, a2_finesse):
|
301 |
+
try:
|
302 |
+
# 验证输入
|
303 |
+
if not record_id:
|
304 |
+
return "❌ Invalid record ID", *load_random_record(task_id)
|
305 |
+
|
306 |
+
# 验证评分
|
307 |
+
scores = [a1_follow, a1_creativity, a1_finesse,
|
308 |
+
a2_follow, a2_creativity, a2_finesse]
|
309 |
+
if any(score is None for score in scores):
|
310 |
+
return "❌ Please complete all evaluations", *load_random_record(task_id)
|
311 |
+
|
312 |
+
with open(f"data/evaluations/eval{task_id}.csv", "a", newline="", encoding="utf-8") as f:
|
313 |
+
writer = csv.DictWriter(f, fieldnames=[
|
314 |
+
"timestamp", "record_id",
|
315 |
+
"a1_follow","a1_creativity","a1_finesse",
|
316 |
+
"a2_follow","a2_creativity","a2_finesse"
|
317 |
+
])
|
318 |
+
writer.writerow({
|
319 |
+
"timestamp": datetime.now().isoformat(),
|
320 |
+
"record_id": record_id,
|
321 |
+
"a1_follow": a1_follow,
|
322 |
+
"a1_creativity": a1_creativity,
|
323 |
+
"a1_finesse": a1_finesse,
|
324 |
+
"a2_follow": a2_follow,
|
325 |
+
"a2_creativity": a2_creativity,
|
326 |
+
"a2_finesse": a2_finesse
|
327 |
+
})
|
328 |
+
|
329 |
+
return "✅ Evaluation submitted!", *load_random_record(task_id)
|
330 |
+
except Exception as e:
|
331 |
+
return f"❌ Error saving evaluation: {str(e)}", *load_random_record(task_id)
|
332 |
+
|
333 |
+
|
334 |
MODEL_CHOICES = ["Model A", "Model B", "Model C"]
|
335 |
TASK_CHOICES = [
|
336 |
"Image Restoration",
|
|
|
372 |
|
373 |
# ——— Tab 2: Human as Judge ———
|
374 |
with gr.TabItem("Human as Judge"):
|
|
|
375 |
record_id_state = gr.State("")
|
376 |
+
task_dropdown = gr.Dropdown(choices=TASK_CHOICES, label="Task Category", type="index")
|
377 |
|
378 |
+
# 原图与 Prompt 并排
|
379 |
+
with gr.Row():
|
380 |
+
judge_orig = gr.Image(label="Original Image")
|
381 |
+
judge_prompt = gr.Textbox(label="Prompt", interactive=False)
|
382 |
+
|
383 |
+
# 两张结果图并排
|
384 |
+
with gr.Row():
|
385 |
+
judge_out1 = gr.Image(label="Agent 1 Result")
|
386 |
+
judge_out2 = gr.Image(label="Agent 2 Result")
|
387 |
|
388 |
+
# 当选 Task 时加载随机样本
|
389 |
task_dropdown.change(
|
390 |
fn=load_random_record,
|
391 |
+
inputs=[task_dropdown],
|
392 |
outputs=[record_id_state, judge_orig, judge_prompt, judge_out1, judge_out2],
|
393 |
show_api=False
|
394 |
)
|
395 |
|
|
|
396 |
with gr.Row():
|
397 |
+
gr.Markdown("## Please Evaluate the Original Image from 3 Aspects").style(text_align="center")
|
398 |
with gr.Column():
|
|
|
399 |
a1_follow = gr.Radio([0,1,2,3,4,5], label="Follow Prompt")
|
400 |
a1_creativity = gr.Radio([0,1,2,3,4,5], label="Creativity")
|
401 |
a1_finesse = gr.Radio([0,1,2,3,4,5], label="Finesse/Detail")
|
402 |
with gr.Column():
|
|
|
403 |
a2_follow = gr.Radio([0,1,2,3,4,5], label="Follow Prompt")
|
404 |
a2_creativity = gr.Radio([0,1,2,3,4,5], label="Creativity")
|
405 |
a2_finesse = gr.Radio([0,1,2,3,4,5], label="Finesse/Detail")
|
406 |
|
407 |
submit_btn = gr.Button("Submit Evaluation")
|
408 |
submit_status = gr.Textbox(label="Status", interactive=False)
|
409 |
+
|
410 |
submit_btn.click(
|
411 |
fn=save_evaluation,
|
412 |
inputs=[
|
413 |
+
task_dropdown, record_id_state,
|
414 |
a1_follow, a1_creativity, a1_finesse,
|
415 |
a2_follow, a2_creativity, a2_finesse
|
416 |
],
|
417 |
+
outputs=[
|
418 |
+
submit_status,
|
419 |
+
record_id_state, judge_orig, judge_prompt, judge_out1, judge_out2
|
420 |
+
],
|
421 |
show_api=False
|
422 |
)
|
423 |
+
|
424 |
|
425 |
demo.queue()
|
426 |
demo.launch(
|
427 |
share=False,
|
428 |
show_api=False,
|
429 |
ssr_mode=False
|
430 |
+
)
|
data/metadata.csv
DELETED
File without changes
|
test.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid
|
2 |
+
|
3 |
+
rec_id = uuid.uuid4().hex
|
4 |
+
print(rec_id)
|