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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -46
app.py CHANGED
@@ -2,63 +2,88 @@ 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 process_via_api(image_path, prompt):
10
- if not image_path:
11
- return None, "Status: Please upload an image."
12
 
13
  try:
14
- # Connect to the target space
15
- client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
16
 
17
- # Call the prediction API
18
- result = client.predict(
19
- image=handle_file(image_path),
20
- prompt=prompt,
21
- api_name="/predict"
22
- )
23
- return result, "Status: Processed successfully via API"
 
 
 
 
24
  except Exception as e:
25
- return None, f"Status Error: {str(e)}"
26
 
27
- # UI Construction (Minimalist & Stable for Python 3.13)
28
  with gr.Blocks() as demo:
29
- gr.HTML("<h1 style='text-align: center;'>🎨 Omni Editor 2.0 (Remote)</h1>")
30
- gr.HTML("<div style='text-align: center; background: #6366f1; color: white; padding: 10px; border-radius: 10px; margin-bottom: 20px;'>Powered by Omni-Image-Editor API | Running on Free Tier</div>")
31
 
32
- with gr.Tabs():
33
- with gr.TabItem("🖼️ Image Editing"):
 
34
  with gr.Row():
35
  with gr.Column(scale=1):
36
- input_img = gr.Image(type="filepath", label="Input Image")
37
- input_prompt = gr.Textbox(
38
- label="Editing Instruction",
39
- placeholder="e.g. 'add a red hat to the dog'",
40
- lines=3
41
- )
42
- submit_btn = gr.Button("Execute", variant="primary")
43
-
44
  with gr.Column(scale=1):
45
- output_img = gr.Image(label="Output Result")
46
- status_box = gr.Textbox(label="System Status", interactive=False)
47
- use_as_input = gr.Button("🔄 Use Result as New Input")
48
 
49
- # Event Handlers
50
- submit_btn.click(
51
- fn=process_via_api,
52
- inputs=[input_img, input_prompt],
53
- outputs=[output_img, status_box]
54
- )
55
-
56
- # Loop back output to input
57
- use_as_input.click(
58
- fn=lambda x: x,
59
- inputs=output_img,
60
- outputs=input_img
61
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.queue(max_size=20).launch(server_name="0.0.0.0")
 
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(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")