Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,29 @@
|
|
| 1 |
import torch, random, gc, numpy as np
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 4 |
-
from huggingface_hub import login
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# ---
|
| 9 |
-
device = "cuda"
|
| 10 |
model_id = "stablediffusionapi/pony-diffusion-v6-xl"
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def generate_single(prompt, neg_prompt, steps, cfg, seed, size_preset, style_preset, file_format):
|
| 20 |
torch.cuda.empty_cache()
|
|
|
|
|
|
|
| 21 |
style_map = {
|
| 22 |
"None (ค่าดั้งเดิม)": "raw photo.cute.high resolution",
|
| 23 |
"Realistic Photo (ดิบๆ)": "raw photo, (photorealistic:1.3), high fidelity, skin pores, film grain, Fujifilm, ",
|
|
@@ -28,21 +34,22 @@ def generate_single(prompt, neg_prompt, steps, cfg, seed, size_preset, style_pre
|
|
| 28 |
"สไตล์บ้านๆ (Homemade)": "amateur phone photography, (casual lighting:1.2), flash photography, messy room background, (real life:1.3), grainy, ",
|
| 29 |
"แนวบิวตี้ (ฟรุ้งฟริ้ง)": "soft focus, (ethereal lighting:1.3), dreamy atmosphere, glowing skin, (pastel tones:1.1), high key lighting, beauty filter, "
|
| 30 |
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
full_prompt = f"score_9, score_8_up, score_7_up, rating_explicit, masterpiece, best quality, {selected_style}{prompt}"
|
| 36 |
size_map = {
|
| 37 |
"Square (512x512)": (512, 512),
|
| 38 |
"Square (1024x1024)": (1024, 1024),
|
| 39 |
"Portrait (832x1216)": (720, 1260),
|
| 40 |
"Landscape (1216x832)": (1280, 720)
|
| 41 |
}
|
| 42 |
-
|
|
|
|
| 43 |
base_seed = int(seed) if int(seed) != -1 else random.randint(0, 10_000_000)
|
|
|
|
|
|
|
| 44 |
with torch.inference_mode():
|
| 45 |
-
generator = torch.manual_seed(base_seed)
|
| 46 |
image = pipe(
|
| 47 |
prompt=full_prompt,
|
| 48 |
negative_prompt=neg_prompt,
|
|
@@ -52,39 +59,36 @@ def generate_single(prompt, neg_prompt, steps, cfg, seed, size_preset, style_pre
|
|
| 52 |
width=width,
|
| 53 |
height=height
|
| 54 |
).images[0]
|
| 55 |
-
|
| 56 |
filename = f"output_{base_seed}.{file_format.lower()}"
|
| 57 |
-
if file_format == "JPG"
|
| 58 |
-
image.save(filename, quality=95, optimize=True)
|
| 59 |
-
elif file_format == "PNG":
|
| 60 |
-
image.save(filename, quality=98)
|
| 61 |
-
else: # PNG
|
| 62 |
-
image.save(random)
|
| 63 |
return image, base_seed, filename
|
| 64 |
-
|
|
|
|
| 65 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
-
gr.Markdown("#
|
|
|
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column(scale=1):
|
| 69 |
btn = gr.Button("🚀 GENERATE NOW", variant="primary")
|
| 70 |
-
p_in = gr.Textbox(label="Prompt", value="a
|
| 71 |
-
n_in = gr.Textbox(label="Negative", value="
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
s_sld = gr.Slider(2, 100, value=70, step=1, label="รอบการวาด (Steps)")
|
| 82 |
-
sd_in = gr.Number(value=-1, label="Seed")
|
| 83 |
with gr.Column(scale=1):
|
| 84 |
img_out = gr.Image(label="Result")
|
| 85 |
-
file_out = gr.File(label="Download File")
|
| 86 |
seed_out = gr.Number(label="Seed ที่ใช้")
|
|
|
|
| 87 |
btn.click(fn=generate_single,
|
| 88 |
inputs=[p_in, n_in, s_sld, c_sld, sd_in, size_drop, style_drop, format_drop],
|
| 89 |
outputs=[img_out, seed_out, file_out])
|
|
|
|
| 90 |
demo.launch(share=True)
|
|
|
|
| 1 |
import torch, random, gc, numpy as np
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# --- 1. Load Model (Free Tier HF) ---
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
model_id = "stablediffusionapi/pony-diffusion-v6-xl"
|
| 10 |
+
|
| 11 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 14 |
+
variant="fp16",
|
| 15 |
+
use_safetensors=True
|
| 16 |
+
).to(device)
|
| 17 |
+
|
| 18 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 19 |
+
pipe.enable_vae_tiling()
|
| 20 |
+
pipe.enable_vae_slicing()
|
| 21 |
+
|
| 22 |
+
# --- 2. Generate Function ---
|
| 23 |
def generate_single(prompt, neg_prompt, steps, cfg, seed, size_preset, style_preset, file_format):
|
| 24 |
torch.cuda.empty_cache()
|
| 25 |
+
gc.collect()
|
| 26 |
+
|
| 27 |
style_map = {
|
| 28 |
"None (ค่าดั้งเดิม)": "raw photo.cute.high resolution",
|
| 29 |
"Realistic Photo (ดิบๆ)": "raw photo, (photorealistic:1.3), high fidelity, skin pores, film grain, Fujifilm, ",
|
|
|
|
| 34 |
"สไตล์บ้านๆ (Homemade)": "amateur phone photography, (casual lighting:1.2), flash photography, messy room background, (real life:1.3), grainy, ",
|
| 35 |
"แนวบิวตี้ (ฟรุ้งฟริ้ง)": "soft focus, (ethereal lighting:1.3), dreamy atmosphere, glowing skin, (pastel tones:1.1), high key lighting, beauty filter, "
|
| 36 |
}
|
| 37 |
+
|
| 38 |
+
selected_style = style_map.get(style_preset, "")
|
| 39 |
+
full_prompt = f"score_9, score_8_up, masterpiece, best quality, {selected_style}{prompt}"
|
| 40 |
+
|
|
|
|
| 41 |
size_map = {
|
| 42 |
"Square (512x512)": (512, 512),
|
| 43 |
"Square (1024x1024)": (1024, 1024),
|
| 44 |
"Portrait (832x1216)": (720, 1260),
|
| 45 |
"Landscape (1216x832)": (1280, 720)
|
| 46 |
}
|
| 47 |
+
|
| 48 |
+
width, height = size_map.get(size_preset, (512, 512))
|
| 49 |
base_seed = int(seed) if int(seed) != -1 else random.randint(0, 10_000_000)
|
| 50 |
+
|
| 51 |
+
generator = torch.manual_seed(base_seed)
|
| 52 |
with torch.inference_mode():
|
|
|
|
| 53 |
image = pipe(
|
| 54 |
prompt=full_prompt,
|
| 55 |
negative_prompt=neg_prompt,
|
|
|
|
| 59 |
width=width,
|
| 60 |
height=height
|
| 61 |
).images[0]
|
| 62 |
+
|
| 63 |
filename = f"output_{base_seed}.{file_format.lower()}"
|
| 64 |
+
image.save(filename, quality=95 if file_format == "JPG" else 98)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return image, base_seed, filename
|
| 66 |
+
|
| 67 |
+
# --- 3. Gradio UI ---
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 69 |
+
gr.Markdown("## 🐴 Pony V6 XL (Free Tier HF)")
|
| 70 |
+
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=1):
|
| 73 |
btn = gr.Button("🚀 GENERATE NOW", variant="primary")
|
| 74 |
+
p_in = gr.Textbox(label="Prompt", value="a beautiful cinematic portrait of a woman", lines=4)
|
| 75 |
+
n_in = gr.Textbox(label="Negative", value="low quality, blurry, bad anatomy, watermark", lines=2)
|
| 76 |
+
|
| 77 |
+
style_drop = gr.Dropdown(list(style_map.keys()), value="Realistic Photo (ดิบๆ)", label="เลือกสไตล์ภาพ")
|
| 78 |
+
size_drop = gr.Dropdown(list(size_map.keys()), value="Square (1024x1024)", label="ขนาดภาพ")
|
| 79 |
+
format_drop = gr.Dropdown(["PNG", "JPG", "WebP"], value="PNG", label="นามสกุลไฟล์")
|
| 80 |
+
|
| 81 |
+
c_sld = gr.Slider(1, 15, value=3.0, step=0.5, label="ความคมชัดคำสั่ง (CFG)")
|
| 82 |
+
s_sld = gr.Slider(2, 100, value=30, step=1, label="รอบการวาด (Steps)")
|
| 83 |
+
sd_in = gr.Number(value=-1, label="Seed")
|
| 84 |
+
|
|
|
|
|
|
|
| 85 |
with gr.Column(scale=1):
|
| 86 |
img_out = gr.Image(label="Result")
|
| 87 |
+
file_out = gr.File(label="Download File")
|
| 88 |
seed_out = gr.Number(label="Seed ที่ใช้")
|
| 89 |
+
|
| 90 |
btn.click(fn=generate_single,
|
| 91 |
inputs=[p_in, n_in, s_sld, c_sld, sd_in, size_drop, style_drop, format_drop],
|
| 92 |
outputs=[img_out, seed_out, file_out])
|
| 93 |
+
|
| 94 |
demo.launch(share=True)
|