Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,44 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import gradio as gr
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
"Anime": "anime style, {prompt}, vivid, expressive, high quality art",
|
| 33 |
-
"Oil Painting": "oil painting, {prompt}, brush strokes, classical style, textured canvas",
|
| 34 |
-
"Photography": "professional photography, {prompt}, realistic, 8k uhd, dslr, high detail"
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
# ฟังก์ชันคำนวณสัดส่วนภาพ
|
| 38 |
-
def get_dimensions(ratio_str):
|
| 39 |
-
ratios = {
|
| 40 |
-
"1:1 (Square)": (512, 512),
|
| 41 |
-
"4:3 (Classic)": (512, 384),
|
| 42 |
-
"3:4 (Portrait)": (384, 512),
|
| 43 |
-
"16:9 (Widescreen)": (512, 288),
|
| 44 |
-
"9:16 (Vertical)": (288, 512)
|
| 45 |
-
}
|
| 46 |
-
return ratios.get(ratio_str, (512, 512))
|
| 47 |
-
|
| 48 |
-
def gen(prompt, negative_prompt, style, aspect_ratio, steps, cfg):
|
| 49 |
-
if not prompt: return None
|
| 50 |
-
|
| 51 |
-
# รวม Prompt กับ Style
|
| 52 |
-
final_prompt = STYLE_TEMPLATES[style].format(prompt=prompt)
|
| 53 |
-
|
| 54 |
-
# กำหนดขนาดภาพจาก Aspect Ratio
|
| 55 |
-
width, height = get_dimensions(aspect_ratio)
|
| 56 |
-
|
| 57 |
-
with torch.no_grad():
|
| 58 |
-
image = pipe(
|
| 59 |
-
prompt=final_prompt,
|
| 60 |
-
negative_prompt=negative_prompt,
|
| 61 |
-
num_inference_steps=int(steps),
|
| 62 |
-
guidance_scale=float(cfg),
|
| 63 |
-
width=width,
|
| 64 |
-
height=height
|
| 65 |
-
).images[0]
|
| 66 |
-
return image
|
| 67 |
-
|
| 68 |
-
# UI
|
| 69 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
-
gr.Markdown("### 🚀 Pure CPU Turbo (Style & Aspect Ratio Support)")
|
| 71 |
-
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column():
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
style_dropdown = gr.Dropdown(
|
| 78 |
-
choices=list(STYLE_TEMPLATES.keys()),
|
| 79 |
-
value="None",
|
| 80 |
-
label="Select Style"
|
| 81 |
-
)
|
| 82 |
-
ratio_dropdown = gr.Dropdown(
|
| 83 |
-
choices=["1:1 (Square)", "4:3 (Classic)", "3:4 (Portrait)", "16:9 (Widescreen)", "9:16 (Vertical)"],
|
| 84 |
-
value="1:1 (Square)",
|
| 85 |
-
label="Aspect Ratio"
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
negative = gr.Textbox(label="Negative", value="deformed, lowres, blurry, nsfw")
|
| 89 |
-
|
| 90 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 91 |
-
steps = gr.Slider(1, 10, 4, step=1, label="Steps (Turbo recommended 1-4)")
|
| 92 |
-
cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG Scale")
|
| 93 |
-
|
| 94 |
-
btn = gr.Button("Generate", variant="primary")
|
| 95 |
-
|
| 96 |
with gr.Column():
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
fn=
|
| 101 |
-
inputs=[
|
| 102 |
-
outputs=
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
| 106 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from gradio_client import Client, handle_file
|
| 4 |
+
|
| 5 |
+
# ดึง Token จาก Secrets ของ Space (ตั้งชื่อว่า HF_TOKEN) เพื่อสิทธิพิเศษในการเรียก API
|
| 6 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
+
|
| 8 |
+
# เชื่อมต่อไปยัง Space ต้นทางผ่าน API
|
| 9 |
+
# การใช้ Client แบบนี้จะไม่กินทรัพยากรเครื่องของคุณ
|
| 10 |
+
client = Client("selfit-camera/omni-image-editor", hf_token=hf_token)
|
| 11 |
+
|
| 12 |
+
def api_bridge(input_img, prompt):
|
| 13 |
+
if input_img is None:
|
| 14 |
+
return None
|
| 15 |
+
try:
|
| 16 |
+
# ส่งข้อมูลไปยัง API ต้นทาง
|
| 17 |
+
# หมายเหตุ: ตรวจสอบ api_name จาก client.view_api() อีกครั้งหากต้นทางอัปเดต
|
| 18 |
+
result = client.predict(
|
| 19 |
+
image=handle_file(input_img),
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
api_name="/predict"
|
| 22 |
+
)
|
| 23 |
+
return result
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {str(e)}"
|
| 26 |
+
|
| 27 |
+
# สร้างหน้า Interface ของคุณเอง
|
| 28 |
+
with gr.Blocks(title="Omni Editor API Bridge") as demo:
|
| 29 |
+
gr.Markdown("### Omni Image Editor (via API Bridge)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Row():
|
| 31 |
with gr.Column():
|
| 32 |
+
img_input = gr.Image(type="filepath", label="Upload Image")
|
| 33 |
+
prompt_input = gr.Textbox(label="Edit Prompt", placeholder="เช่น 'make it look like a sketch'")
|
| 34 |
+
submit_btn = gr.Button("Send to API", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with gr.Column():
|
| 36 |
+
img_output = gr.Image(label="Result")
|
| 37 |
|
| 38 |
+
submit_btn.click(
|
| 39 |
+
fn=api_bridge,
|
| 40 |
+
inputs=[img_input, prompt_input],
|
| 41 |
+
outputs=img_output
|
| 42 |
)
|
| 43 |
|
| 44 |
+
demo.launch()
|
|
|