Saravutw commited on
Commit
f981c30
·
verified ·
1 Parent(s): a90f818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -6,84 +6,78 @@ from gradio_client import Client, handle_file
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  TARGET = "selfit-camera/omni-image-editor"
8
 
9
- def call_remote_api(image, prompt, mode_api_name):
10
- if image is None and mode_api_name != "/t2i": # T2I ไม่ต้องใช้รูป
11
- return None, "Error: Please provide necessary input."
12
-
13
  try:
14
  client = Client(TARGET, hf_token=HF_TOKEN)
15
 
16
- # จัดการ input ตามแต่ละโหมด
17
  if mode_api_name == "/t2i":
18
  result = client.predict(prompt=prompt, api_name="/t2i")
19
  elif mode_api_name == "/multi_edit":
20
- # สหรับ Multi-edit ต้นทงอาจ้องก list ของไฟล์
21
- result = client.predict(images=[handle_file(image)], prompt=prompt, api_name="/multi_edit")
 
22
  else:
23
- # โหมดทั่วไป: Single Edit, Upscale, Watermark
24
- result = client.predict(image=handle_file(image), prompt=prompt, api_name=mode_api_name)
25
 
26
- return result, f"Status: Processed via {mode_api_name}"
27
  except Exception as e:
28
  return None, f"Remote Error: {str(e)}"
29
 
30
- # UI Layout แบบไม่มั่ว (Row/Column สัดส่วนชัดเจน)
31
  with gr.Blocks() as demo:
32
- gr.HTML("<h1 style='text-align: center;'>🎨 Omni Editor 2.0 (Full Remote)</h1>")
33
 
34
- with gr.Tabs() as tabs:
35
  # 1. Single Image Edit
36
  with gr.TabItem("🖼️ Single Image Edit"):
37
  with gr.Row():
38
  with gr.Column(scale=1):
39
  s_in = gr.Image(type="filepath", label="Input Image")
40
- s_prompt = gr.Textbox(label="Prompt", placeholder="e.g. Change background to Mars")
41
  s_run = gr.Button("Execute Edit", variant="primary")
42
  with gr.Column(scale=1):
43
  s_out = gr.Image(label="Result")
44
  s_status = gr.Textbox(label="Status", interactive=False)
45
  s_run.click(lambda img, txt: call_remote_api(img, txt, "/predict"), [s_in, s_prompt], [s_out, s_status])
46
 
47
- # 2. Multi-Image Edit
48
  with gr.TabItem("🖼️🖼️ Multi-Image Edit"):
49
- gr.Markdown("Select multiple images to edit (Handled by 8B MM-DiT)")
50
  with gr.Row():
51
- with gr.Column():
52
- m_in = gr.Image(type="filepath", label="Reference Image")
53
- m_prompt = gr.Textbox(label="Instruction")
 
54
  m_run = gr.Button("Execute Multi-Edit", variant="primary")
55
- with gr.Column():
56
  m_out = gr.Image(label="Result")
57
- m_run.click(lambda img, txt: call_remote_api(img, txt, "/multi_edit"), [m_in, m_prompt], [m_out, s_status])
 
58
 
59
  # 3. Text to Image
60
  with gr.TabItem("✨ Text to Image"):
61
  with gr.Row():
62
  with gr.Column():
63
- t_prompt = gr.Textbox(label="What do you want to create?")
64
- t_run = gr.Button("Generate Image", variant="primary")
65
  with gr.Column():
66
- t_out = gr.Image(label="Generated Result")
67
  t_run.click(lambda txt: call_remote_api(None, txt, "/t2i"), [t_prompt], [t_out, s_status])
68
 
69
  # 4. Image Upscale & Watermark
70
- with gr.TabItem("🔍 Tools (Upscale/Watermark)"):
71
  tool_mode = gr.Radio(["Upscale", "Remove Watermark"], label="Select Tool", value="Upscale")
72
  with gr.Row():
73
  with gr.Column():
74
  tool_in = gr.Image(type="filepath", label="Input")
75
  tool_run = gr.Button("Process", variant="primary")
76
  with gr.Column():
77
- tool_out = gr.Image(label="Processed Result")
78
 
79
- # เลือก API name ตาม Radio
80
  def tool_handler(mode, img):
81
  api_name = "/upscale" if mode == "Upscale" else "/remove_watermark"
82
  return call_remote_api(img, "", api_name)
83
 
84
  tool_run.click(tool_handler, [tool_mode, tool_in], [tool_out, s_status])
85
 
86
- gr.Markdown("---")
87
- gr.Markdown("🤖 **Omni Creator 2.0:** 8B Unified Multi-Modal Diffusion Transformer")
88
-
89
- demo.queue().launch(server_name="0.0.0.0")
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  TARGET = "selfit-camera/omni-image-editor"
8
 
9
+ def call_remote_api(images, prompt, mode_api_name):
 
 
 
10
  try:
11
  client = Client(TARGET, hf_token=HF_TOKEN)
12
 
 
13
  if mode_api_name == "/t2i":
14
  result = client.predict(prompt=prompt, api_name="/t2i")
15
  elif mode_api_name == "/multi_edit":
16
+ # ส่ง List ของไฟล์ภาพทั้งมด (องรับสูงสุด 3 ตามสเปก 8B MM-DiT)
17
+ files = [handle_file(img.name if hasattr(img, 'name') else img) for img in images]
18
+ result = client.predict(images=files, prompt=prompt, api_name="/multi_edit")
19
  else:
20
+ # Single Edit, Upscale, Watermark
21
+ result = client.predict(image=handle_file(images), prompt=prompt, api_name=mode_api_name)
22
 
23
+ return result, f"Status: Success ({mode_api_name})"
24
  except Exception as e:
25
  return None, f"Remote Error: {str(e)}"
26
 
 
27
  with gr.Blocks() as demo:
28
+ gr.HTML("<h1 style='text-align: center;'>🎨 Omni Editor 2.0 (Remote)</h1>")
29
 
30
+ with gr.Tabs():
31
  # 1. Single Image Edit
32
  with gr.TabItem("🖼️ Single Image Edit"):
33
  with gr.Row():
34
  with gr.Column(scale=1):
35
  s_in = gr.Image(type="filepath", label="Input Image")
36
+ s_prompt = gr.Textbox(label="Prompt", placeholder="Describe your edit...")
37
  s_run = gr.Button("Execute Edit", variant="primary")
38
  with gr.Column(scale=1):
39
  s_out = gr.Image(label="Result")
40
  s_status = gr.Textbox(label="Status", interactive=False)
41
  s_run.click(lambda img, txt: call_remote_api(img, txt, "/predict"), [s_in, s_prompt], [s_out, s_status])
42
 
43
+ # 2. Multi-Image Edit (แก้ไขจุดที่มักง่ายแล้ว: รับได้หลายรูป)
44
  with gr.TabItem("🖼️🖼️ Multi-Image Edit"):
45
+ gr.Markdown("### 🚀 Multi-Modal Fusion (Select up to 3 images)")
46
  with gr.Row():
47
+ with gr.Column(scale=1):
48
+ # ใช้ File component แบบ multiple เพื่อให้เลือกได้หลายรูปพร้อมกัน
49
+ m_in = gr.File(label="Upload Images (Base/Ref/Style)", file_count="multiple", file_types=["image"])
50
+ m_prompt = gr.Textbox(label="Instruction", placeholder="How should these images interact?")
51
  m_run = gr.Button("Execute Multi-Edit", variant="primary")
52
+ with gr.Column(scale=1):
53
  m_out = gr.Image(label="Result")
54
+ m_status = gr.Textbox(label="Status", interactive=False)
55
+ m_run.click(call_remote_api, [m_in, m_prompt, gr.State("/multi_edit")], [m_out, m_status])
56
 
57
  # 3. Text to Image
58
  with gr.TabItem("✨ Text to Image"):
59
  with gr.Row():
60
  with gr.Column():
61
+ t_prompt = gr.Textbox(label="Describe the image you want")
62
+ t_run = gr.Button("Generate", variant="primary")
63
  with gr.Column():
64
+ t_out = gr.Image(label="Result")
65
  t_run.click(lambda txt: call_remote_api(None, txt, "/t2i"), [t_prompt], [t_out, s_status])
66
 
67
  # 4. Image Upscale & Watermark
68
+ with gr.TabItem("🔍 Tools"):
69
  tool_mode = gr.Radio(["Upscale", "Remove Watermark"], label="Select Tool", value="Upscale")
70
  with gr.Row():
71
  with gr.Column():
72
  tool_in = gr.Image(type="filepath", label="Input")
73
  tool_run = gr.Button("Process", variant="primary")
74
  with gr.Column():
75
+ tool_out = gr.Image(label="Result")
76
 
 
77
  def tool_handler(mode, img):
78
  api_name = "/upscale" if mode == "Upscale" else "/remove_watermark"
79
  return call_remote_api(img, "", api_name)
80
 
81
  tool_run.click(tool_handler, [tool_mode, tool_in], [tool_out, s_status])
82
 
83
+ demo.queue().launch()