Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,25 @@ 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 |
-
"""ฟังก์ชันส่งคำสั่งไปที่ API ของ Omni 2.0"""
|
| 11 |
try:
|
| 12 |
client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
|
| 13 |
|
| 14 |
if mode == "/multi_edit":
|
| 15 |
-
#
|
| 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
|
| 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 |
|
|
@@ -30,40 +28,41 @@ def remote_bridge(inputs, prompt, mode):
|
|
| 30 |
except Exception as e:
|
| 31 |
return None, f"❌ API Error: {str(e)}"
|
| 32 |
|
| 33 |
-
# สร้าง UI ด
|
| 34 |
-
with gr.Blocks(
|
| 35 |
-
gr.
|
| 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(
|
| 43 |
-
s_in = gr.Image(type="filepath", label="Input Image"
|
| 44 |
-
s_prompt = gr.Textbox(label="
|
| 45 |
-
s_run = gr.Button("
|
| 46 |
-
with gr.Column(
|
| 47 |
-
s_out = gr.Image(label="
|
| 48 |
-
s_status = gr.Textbox(label="
|
| 49 |
-
s_use = gr.Button("🔄 Use as
|
|
|
|
| 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 (
|
| 54 |
-
with gr.TabItem("🖼️
|
| 55 |
-
gr.Markdown("### 🚀 Multi-Modal Fusion
|
| 56 |
with gr.Row():
|
| 57 |
-
with gr.Column(scale=
|
| 58 |
-
with gr.Row():
|
| 59 |
-
m_in1 = gr.Image(type="filepath", label="Base
|
| 60 |
-
m_in2 = gr.Image(type="filepath", label="Ref
|
| 61 |
-
m_in3 = gr.Image(type="filepath", label="Style
|
| 62 |
-
m_prompt = gr.Textbox(label="Fusion
|
| 63 |
-
m_run = gr.Button("
|
| 64 |
-
with gr.Column(scale=
|
| 65 |
-
m_out = gr.Image(label="Fused Result"
|
| 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],
|
|
@@ -73,32 +72,27 @@ with gr.Blocks(css=".container { max-width: 1200px; margin: auto; }") as demo:
|
|
| 73 |
# --- TAB 3: Text to Image ---
|
| 74 |
with gr.TabItem("✨ Text to Image"):
|
| 75 |
with gr.Row():
|
| 76 |
-
with gr.Column(
|
| 77 |
-
t_prompt = gr.Textbox(label="Prompt",
|
| 78 |
-
t_run = gr.Button("
|
| 79 |
-
with gr.Column(
|
| 80 |
-
t_out = gr.Image(label="Generated Result"
|
| 81 |
-
|
| 82 |
-
t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, t_status])
|
| 83 |
|
| 84 |
-
# --- TAB 4: Tools
|
| 85 |
with gr.TabItem("🔍 Tools"):
|
|
|
|
| 86 |
with gr.Row():
|
| 87 |
-
with gr.Column(
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 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,
|
| 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 |
-
|
| 104 |
-
demo.queue().launch(server_name="0.0.0.0")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
+
# Configuration - ตรวจสอบว่ามี HF_TOKEN ใน Settings แล้ว
|
| 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 |
|
| 13 |
if mode == "/multi_edit":
|
| 14 |
+
# รวมรูปจาก 3 ช่อง (Base, Ref, Style)
|
| 15 |
valid_files = [handle_file(img) for img in inputs if img is not None]
|
| 16 |
if not valid_files:
|
| 17 |
+
return None, "Status: Please upload images."
|
| 18 |
result = client.predict(images=valid_files, prompt=prompt, api_name="/multi_edit")
|
| 19 |
|
| 20 |
elif mode == "/t2i":
|
| 21 |
result = client.predict(prompt=prompt, api_name="/t2i")
|
| 22 |
|
| 23 |
else:
|
|
|
|
| 24 |
if inputs is None: return None, "Status: No image provided."
|
| 25 |
result = client.predict(image=handle_file(inputs), prompt=prompt, api_name=mode)
|
| 26 |
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
return None, f"❌ API Error: {str(e)}"
|
| 30 |
|
| 31 |
+
# สร้าง UI แบบตัดพารามิเตอร์ที่ทำให้พังออกทั้งหมด
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("# 🎨 Omni Editor 2.0 (Remote)")
|
|
|
|
| 34 |
|
| 35 |
with gr.Tabs():
|
| 36 |
# --- TAB 1: Single Image Edit ---
|
| 37 |
with gr.TabItem("🖼️ Single Image Edit"):
|
| 38 |
with gr.Row():
|
| 39 |
+
with gr.Column():
|
| 40 |
+
s_in = gr.Image(type="filepath", label="Input Image")
|
| 41 |
+
s_prompt = gr.Textbox(label="Prompt", lines=2)
|
| 42 |
+
s_run = gr.Button("Execute Edit", variant="primary")
|
| 43 |
+
with gr.Column():
|
| 44 |
+
s_out = gr.Image(label="Result")
|
| 45 |
+
s_status = gr.Textbox(label="Status", interactive=False)
|
| 46 |
+
s_use = gr.Button("🔄 Use Result as Input")
|
| 47 |
+
|
| 48 |
s_run.click(lambda i, p: remote_bridge(i, p, "/predict"), [s_in, s_prompt], [s_out, s_status])
|
| 49 |
s_use.click(fn=lambda x: x, inputs=s_out, outputs=s_in)
|
| 50 |
|
| 51 |
+
# --- TAB 2: Multi-Image Edit (3 ช่องแยกตามสเปก 8B MM-DiT) ---
|
| 52 |
+
with gr.TabItem("🖼️ Multi-Image Edit"):
|
| 53 |
+
gr.Markdown("### 🚀 Multi-Modal Fusion")
|
| 54 |
with gr.Row():
|
| 55 |
+
with gr.Column(scale=2):
|
| 56 |
+
with gr.Row():
|
| 57 |
+
m_in1 = gr.Image(type="filepath", label="Base (A)")
|
| 58 |
+
m_in2 = gr.Image(type="filepath", label="Ref (B)")
|
| 59 |
+
m_in3 = gr.Image(type="filepath", label="Style (C)")
|
| 60 |
+
m_prompt = gr.Textbox(label="Fusion Instruction", lines=2)
|
| 61 |
+
m_run = gr.Button("Execute Multi-Fusion", variant="primary")
|
| 62 |
+
with gr.Column(scale=1):
|
| 63 |
+
m_out = gr.Image(label="Fused Result")
|
| 64 |
m_status = gr.Textbox(label="Status", interactive=False)
|
| 65 |
+
|
| 66 |
m_run.click(
|
| 67 |
fn=lambda i1, i2, i3, p: remote_bridge([i1, i2, i3], p, "/multi_edit"),
|
| 68 |
inputs=[m_in1, m_in2, m_in3, m_prompt],
|
|
|
|
| 72 |
# --- TAB 3: Text to Image ---
|
| 73 |
with gr.TabItem("✨ Text to Image"):
|
| 74 |
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
t_prompt = gr.Textbox(label="Prompt", lines=4)
|
| 77 |
+
t_run = gr.Button("Generate", variant="primary")
|
| 78 |
+
with gr.Column():
|
| 79 |
+
t_out = gr.Image(label="Generated Result")
|
| 80 |
+
t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, s_status])
|
|
|
|
| 81 |
|
| 82 |
+
# --- TAB 4: Tools ---
|
| 83 |
with gr.TabItem("🔍 Tools"):
|
| 84 |
+
tool_mode = gr.Radio(["Upscale", "Remove Watermark"], label="Select Tool", value="Upscale")
|
| 85 |
with gr.Row():
|
| 86 |
+
with gr.Column():
|
| 87 |
+
tool_in = gr.Image(type="filepath", label="Input")
|
| 88 |
+
tool_run = gr.Button("Process", variant="primary")
|
| 89 |
+
with gr.Column():
|
|
|
|
| 90 |
tool_out = gr.Image(label="Result")
|
|
|
|
| 91 |
|
| 92 |
def tool_router(mode, img):
|
| 93 |
api_path = "/upscale" if mode == "Upscale" else "/remove_watermark"
|
| 94 |
return remote_bridge(img, "", api_path)
|
| 95 |
|
| 96 |
+
tool_run.click(tool_router, [tool_mode, tool_in], [tool_out, s_status])
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
demo.queue().launch()
|
|
|