Saravutw commited on
Commit
230a85a
·
verified ·
1 Parent(s): 03c151a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -99
app.py CHANGED
@@ -1,106 +1,44 @@
1
- import torch
2
- import gradio as gr
3
  import os
4
- from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
5
-
6
- # ใช้โมเดลเดิม
7
- MODEL_ID = "Lykon/dreamshaper-xl-turbo"
8
-
9
- print("Loading for Pure CPU Environment (No GPU)...")
10
-
11
- # โหลดโมเดลแบบ CPU
12
- pipe = AutoPipelineForText2Image.from_pretrained(
13
- MODEL_ID,
14
- torch_dtype=torch.float32,
15
- low_cpu_mem_usage=True,
16
- safety_checker=None,
17
- requires_safety_checker=False
18
- )
19
- pipe.to("cpu")
20
-
21
- # [SAFE CPU OPTIMIZATIONS]
22
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
23
- pipe.enable_attention_slicing("max")
24
- pipe.enable_vae_tiling()
25
- torch.set_num_threads(os.cpu_count())
26
-
27
- # พจนานุกรมสำหรับ Style
28
- STYLE_TEMPLATES = {
29
- "None": "{prompt}",
30
- "Cinematic": "cinematic photo, {prompt}, high resolution, detailed, 8k, masterpiece",
31
- "Digital Art": "digital art, {prompt}, vibrant colors, detailed, trending on artstation",
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
- prompt = gr.Textbox(label="Prompt", lines=3, placeholder="What do you want to create?")
75
-
76
- with gr.Row():
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
- output_img = gr.Image(label="Result")
98
 
99
- btn.click(
100
- fn=gen,
101
- inputs=[prompt, negative, style_dropdown, ratio_dropdown, steps, cfg],
102
- outputs=[output_img]
103
  )
104
 
105
- if __name__ == "__main__":
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()