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