Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
from io import BytesIO
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# =========================
|
| 9 |
-
#
|
| 10 |
# =========================
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
HF_MODEL = os.environ.get("HF_MODEL", "playgroundai/playground-v2.5")
|
| 16 |
|
| 17 |
-
API_URL = "https://api-inference.huggingface.co/models/stabilityai/sdxl-turbo"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# =========================
|
|
@@ -28,7 +59,7 @@ def auto_prompt(category: str) -> str:
|
|
| 28 |
templates = {
|
| 29 |
"Skincare": "Serum skincare botol kaca premium, lighting studio, aesthetic clean look",
|
| 30 |
"Makanan/Minuman": "Minuman segar dengan efek splash, lighting vibrant, cocok untuk iklan",
|
| 31 |
-
"Fashion": "
|
| 32 |
"Elektronik": "Headphone wireless premium, lighting studio, tampilan high-end",
|
| 33 |
"Umum": "Produk premium dengan lighting studio dan background bersih",
|
| 34 |
}
|
|
@@ -62,41 +93,40 @@ def build_prompt(prompt: str, style: str, category: str, with_model: bool) -> st
|
|
| 62 |
style_map.get(style, ""),
|
| 63 |
category_map.get(category, ""),
|
| 64 |
model_snippet,
|
|
|
|
| 65 |
]
|
| 66 |
|
| 67 |
return ", ".join([p for p in parts if p])
|
| 68 |
|
| 69 |
|
| 70 |
# =========================
|
| 71 |
-
#
|
| 72 |
# =========================
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
"Content-Type": "application/json",
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
payload = {
|
| 81 |
-
"inputs": prompt
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 85 |
-
response.raise_for_status()
|
| 86 |
-
|
| 87 |
-
img_bytes = response.content
|
| 88 |
-
img = Image.open(BytesIO(img_bytes))
|
| 89 |
-
|
| 90 |
-
return img
|
| 91 |
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
full_prompt = build_prompt(prompt, style, category, with_model)
|
| 99 |
-
img = call_huggingface(full_prompt)
|
| 100 |
return img
|
| 101 |
|
| 102 |
|
|
@@ -104,14 +134,21 @@ def run(prompt, category, style, with_model):
|
|
| 104 |
# GRADIO UI
|
| 105 |
# =========================
|
| 106 |
|
| 107 |
-
with gr.Blocks(title="RuangAI β Product Visualizer (
|
| 108 |
gr.Markdown("""
|
| 109 |
-
# π§΄ RuangAI β Product Visualizer (Level 2)
|
| 110 |
-
|
|
|
|
| 111 |
""")
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
category = gr.Dropdown(
|
| 116 |
["Umum", "Skincare", "Makanan/Minuman", "Fashion", "Elektronik"],
|
| 117 |
value="Umum",
|
|
@@ -135,17 +172,39 @@ with gr.Blocks(title="RuangAI β Product Visualizer (Level 2)") as demo:
|
|
| 135 |
lines=3,
|
| 136 |
)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
with gr.Column():
|
| 144 |
output_image = gr.Image(label="Hasil", type="pil")
|
| 145 |
|
|
|
|
|
|
|
| 146 |
generate_btn.click(
|
| 147 |
run,
|
| 148 |
-
inputs=[prompt, category, style, with_model],
|
| 149 |
outputs=[output_image],
|
| 150 |
)
|
| 151 |
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from io import BytesIO
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import StableDiffusionPipeline
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# =========================
|
| 10 |
+
# DEVICE
|
| 11 |
+
# =========================
|
| 12 |
+
|
| 13 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 15 |
+
|
| 16 |
+
print(f"[INFO] Using device: {DEVICE}, dtype: {DTYPE}")
|
| 17 |
+
|
| 18 |
|
| 19 |
# =========================
|
| 20 |
+
# MODEL CONFIG
|
| 21 |
# =========================
|
| 22 |
|
| 23 |
+
MODEL_OPTIONS = {
|
| 24 |
+
"Realistic Vision v5.1": "SG161222/Realistic_Vision_V5.1_noVAE",
|
| 25 |
+
"Stable Diffusion 1.5": "runwayml/stable-diffusion-v1-5",
|
| 26 |
+
"DreamShaper 8": "Lykon/dreamshaper-8",
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
PIPELINES = {}
|
|
|
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
+
def get_pipeline(model_name: str) -> StableDiffusionPipeline:
|
| 33 |
+
if model_name in PIPELINES:
|
| 34 |
+
return PIPELINES[model_name]
|
| 35 |
+
|
| 36 |
+
repo_id = MODEL_OPTIONS[model_name]
|
| 37 |
+
print(f"[INFO] Loading model: {model_name} ({repo_id})")
|
| 38 |
+
|
| 39 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 40 |
+
repo_id,
|
| 41 |
+
torch_dtype=DTYPE,
|
| 42 |
+
safety_checker=None,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
pipe = pipe.to(DEVICE)
|
| 46 |
+
|
| 47 |
+
if DEVICE == "cuda":
|
| 48 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 49 |
+
|
| 50 |
+
PIPELINES[model_name] = pipe
|
| 51 |
+
return pipe
|
| 52 |
|
| 53 |
|
| 54 |
# =========================
|
|
|
|
| 59 |
templates = {
|
| 60 |
"Skincare": "Serum skincare botol kaca premium, lighting studio, aesthetic clean look",
|
| 61 |
"Makanan/Minuman": "Minuman segar dengan efek splash, lighting vibrant, cocok untuk iklan",
|
| 62 |
+
"Fashion": "Pakaian atau sepatu fashion modern, lighting studio, katalog e-commerce",
|
| 63 |
"Elektronik": "Headphone wireless premium, lighting studio, tampilan high-end",
|
| 64 |
"Umum": "Produk premium dengan lighting studio dan background bersih",
|
| 65 |
}
|
|
|
|
| 93 |
style_map.get(style, ""),
|
| 94 |
category_map.get(category, ""),
|
| 95 |
model_snippet,
|
| 96 |
+
"high quality, 4k, detailed",
|
| 97 |
]
|
| 98 |
|
| 99 |
return ", ".join([p for p in parts if p])
|
| 100 |
|
| 101 |
|
| 102 |
# =========================
|
| 103 |
+
# GENERATION
|
| 104 |
# =========================
|
| 105 |
|
| 106 |
+
def run(prompt, category, style, with_model, model_choice, steps, guidance, seed):
|
| 107 |
+
if not prompt or prompt.strip() == "":
|
| 108 |
+
prompt = auto_prompt(category)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
full_prompt = build_prompt(prompt, style, category, with_model)
|
| 111 |
|
| 112 |
+
pipe = get_pipeline(model_choice)
|
| 113 |
+
|
| 114 |
+
generator = None
|
| 115 |
+
if seed is not None and seed != "":
|
| 116 |
+
try:
|
| 117 |
+
seed_int = int(seed)
|
| 118 |
+
generator = torch.Generator(device=DEVICE).manual_seed(seed_int)
|
| 119 |
+
except ValueError:
|
| 120 |
+
generator = None
|
| 121 |
+
|
| 122 |
+
result = pipe(
|
| 123 |
+
full_prompt,
|
| 124 |
+
num_inference_steps=int(steps),
|
| 125 |
+
guidance_scale=float(guidance),
|
| 126 |
+
generator=generator,
|
| 127 |
+
)
|
| 128 |
|
| 129 |
+
img: Image.Image = result.images[0]
|
|
|
|
|
|
|
| 130 |
return img
|
| 131 |
|
| 132 |
|
|
|
|
| 134 |
# GRADIO UI
|
| 135 |
# =========================
|
| 136 |
|
| 137 |
+
with gr.Blocks(title="RuangAI β Product Visualizer (Diffusers)") as demo:
|
| 138 |
gr.Markdown("""
|
| 139 |
+
# π§΄ RuangAI β Product Visualizer (Level 2 β Diffusers Lokal)
|
| 140 |
+
Tiga model lokal: Realistic Vision v5.1, Stable Diffusion 1.5, DreamShaper 8
|
| 141 |
+
**Catatan:** di CPU akan agak lambat, sabar sebentar saat generate π
|
| 142 |
""")
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column():
|
| 146 |
+
model_choice = gr.Dropdown(
|
| 147 |
+
list(MODEL_OPTIONS.keys()),
|
| 148 |
+
value="Realistic Vision v5.1",
|
| 149 |
+
label="Pilih Model",
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
category = gr.Dropdown(
|
| 153 |
["Umum", "Skincare", "Makanan/Minuman", "Fashion", "Elektronik"],
|
| 154 |
value="Umum",
|
|
|
|
| 172 |
lines=3,
|
| 173 |
)
|
| 174 |
|
| 175 |
+
with gr.Row():
|
| 176 |
+
auto_btn = gr.Button("Auto Prompt β¨")
|
| 177 |
+
generate_btn = gr.Button("Generate π")
|
| 178 |
+
|
| 179 |
+
steps = gr.Slider(
|
| 180 |
+
minimum=10,
|
| 181 |
+
maximum=40,
|
| 182 |
+
value=25,
|
| 183 |
+
step=1,
|
| 184 |
+
label="Inference Steps",
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
guidance = gr.Slider(
|
| 188 |
+
minimum=3.0,
|
| 189 |
+
maximum=12.0,
|
| 190 |
+
value=7.5,
|
| 191 |
+
step=0.5,
|
| 192 |
+
label="Guidance Scale",
|
| 193 |
+
)
|
| 194 |
|
| 195 |
+
seed = gr.Textbox(
|
| 196 |
+
label="Seed (opsional, untuk hasil konsisten)",
|
| 197 |
+
placeholder="Kosongkan untuk random",
|
| 198 |
+
)
|
| 199 |
|
| 200 |
with gr.Column():
|
| 201 |
output_image = gr.Image(label="Hasil", type="pil")
|
| 202 |
|
| 203 |
+
auto_btn.click(auto_prompt, inputs=[category], outputs=[prompt])
|
| 204 |
+
|
| 205 |
generate_btn.click(
|
| 206 |
run,
|
| 207 |
+
inputs=[prompt, category, style, with_model, model_choice, steps, guidance, seed],
|
| 208 |
outputs=[output_image],
|
| 209 |
)
|
| 210 |
|