Saravutw commited on
Commit
017e4f8
·
verified ·
1 Parent(s): bc3796a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -33
app.py CHANGED
@@ -2,72 +2,103 @@ import os
2
  import gradio as gr
3
  from gradio_client import Client, handle_file
4
 
5
- # Configuration
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  TARGET_SPACE = "selfit-camera/omni-image-editor"
8
 
9
  def remote_bridge(inputs, prompt, mode):
 
10
  try:
11
  client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
 
12
  if mode == "/multi_edit":
13
- # กรองเอาเฉพาะช่องที่มีกาอัปโหลดรูปจริ
14
  valid_files = [handle_file(img) for img in inputs if img is not None]
15
- if not valid_files: return None, "Status: Please upload at least one image."
 
16
  result = client.predict(images=valid_files, prompt=prompt, api_name="/multi_edit")
 
17
  elif mode == "/t2i":
18
  result = client.predict(prompt=prompt, api_name="/t2i")
 
19
  else:
 
 
20
  result = client.predict(image=handle_file(inputs), prompt=prompt, api_name=mode)
21
- return result, f"✅ Success ({mode})"
 
22
  except Exception as e:
23
- return None, f"❌ Error: {str(e)}"
24
 
 
25
  with gr.Blocks(css=".container { max-width: 1200px; margin: auto; }") as demo:
26
- gr.HTML("<h1 style='text-align: center;'>🎨 Omni Editor 2.0 (Remote)</h1>")
 
27
 
28
  with gr.Tabs():
29
- # --- TAB 1: Single Image ---
30
  with gr.TabItem("🖼️ Single Image Edit"):
31
  with gr.Row():
32
  with gr.Column(scale=5):
33
- s_in = gr.Image(type="filepath", label="Input Image")
34
- s_prompt = gr.Textbox(label="Prompt", lines=2)
35
- s_run = gr.Button("🎨 Execute", variant="primary")
36
  with gr.Column(scale=5):
37
- s_out = gr.Image(label="Result")
38
- s_status = gr.Textbox(label="Status", interactive=False)
 
39
  s_run.click(lambda i, p: remote_bridge(i, p, "/predict"), [s_in, s_prompt], [s_out, s_status])
 
40
 
41
- # --- TAB 2: Multi-Image (แก้ให้มี 3 ช่องแยกตามสเปก 8B MM-DiT) ---
42
  with gr.TabItem("🖼️🖼️ Multi-Image Edit"):
43
- gr.Markdown("### 🚀 Multi-Modal Fusion (Adaptive Multi-Modal Gating)")
44
  with gr.Row():
45
- with gr.Column(scale=5):
46
- with gr.Row(): # แบ่งช่องใส่รูป่อยๆ
47
- m_in1 = gr.Image(type="filepath", label="Base Image (A)", height=200)
48
- m_in2 = gr.Image(type="filepath", label="Ref Image (B)", height=200)
49
- m_in3 = gr.Image(type="filepath", label="Style Image (C)", height=200)
50
- m_prompt = gr.Textbox(label="Fusion Instruction", placeholder="e.g., Apply style C to object in A using B as reference", lines=2)
51
- m_run = gr.Button("🎯 Execute Multi-Fusion", variant="primary")
52
- with gr.Column(scale=5):
53
- m_out = gr.Image(label="Fused Result", height=450)
54
  m_status = gr.Textbox(label="Status", interactive=False)
55
-
56
- # ส่ง List ของทั้ง 3 ช่องไปที่ฟังก์ชัน
57
  m_run.click(
58
- fn=lambda i1, i2, i3, p: remote_bridge([i1, i2, i3], p, "/multi_edit"),
59
- inputs=[m_in1, m_in2, m_in3, m_prompt],
60
  outputs=[m_out, m_status]
61
  )
62
 
63
- # --- TAB 3: Text to Image / Tools --- (ส่วนที่เหลือคงเดิม)
64
  with gr.TabItem("✨ Text to Image"):
65
  with gr.Row():
66
  with gr.Column(scale=5):
67
- t_prompt = gr.Textbox(label="Description", lines=4)
68
- t_run = gr.Button("✨ Generate", variant="primary")
69
  with gr.Column(scale=5):
70
- t_out = gr.Image(label="AI Result")
71
- t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, s_status])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- demo.queue().launch()
 
 
2
  import gradio as gr
3
  from gradio_client import Client, handle_file
4
 
5
+ # Configuration - ดึง Token จาก Secrets
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  TARGET_SPACE = "selfit-camera/omni-image-editor"
8
 
9
  def remote_bridge(inputs, prompt, mode):
10
+ """ฟังก์ชันส่งคำสั่งไปที่ API ของ Omni 2.0"""
11
  try:
12
  client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
13
+
14
  if mode == "/multi_edit":
15
+ # กรองรูปจากทั้ 3 ช่อง (Base, Ref, Style)
16
  valid_files = [handle_file(img) for img in inputs if img is not None]
17
+ if not valid_files:
18
+ return None, "Status: Please upload images in the slots."
19
  result = client.predict(images=valid_files, prompt=prompt, api_name="/multi_edit")
20
+
21
  elif mode == "/t2i":
22
  result = client.predict(prompt=prompt, api_name="/t2i")
23
+
24
  else:
25
+ # Single Edit, Upscale, Watermark
26
+ if inputs is None: return None, "Status: No image provided."
27
  result = client.predict(image=handle_file(inputs), prompt=prompt, api_name=mode)
28
+
29
+ return result, f"✅ Success: {mode}"
30
  except Exception as e:
31
+ return None, f"❌ API Error: {str(e)}"
32
 
33
+ # สร้าง UI ด้วยความละเอียดรอบคอบ
34
  with gr.Blocks(css=".container { max-width: 1200px; margin: auto; }") as demo:
35
+ gr.HTML("<h1 style='text-align: center; margin-bottom: 0;'>🎨 Omni Editor 2.0 (Full Remote)</h1>")
36
+ gr.HTML("<p style='text-align: center; opacity: 0.7;'>Native 8B MM-DiT Bridge | High-Fidelity Multi-Modal Results</p>")
37
 
38
  with gr.Tabs():
39
+ # --- TAB 1: Single Image Edit ---
40
  with gr.TabItem("🖼️ Single Image Edit"):
41
  with gr.Row():
42
  with gr.Column(scale=5):
43
+ s_in = gr.Image(type="filepath", label="Input Image", height=420)
44
+ s_prompt = gr.Textbox(label="Editing Instruction", placeholder="e.g. Add a red hat", lines=2)
45
+ s_run = gr.Button("🎯 Execute Edit", variant="primary")
46
  with gr.Column(scale=5):
47
+ s_out = gr.Image(label="Output Result", height=420)
48
+ s_status = gr.Textbox(label="System Status", interactive=False)
49
+ s_use = gr.Button("🔄 Use as New Input")
50
  s_run.click(lambda i, p: remote_bridge(i, p, "/predict"), [s_in, s_prompt], [s_out, s_status])
51
+ s_use.click(fn=lambda x: x, inputs=s_out, outputs=s_in)
52
 
53
+ # --- TAB 2: Multi-Image Edit (แก 3 ช่องตามสเปก AMG) ---
54
  with gr.TabItem("🖼️🖼️ Multi-Image Edit"):
55
+ gr.Markdown("### 🚀 Multi-Modal Fusion (Unified Transformer Backbone)")
56
  with gr.Row():
57
+ with gr.Column(scale=6):
58
+ with gr.Row(): # แถวช่องใส่ภาพ 3 ใบแกกัน
59
+ m_in1 = gr.Image(type="filepath", label="Base Image (A)", height=180)
60
+ m_in2 = gr.Image(type="filepath", label="Ref Image (B)", height=180)
61
+ m_in3 = gr.Image(type="filepath", label="Style Image (C)", height=180)
62
+ m_prompt = gr.Textbox(label="Fusion Prompt", placeholder="Describe how images should interact...", lines=2)
63
+ m_run = gr.Button("🔗 Execute Multi-Fusion", variant="primary")
64
+ with gr.Column(scale=4):
65
+ m_out = gr.Image(label="Fused Result", height=400)
66
  m_status = gr.Textbox(label="Status", interactive=False)
 
 
67
  m_run.click(
68
+ fn=lambda i1, i2, i3, p: remote_bridge([i1, i2, i3], p, "/multi_edit"),
69
+ inputs=[m_in1, m_in2, m_in3, m_prompt],
70
  outputs=[m_out, m_status]
71
  )
72
 
73
+ # --- TAB 3: Text to Image ---
74
  with gr.TabItem("✨ Text to Image"):
75
  with gr.Row():
76
  with gr.Column(scale=5):
77
+ t_prompt = gr.Textbox(label="Prompt", placeholder="What do you want to see?", lines=5)
78
+ t_run = gr.Button("✨ Generate Image", variant="primary")
79
  with gr.Column(scale=5):
80
+ t_out = gr.Image(label="Generated Result", height=400)
81
+ t_status = gr.Textbox(label="Status", interactive=False)
82
+ t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, t_status])
83
+
84
+ # --- TAB 4: Tools (Upscale / Watermark) ---
85
+ with gr.TabItem("🔍 Tools"):
86
+ with gr.Row():
87
+ with gr.Column(scale=5):
88
+ tool_mode = gr.Radio(["Upscale", "Remove Watermark"], label="Select Function", value="Upscale")
89
+ tool_in = gr.Image(type="filepath", label="Input Image")
90
+ tool_run = gr.Button("🚀 Process", variant="primary")
91
+ with gr.Column(scale=5):
92
+ tool_out = gr.Image(label="Result")
93
+ tool_status = gr.Textbox(label="Status", interactive=False)
94
+
95
+ def tool_router(mode, img):
96
+ api_path = "/upscale" if mode == "Upscale" else "/remove_watermark"
97
+ return remote_bridge(img, "", api_path)
98
+
99
+ tool_run.click(tool_router, [tool_mode, tool_in], [tool_out, tool_status])
100
+
101
+ gr.HTML("<hr><p style='text-align: center; font-size: 0.8em;'>Omni Creator 2.0 | FP8 + RoPE + AMG Optimization</p>")
102
 
103
+ # Launch
104
+ demo.queue().launch(server_name="0.0.0.0")