Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,8 @@ import base64
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import json
|
|
|
|
| 7 |
from groq import Groq
|
| 8 |
-
from huggingface_hub import model_info
|
| 9 |
from fpdf import FPDF, XPos, YPos
|
| 10 |
|
| 11 |
# ============================
|
|
@@ -25,55 +25,75 @@ client = Groq(api_key=GROQ_API_KEY)
|
|
| 25 |
# MODEL ENDPOINTS (FREE)
|
| 26 |
# ============================
|
| 27 |
MODEL_ENDPOINTS = {
|
| 28 |
-
"
|
| 29 |
"Playground v2.5": "playgroundai/playground-v2.5-1024px-aesthetic",
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
}
|
| 33 |
|
| 34 |
# ============================
|
| 35 |
-
# ENGINE GAMBAR
|
| 36 |
# ============================
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "image" in content_type:
|
| 64 |
-
filename = f"img_{uuid.uuid4().hex}.png"
|
| 65 |
-
os.makedirs("outputs", exist_ok=True)
|
| 66 |
-
filepath = os.path.join("outputs", filename)
|
| 67 |
-
|
| 68 |
-
with open(filepath, "wb") as f:
|
| 69 |
-
f.write(response.content)
|
| 70 |
-
|
| 71 |
-
return filepath
|
| 72 |
-
|
| 73 |
-
# Jika base64
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if "generated_image" in data:
|
| 78 |
b64 = data["generated_image"].split(",")[-1]
|
| 79 |
img_bytes = base64.b64decode(b64)
|
|
@@ -85,10 +105,25 @@ def hf_generate_image(model, prompt):
|
|
| 85 |
with open(filepath, "wb") as f:
|
| 86 |
f.write(img_bytes)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
|
|
|
| 92 |
return None
|
| 93 |
|
| 94 |
# ============================
|
|
@@ -137,12 +172,12 @@ def build_visual_prompt(panel_text, style):
|
|
| 137 |
"""
|
| 138 |
|
| 139 |
# ============================
|
| 140 |
-
# PDF BUILDER
|
| 141 |
# ============================
|
| 142 |
class ComicPDF(FPDF):
|
| 143 |
pass
|
| 144 |
|
| 145 |
-
def build_pdf(story, style,
|
| 146 |
pdf = ComicPDF()
|
| 147 |
pdf.set_auto_page_break(True, margin=15)
|
| 148 |
|
|
@@ -158,7 +193,9 @@ def build_pdf(story, style, model, chapters):
|
|
| 158 |
pdf.cell(0, 8, f"Panel {p_idx}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
| 159 |
|
| 160 |
visual_prompt = build_visual_prompt(panel, style)
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
|
| 163 |
if img_path:
|
| 164 |
pdf.image(img_path, x=20, w=170)
|
|
@@ -174,115 +211,40 @@ def build_pdf(story, style, model, chapters):
|
|
| 174 |
# ============================
|
| 175 |
# GRADIO UI
|
| 176 |
# ============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
with gr.Blocks() as app:
|
| 178 |
|
| 179 |
-
gr.Markdown("# π AIPromptLab 3.
|
| 180 |
-
|
| 181 |
-
with gr.
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
label="Model Gratis",
|
| 203 |
-
choices=list(MODEL_ENDPOINTS.keys()),
|
| 204 |
-
value="Playground v2.5"
|
| 205 |
-
)
|
| 206 |
-
chapters = gr.Slider(1, 10, value=1, step=1, label="Jumlah Bab")
|
| 207 |
-
btn = gr.Button("Generate Comic PDF")
|
| 208 |
-
out = gr.File()
|
| 209 |
-
|
| 210 |
-
def run_comic(story, style, model, chapters):
|
| 211 |
-
return build_pdf(story, style, MODEL_ENDPOINTS[model], chapters)
|
| 212 |
-
|
| 213 |
-
btn.click(run_comic, [story, style, model, chapters], out)
|
| 214 |
-
|
| 215 |
-
# ============================
|
| 216 |
-
# TAB 2 β IMAGE GENERATOR
|
| 217 |
-
# ============================
|
| 218 |
-
with gr.Tab("Image Generator"):
|
| 219 |
-
img_prompt = gr.Textbox(label="Prompt Gambar")
|
| 220 |
-
img_style = gr.Dropdown(
|
| 221 |
-
label="Style Visual",
|
| 222 |
-
choices=[
|
| 223 |
-
"Pastel 3D Isometric",
|
| 224 |
-
"Cute 3D Cartoon",
|
| 225 |
-
"Realistic Cinematic",
|
| 226 |
-
"Lowpoly Diorama",
|
| 227 |
-
"Claymation 3D",
|
| 228 |
-
"Toy Photography",
|
| 229 |
-
"Anime 3D Soft Light"
|
| 230 |
-
],
|
| 231 |
-
value="Pastel 3D Isometric"
|
| 232 |
-
)
|
| 233 |
-
img_model = gr.Dropdown(
|
| 234 |
-
label="Model Gratis",
|
| 235 |
-
choices=list(MODEL_ENDPOINTS.keys()),
|
| 236 |
-
value="Playground v2.5"
|
| 237 |
-
)
|
| 238 |
-
btn2 = gr.Button("Generate Image")
|
| 239 |
-
img_out = gr.Image()
|
| 240 |
-
|
| 241 |
-
def generate_image(p, s, m):
|
| 242 |
-
prompt = f"{s}. Scene: {p}"
|
| 243 |
-
return hf_generate_image(MODEL_ENDPOINTS[m], prompt)
|
| 244 |
-
|
| 245 |
-
btn2.click(generate_image, [img_prompt, img_style, img_model], img_out)
|
| 246 |
-
|
| 247 |
-
# ============================
|
| 248 |
-
# TAB 3 β VIDEO STORYBOARD
|
| 249 |
-
# ============================
|
| 250 |
-
with gr.Tab("Video Storyboard"):
|
| 251 |
-
vid_desc = gr.Textbox(label="Deskripsi Video")
|
| 252 |
-
vid_style = gr.Dropdown(
|
| 253 |
-
label="Style Visual",
|
| 254 |
-
choices=[
|
| 255 |
-
"Pastel 3D Isometric",
|
| 256 |
-
"Cute 3D Cartoon",
|
| 257 |
-
"Realistic Cinematic",
|
| 258 |
-
"Lowpoly Diorama",
|
| 259 |
-
"Claymation 3D",
|
| 260 |
-
"Toy Photography",
|
| 261 |
-
"Anime 3D Soft Light"
|
| 262 |
-
],
|
| 263 |
-
value="Pastel 3D Isometric"
|
| 264 |
-
)
|
| 265 |
-
vid_model = gr.Dropdown(
|
| 266 |
-
label="Model Gratis",
|
| 267 |
-
choices=list(MODEL_ENDPOINTS.keys()),
|
| 268 |
-
value="SD Turbo"
|
| 269 |
-
)
|
| 270 |
-
btn3 = gr.Button("Generate Storyboard Frames")
|
| 271 |
-
frame_gallery = gr.Gallery(label="Frames")
|
| 272 |
-
|
| 273 |
-
def generate_storyboard(desc, style, model):
|
| 274 |
-
prompt = f"Buatkan 8 scene storyboard. Deskripsi: {desc}"
|
| 275 |
-
scenes = ai(prompt).split("\n")
|
| 276 |
-
frames = []
|
| 277 |
-
|
| 278 |
-
for s in scenes[:8]:
|
| 279 |
-
visual = build_visual_prompt(s, style)
|
| 280 |
-
img_path = hf_generate_image(MODEL_ENDPOINTS[model], visual)
|
| 281 |
-
if img_path:
|
| 282 |
-
frames.append(img_path)
|
| 283 |
-
|
| 284 |
-
return frames
|
| 285 |
-
|
| 286 |
-
btn3.click(generate_storyboard, [vid_desc, vid_style, vid_model], frame_gallery)
|
| 287 |
|
| 288 |
app.launch(ssr_mode=False)
|
|
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
+
import time
|
| 8 |
from groq import Groq
|
|
|
|
| 9 |
from fpdf import FPDF, XPos, YPos
|
| 10 |
|
| 11 |
# ============================
|
|
|
|
| 25 |
# MODEL ENDPOINTS (FREE)
|
| 26 |
# ============================
|
| 27 |
MODEL_ENDPOINTS = {
|
| 28 |
+
"SD Turbo": "stabilityai/sd-turbo",
|
| 29 |
"Playground v2.5": "playgroundai/playground-v2.5-1024px-aesthetic",
|
| 30 |
+
"SDXL": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 31 |
+
"PixArt-XL": "PixArt-alpha/PixArt-XL-2-1024-MS"
|
| 32 |
}
|
| 33 |
|
| 34 |
# ============================
|
| 35 |
+
# ENGINE GAMBAR v3.2 (ULTRA STABLE)
|
| 36 |
# ============================
|
| 37 |
+
def hf_generate_image_ultra(prompt, log_callback):
|
| 38 |
+
|
| 39 |
+
fallback_rounds = [
|
| 40 |
+
["SD Turbo", "Playground v2.5", "SDXL", "PixArt-XL"],
|
| 41 |
+
["Playground v2.5", "SDXL", "PixArt-XL", "SD Turbo"],
|
| 42 |
+
["SDXL", "PixArt-XL", "Playground v2.5", "SD Turbo"]
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
delays = [30, 60, 120, 180, 300, 600]
|
| 46 |
+
|
| 47 |
+
attempt = 1
|
| 48 |
+
total_attempts = 12
|
| 49 |
+
|
| 50 |
+
for round_idx, models in enumerate(fallback_rounds, start=1):
|
| 51 |
+
for model_name in models:
|
| 52 |
+
|
| 53 |
+
log_callback(f"[Try {attempt}/{total_attempts}] Model: {model_name}")
|
| 54 |
+
|
| 55 |
+
model = MODEL_ENDPOINTS[model_name]
|
| 56 |
+
url = f"https://api-inference.huggingface.co/models/{model}"
|
| 57 |
+
|
| 58 |
+
payload = {
|
| 59 |
+
"inputs": prompt,
|
| 60 |
+
"options": {"wait_for_model": True}
|
| 61 |
+
}
|
| 62 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
+
response = requests.post(url, headers=HEADERS, json=payload)
|
| 65 |
+
|
| 66 |
+
# Cek JSON error
|
| 67 |
+
try:
|
| 68 |
+
data = response.json()
|
| 69 |
+
if "error" in data:
|
| 70 |
+
log_callback(f" β HF Error: {data['error']}")
|
| 71 |
+
time.sleep(delays[min(attempt-1, len(delays)-1)])
|
| 72 |
+
attempt += 1
|
| 73 |
+
continue
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
+
# Cek MIME type
|
| 78 |
+
content_type = response.headers.get("Content-Type", "")
|
| 79 |
+
|
| 80 |
+
if "image" in content_type:
|
| 81 |
+
filename = f"img_{uuid.uuid4().hex}.png"
|
| 82 |
+
os.makedirs("outputs", exist_ok=True)
|
| 83 |
+
filepath = os.path.join("outputs", filename)
|
| 84 |
+
|
| 85 |
+
with open(filepath, "wb") as f:
|
| 86 |
+
f.write(response.content)
|
| 87 |
+
|
| 88 |
+
if os.path.getsize(filepath) > 10000:
|
| 89 |
+
log_callback(" β SUCCESS (image bytes)")
|
| 90 |
+
return filepath
|
| 91 |
+
else:
|
| 92 |
+
log_callback(" β FAILED (image too small)")
|
| 93 |
+
|
| 94 |
+
# Cek base64
|
| 95 |
+
try:
|
| 96 |
+
data = response.json()
|
| 97 |
if "generated_image" in data:
|
| 98 |
b64 = data["generated_image"].split(",")[-1]
|
| 99 |
img_bytes = base64.b64decode(b64)
|
|
|
|
| 105 |
with open(filepath, "wb") as f:
|
| 106 |
f.write(img_bytes)
|
| 107 |
|
| 108 |
+
if os.path.getsize(filepath) > 10000:
|
| 109 |
+
log_callback(" β SUCCESS (base64)")
|
| 110 |
+
return filepath
|
| 111 |
+
else:
|
| 112 |
+
log_callback(" β FAILED (base64 too small)")
|
| 113 |
+
except:
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
log_callback(f" β Exception: {str(e)}")
|
| 118 |
+
|
| 119 |
+
# Delay adaptif
|
| 120 |
+
delay = delays[min(attempt-1, len(delays)-1)]
|
| 121 |
+
log_callback(f" β Waiting {delay} seconds before retry...")
|
| 122 |
+
time.sleep(delay)
|
| 123 |
+
|
| 124 |
+
attempt += 1
|
| 125 |
|
| 126 |
+
log_callback("β Gagal menghasilkan gambar setelah 12 percobaan.")
|
| 127 |
return None
|
| 128 |
|
| 129 |
# ============================
|
|
|
|
| 172 |
"""
|
| 173 |
|
| 174 |
# ============================
|
| 175 |
+
# PDF BUILDER
|
| 176 |
# ============================
|
| 177 |
class ComicPDF(FPDF):
|
| 178 |
pass
|
| 179 |
|
| 180 |
+
def build_pdf(story, style, chapters, log_callback):
|
| 181 |
pdf = ComicPDF()
|
| 182 |
pdf.set_auto_page_break(True, margin=15)
|
| 183 |
|
|
|
|
| 193 |
pdf.cell(0, 8, f"Panel {p_idx}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
| 194 |
|
| 195 |
visual_prompt = build_visual_prompt(panel, style)
|
| 196 |
+
|
| 197 |
+
log_callback(f"\n=== GENERATING PANEL {p_idx} ===")
|
| 198 |
+
img_path = hf_generate_image_ultra(visual_prompt, log_callback)
|
| 199 |
|
| 200 |
if img_path:
|
| 201 |
pdf.image(img_path, x=20, w=170)
|
|
|
|
| 211 |
# ============================
|
| 212 |
# GRADIO UI
|
| 213 |
# ============================
|
| 214 |
+
def run_comic(story, style, chapters):
|
| 215 |
+
logs = []
|
| 216 |
+
|
| 217 |
+
def log_callback(msg):
|
| 218 |
+
logs.append(msg)
|
| 219 |
+
|
| 220 |
+
pdf_file = build_pdf(story, style, chapters, log_callback)
|
| 221 |
+
return pdf_file, "\n".join(logs)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
with gr.Blocks() as app:
|
| 225 |
|
| 226 |
+
gr.Markdown("# π AIPromptLab 3.2 β Ultra Stable Free Mode (HuggingFace Engine)")
|
| 227 |
+
|
| 228 |
+
with gr.Tab("Comic Generator"):
|
| 229 |
+
story = gr.Textbox(label="Ide Cerita Komik")
|
| 230 |
+
style = gr.Dropdown(
|
| 231 |
+
label="Style Visual",
|
| 232 |
+
choices=[
|
| 233 |
+
"Pastel 3D Isometric",
|
| 234 |
+
"Cute 3D Cartoon",
|
| 235 |
+
"Realistic Cinematic",
|
| 236 |
+
"Lowpoly Diorama",
|
| 237 |
+
"Claymation 3D",
|
| 238 |
+
"Toy Photography",
|
| 239 |
+
"Anime 3D Soft Light"
|
| 240 |
+
],
|
| 241 |
+
value="Pastel 3D Isometric"
|
| 242 |
+
)
|
| 243 |
+
chapters = gr.Slider(1, 10, value=1, step=1, label="Jumlah Bab")
|
| 244 |
+
btn = gr.Button("Generate Comic PDF")
|
| 245 |
+
out_pdf = gr.File()
|
| 246 |
+
out_log = gr.Textbox(label="Log Proses", lines=30)
|
| 247 |
+
|
| 248 |
+
btn.click(run_comic, [story, style, chapters], [out_pdf, out_log])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
app.launch(ssr_mode=False)
|