Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if mode == "/multi_edit":
|
| 13 |
-
# รวมรูปจาก 3 ช่อง (Base, Ref, Style)
|
| 14 |
-
valid_files = [handle_file(img) for img in inputs if img is not None]
|
| 15 |
-
if not valid_files:
|
| 16 |
-
return None, "Status: Please upload images."
|
| 17 |
-
result = client.predict(images=valid_files, prompt=prompt, api_name="/multi_edit")
|
| 18 |
-
|
| 19 |
-
elif mode == "/t2i":
|
| 20 |
-
result = client.predict(prompt=prompt, api_name="/t2i")
|
| 21 |
-
|
| 22 |
-
else:
|
| 23 |
-
if inputs is None: return None, "Status: No image provided."
|
| 24 |
-
result = client.predict(image=handle_file(inputs), prompt=prompt, api_name=mode)
|
| 25 |
-
|
| 26 |
-
return result, f"✅ Success: {mode}"
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return None, f"❌ API Error: {str(e)}"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# --- TAB 1: Single Image Edit ---
|
| 36 |
-
with gr.TabItem("🖼️ Single Image Edit"):
|
| 37 |
-
with gr.Row():
|
| 38 |
-
with gr.Column():
|
| 39 |
-
s_in = gr.Image(type="filepath", label="Input Image")
|
| 40 |
-
s_prompt = gr.Textbox(label="Prompt", lines=2)
|
| 41 |
-
s_run = gr.Button("Execute Edit", variant="primary")
|
| 42 |
-
with gr.Column():
|
| 43 |
-
s_out = gr.Image(label="Result")
|
| 44 |
-
s_status = gr.Textbox(label="Status", interactive=False)
|
| 45 |
-
s_use = gr.Button("🔄 Use Result as Input")
|
| 46 |
-
|
| 47 |
-
s_run.click(lambda i, p: remote_bridge(i, p, "/predict"), [s_in, s_prompt], [s_out, s_status])
|
| 48 |
-
s_use.click(fn=lambda x: x, inputs=s_out, outputs=s_in)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Markdown("### 🚀 Multi-Modal Fusion")
|
| 53 |
-
with gr.Row():
|
| 54 |
-
with gr.Column(scale=2):
|
| 55 |
-
with gr.Row():
|
| 56 |
-
m_in1 = gr.Image(type="filepath", label="Base (A)")
|
| 57 |
-
m_in2 = gr.Image(type="filepath", label="Ref (B)")
|
| 58 |
-
m_in3 = gr.Image(type="filepath", label="Style (C)")
|
| 59 |
-
m_prompt = gr.Textbox(label="Fusion Instruction", lines=2)
|
| 60 |
-
m_run = gr.Button("Execute Multi-Fusion", variant="primary")
|
| 61 |
-
with gr.Column(scale=1):
|
| 62 |
-
m_out = gr.Image(label="Fused Result")
|
| 63 |
-
m_status = gr.Textbox(label="Status", interactive=False)
|
| 64 |
-
|
| 65 |
-
m_run.click(
|
| 66 |
-
fn=lambda i1, i2, i3, p: remote_bridge([i1, i2, i3], p, "/multi_edit"),
|
| 67 |
-
inputs=[m_in1, m_in2, m_in3, m_prompt],
|
| 68 |
-
outputs=[m_out, m_status]
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
with gr.Column():
|
| 75 |
-
t_prompt = gr.Textbox(label="Prompt", lines=4)
|
| 76 |
-
t_run = gr.Button("Generate", variant="primary")
|
| 77 |
-
with gr.Column():
|
| 78 |
-
t_out = gr.Image(label="Generated Result")
|
| 79 |
-
t_run.click(lambda p: remote_bridge(None, p, "/t2i"), [t_prompt], [t_out, s_status])
|
| 80 |
-
|
| 81 |
-
# --- TAB 4: Tools ---
|
| 82 |
-
with gr.TabItem("🔍 Tools"):
|
| 83 |
-
tool_mode = gr.Radio(["Upscale", "Remove Watermark"], label="Select Tool", value="Upscale")
|
| 84 |
-
with gr.Row():
|
| 85 |
-
with gr.Column():
|
| 86 |
-
tool_in = gr.Image(type="filepath", label="Input")
|
| 87 |
-
tool_run = gr.Button("Process", variant="primary")
|
| 88 |
-
with gr.Column():
|
| 89 |
-
tool_out = gr.Image(label="Result")
|
| 90 |
-
|
| 91 |
-
def tool_router(mode, img):
|
| 92 |
-
api_path = "/upscale" if mode == "Upscale" else "/remove_watermark"
|
| 93 |
-
return remote_bridge(img, "", api_path)
|
| 94 |
-
|
| 95 |
-
tool_run.click(tool_router, [tool_mode, tool_in], [tool_out, s_status])
|
| 96 |
-
|
| 97 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
from huggingface_hub import login
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Login Hugging Face (ใสj Token ของคุณเมื่อรันโค้ดนี้)
|
| 5 |
+
# จำเป็นต้องใช้เพราะ Space อาจมีการดึงโมเดลที่ต้องยืนยันตัวตน
|
| 6 |
+
print("=== กรุณาใส่ HF Token ===")
|
| 7 |
+
login()
|
| 8 |
|
| 9 |
+
# 2. Clone Repository จาก Space
|
| 10 |
+
# ใช้ git lfs เพื่อดึงไฟล์โมเดลขนาดใหญ่ที่ติดมากับ Repo (ถ้ามี)
|
| 11 |
+
!git lfs install
|
| 12 |
+
!git clone https://huggingface.co/spaces/selfit-camera/omni-image-editor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# 3. เข้าไปในโฟลเดอร์และติดตั้ง Dependencies
|
| 15 |
+
%cd omni-image-editor
|
| 16 |
+
print("=== กำลังติดตั้ง Library... อาจใช้เวลาสักพัก ===")
|
| 17 |
+
!pip install -r requirements.txt
|
| 18 |
+
!pip install gradio --upgrade
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# 4. ตั้งค่า Environment Variable เพื่อให้ Gradio สร้าง Public Link (Share=True)
|
| 21 |
+
os.environ['GRADIO_SHARE'] = 'True'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# 5. รันแอปพลิเคชัน
|
| 24 |
+
print("=== กำลังเริ่มระบบ... กรุณารอลิงก์ Public URL ===")
|
| 25 |
+
!python app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|