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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -2,82 +2,92 @@ import os
2
  import gradio as gr
3
  from gradio_client import Client, handle_file
4
 
5
- # Config
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()
 
 
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
+ """ฟังก์ชันกลางสำหรับส่งคำสั่งไปที่ API ต้นทาง"""
11
+ if not inputs and mode != "/t2i":
12
+ return None, "Status: Please provide input files."
13
+
14
  try:
15
+ client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
16
 
17
+ if mode == "/multi_edit":
18
+ # ส่งไฟล์หลายใบ (List of files) สำหรับ Multi-modal fusion
19
+ file_paths = [handle_file(f.name) for f in inputs]
20
+ result = client.predict(images=file_paths, prompt=prompt, api_name="/multi_edit")
21
+ elif mode == "/t2i":
22
  result = client.predict(prompt=prompt, api_name="/t2i")
 
 
 
 
23
  else:
24
+ # Single Edit (/predict), Upscale, Watermark
25
+ result = client.predict(image=handle_file(inputs), prompt=prompt, api_name=mode)
26
 
27
+ return result, f"Status: Processed via {mode} successfully."
28
  except Exception as e:
29
  return None, f"Remote Error: {str(e)}"
30
 
31
+ # สร้างหน้าจอ UI แบบแบ่งส่วนชัดเจน
32
  with gr.Blocks() as demo:
33
+ gr.HTML("<h1 style='text-align: center;'>🎨 Omni Editor 2.0 (Full Remote)</h1>")
34
 
35
  with gr.Tabs():
36
+ # --- TAB 1: Single Image ---
37
  with gr.TabItem("🖼️ Single Image Edit"):
38
  with gr.Row():
39
  with gr.Column(scale=1):
40
  s_in = gr.Image(type="filepath", label="Input Image")
41
+ s_prompt = gr.Textbox(label="Prompt", placeholder="e.g., Change background to snow")
42
  s_run = gr.Button("Execute Edit", variant="primary")
43
  with gr.Column(scale=1):
44
  s_out = gr.Image(label="Result")
45
  s_status = gr.Textbox(label="Status", interactive=False)
46
+ s_run.click(lambda i, p: remote_bridge(i, p, "/predict"), [s_in, s_prompt], [s_out, s_status])
47
 
48
+ # --- TAB 2: Multi-Image (แก้ให้รับได้หลายรูปตามสเปก 8B MM-DiT) ---
49
  with gr.TabItem("🖼️🖼️ Multi-Image Edit"):
50
+ gr.Markdown("### 🚀 Multi-Modal Fusion (Select multiple reference images)")
51
  with gr.Row():
52
  with gr.Column(scale=1):
53
+ m_in = gr.File(label="Upload Images (Original + References)", file_count="multiple", file_types=["image"])
54
+ m_prompt = gr.Textbox(label="Instruction", placeholder="How to fuse these images?")
 
55
  m_run = gr.Button("Execute Multi-Edit", variant="primary")
56
  with gr.Column(scale=1):
57
  m_out = gr.Image(label="Result")
58
  m_status = gr.Textbox(label="Status", interactive=False)
59
+ m_run.click(lambda i, p: remote_bridge(i, p, "/multi_edit"), [m_in, m_prompt], [m_out, m_status])
60
 
61
+ # --- TAB 3: Text to Image ---
62
  with gr.TabItem("✨ Text to Image"):
63
  with gr.Row():
64
+ with gr.Column(scale=1):
65
+ t_prompt = gr.Textbox(label="Prompt", placeholder="What do you want to generate?")
66
+ t_run = gr.Button("Generate Image", variant="primary")
67
+ with gr.Column(scale=1):
68
  t_out = gr.Image(label="Result")
69
+ t_status = gr.Textbox(label="Status", interactive=False)
70
+ t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, t_status])
71
 
72
+ # --- TAB 4: Tools (Upscale/Watermark) ---
73
+ with gr.TabItem("🔍 Tools (Upscale/Watermark)"):
 
74
  with gr.Row():
75
+ with gr.Column(scale=1):
76
+ tool_mode = gr.Radio(["Upscale", "/remove_watermark"], label="Select Tool", value="Upscale")
77
+ tool_in = gr.Image(type="filepath", label="Input Image")
78
+ tool_run = gr.Button("Process Tool", variant="primary")
79
+ with gr.Column(scale=1):
80
  tool_out = gr.Image(label="Result")
81
+ tool_status = gr.Textbox(label="Status", interactive=False)
82
 
83
+ def tool_router(mode, img):
84
+ api_path = "/upscale" if mode == "Upscale" else "/remove_watermark"
85
+ return remote_bridge(img, "", api_path)
86
 
87
+ tool_run.click(tool_router, [tool_mode, tool_in], [tool_out, tool_status])
88
+
89
+ gr.Markdown("---")
90
+ gr.HTML("<p style='text-align: center;'>Omni Creator 2.0: 8B Unified Multi-Modal Diffusion Transformer</p>")
91
 
92
+ # Launch settings
93
+ demo.queue().launch(server_name="0.0.0.0")