Saravutw commited on
Commit
360b978
·
verified ·
1 Parent(s): e070156

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
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
- # --- 2. Load Model ---
9
- device = "cuda"
10
  model_id = "stablediffusionapi/pony-diffusion-v6-xl"
11
- if 'pipe' not in locals():
12
- pipe = StableDiffusionXLPipeline.from_pretrained(
13
- model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True
14
- ).to(device)
15
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
16
- pipe.enable_vae_tiling()
17
- pipe.enable_vae_slicing()
18
- # --- 3. Function เจนภาพ (เพิ่มการจัดการนามสกุลไฟล์) ---
 
 
 
 
 
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
- selected_style = style_map[style_preset]
32
- if "score_9" in prompt:
33
- full_prompt = f"{selected_style}{prompt}"
34
- else:
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
- width, height = size_map[size_preset]
 
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
- # --- 4. UI Layout ---
 
65
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
- gr.Markdown("# 🚀 Pony V6 XL: Ultimate Realistic Engine")
 
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 young slutty asian cute girl, 2men,double-penetration. gangbang, group sex.sex on top potition.cum in pussy.creampie.deepthroat.eating sperm,public sex,bikini.nipple piercinglook pain mix happy,", lines=4)
71
- n_in = gr.Textbox(label="Negative", value="anime,,low quality, worst quality, motion artifacts, unstable motion, jitter, frame jitter, wobbling limbs, motion distortion, inconsistent movement, robotic movement, animation-like motion, awkward transitions, incorrect body mechanics, unnatural posing, off-balance poses, broken motion paths, frozen frames, duplicated frames, frame skipping, warped motion, stretching artifacts bad anatomy, incorrect proportions, deformed body, twisted torso, broken joints, dislocated limbs, distorted neck, unnatural spine curvature, malformed hands, extra fingers, missing fingers, fused fingers, distorted legs, extra limbs, collapsed feet, floating feet, foot sliding, foot jitter, backward walking, unnatural gait blurry details, long exposure blur, ghosting, shadow trails, smearing, washed-out colors, overexposure, underexposure, excessive contrast, blown highlights, poorly rendered clothing, fabric glitches, texture warping, clothing merging with body, incorrect cloth physics ugly background, cluttered scene, crowded background, random objects, unwanted text, subtitles, logos, graffiti, grain, noise, static artifacts, compression noise, jpeg artifacts, image-like stillness, painting-like look, cartoon texture, low-resolution textures manga, Disney, cartoon, low resolution, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name, drawing, illustration, boring, 3d render, long neck, out of frame, extra fingers mutation, deformed, canvas frame, high contrast:1.2, over saturated:1.2, glossy:1.1, bad lighting, poorly drawn, bad art, bad angle, low-resolution, bad composition, terrible lighting, score_4, score_5, score_6, (anime, cartoon, manga, illustration, 3d, render:1.3), (extra limbs:1.2), (extra legs:1.2), (extra arms:1.2), low quality, blurry, censorship, mosaic", lines=2)
72
- with gr.Row():
73
- style_drop = gr.Dropdown(list(["None (ค่าดั้งเดิม)", "Realistic Photo (ดิบๆ)", "Cinematic Real (แสงสวย)", "3D Realistic (เนียนกริบ)", "Semi-Real (นวลตา)", "Portrait Real (เน้นหน้า)", "สไตล์บ้านๆ (Homemade)", "แนวบิวตี้ (ฟรุ้งฟริ้ง)"]), value="Realistic Photo (ดิบๆ)", label="เลือกสไตล์ภาพ")
74
- # ตั้งขนาดเริ่มต้นเป็น 1024x1024 ตมสั่ง
75
- size_drop = gr.Dropdown(["Square (512x512)", "Square (1024x1024)", "Portrait (832x1216)", "Landscape (1216x832)"], value="Square (1024x1024)", label="นาดภาพ")
76
- with gr.Row():
77
- # เพิ่มช่เลือกนามสกุลไฟล์
78
- format_drop = gr.Dropdown(["PNG", "JPG", "WebP"], value="PNG", label="นามสุลไฟล์")
79
- c_sld = gr.Slider(1, 15, value=3.0, step=0.5, label="ความคมชัดคำสั่ง (CFG)")
80
- with gr.Row():
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)