Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ import replicate
|
|
| 11 |
# ============================
|
| 12 |
# CONFIG
|
| 13 |
# ============================
|
| 14 |
-
REPLICATE_API_TOKEN = "
|
| 15 |
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
|
| 16 |
|
| 17 |
MAX_CHAPTERS = 10 # batas maksimal bab
|
|
@@ -40,36 +40,44 @@ def ensure_outputs_dir():
|
|
| 40 |
os.makedirs("outputs", exist_ok=True)
|
| 41 |
|
| 42 |
# ============================
|
| 43 |
-
# REPLICATE IMAGE HELPERS
|
| 44 |
# ============================
|
| 45 |
def generate_image_flux(prompt: str) -> str:
|
| 46 |
"""
|
| 47 |
-
Generate image URL using Flux Schnell on Replicate (for cover).
|
|
|
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
return ""
|
| 57 |
|
| 58 |
def generate_image_sdxl(prompt: str) -> str:
|
| 59 |
"""
|
| 60 |
-
Generate image URL using SDXL on Replicate (for chapter illustrations).
|
|
|
|
| 61 |
"""
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
return ""
|
| 69 |
|
| 70 |
def download_image(url: str, filename: str) -> str:
|
| 71 |
"""
|
| 72 |
Download image from URL to local file.
|
|
|
|
| 73 |
"""
|
| 74 |
if not url:
|
| 75 |
return ""
|
|
@@ -244,7 +252,7 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 244 |
pdf = EbookPDF(format="A4")
|
| 245 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 246 |
|
| 247 |
-
# ===== COVER IMAGE VIA FLUX =====
|
| 248 |
cover_prompt = f"{cover_brief} | {goal} | {genre} | {style} | pastel, kid-friendly, Islamic"
|
| 249 |
cover_url = generate_image_flux(cover_prompt)
|
| 250 |
cover_path = download_image(cover_url, f"cover_{uuid.uuid4().hex}.png")
|
|
@@ -252,7 +260,6 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 252 |
# COVER PAGE
|
| 253 |
pdf.add_page()
|
| 254 |
if cover_path:
|
| 255 |
-
# full width cover
|
| 256 |
pdf.image(cover_path, x=0, y=0, w=210)
|
| 257 |
pdf.ln(200)
|
| 258 |
else:
|
|
@@ -286,14 +293,13 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 286 |
"mengandung nilai-nilai kebaikan, dan mengajak pembaca untuk merenungkan makna di balik setiap peristiwa."
|
| 287 |
)
|
| 288 |
|
| 289 |
-
# CHAPTERS WITH SDXL IMAGES
|
| 290 |
for idx, ch in enumerate(chapters, start=1):
|
| 291 |
pdf.add_page()
|
| 292 |
pdf.set_font("Helvetica", "B", 16)
|
| 293 |
pdf.cell(0, 10, ch, 0, 1)
|
| 294 |
pdf.ln(4)
|
| 295 |
|
| 296 |
-
# generate illustration for this chapter
|
| 297 |
illus_prompt = (
|
| 298 |
f"Illustration for chapter {idx} of an Islamic children's story about {goal}. "
|
| 299 |
f"Scene: {ch}. Style: {style}, tone {tone}, pastel colors, kid-friendly, Islamic."
|
|
@@ -302,8 +308,9 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 302 |
illus_path = download_image(illus_url, f"chapter_{idx}_{uuid.uuid4().hex}.png")
|
| 303 |
|
| 304 |
if illus_path:
|
| 305 |
-
|
| 306 |
-
pdf.
|
|
|
|
| 307 |
|
| 308 |
pdf.set_font("Helvetica", "", 12)
|
| 309 |
for _ in range(5):
|
|
@@ -343,7 +350,7 @@ with gr.Blocks() as demo:
|
|
| 343 |
# MAIN UI (hidden first)
|
| 344 |
with gr.Group(visible=False) as main_ui:
|
| 345 |
|
| 346 |
-
gr.Markdown("# π AIPromptLab β JSON & PDF E-Book Generator (Replicate
|
| 347 |
|
| 348 |
with gr.Tabs():
|
| 349 |
|
|
|
|
| 11 |
# ============================
|
| 12 |
# CONFIG
|
| 13 |
# ============================
|
| 14 |
+
REPLICATE_API_TOKEN = "ISI_API_KEY_REPLICATE_KAMU_DI_SINI"
|
| 15 |
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
|
| 16 |
|
| 17 |
MAX_CHAPTERS = 10 # batas maksimal bab
|
|
|
|
| 40 |
os.makedirs("outputs", exist_ok=True)
|
| 41 |
|
| 42 |
# ============================
|
| 43 |
+
# REPLICATE IMAGE HELPERS (MODEL GRATIS + FALLBACK)
|
| 44 |
# ============================
|
| 45 |
def generate_image_flux(prompt: str) -> str:
|
| 46 |
"""
|
| 47 |
+
Generate image URL using Flux Schnell FREE on Replicate (for cover).
|
| 48 |
+
Jika gagal (kredit / model / jaringan), return "".
|
| 49 |
"""
|
| 50 |
+
try:
|
| 51 |
+
output = replicate.run(
|
| 52 |
+
"black-forest-labs/flux-schnell-free",
|
| 53 |
+
input={"prompt": prompt}
|
| 54 |
+
)
|
| 55 |
+
if isinstance(output, list) and len(output) > 0:
|
| 56 |
+
return output[0]
|
| 57 |
+
except Exception:
|
| 58 |
+
return ""
|
| 59 |
return ""
|
| 60 |
|
| 61 |
def generate_image_sdxl(prompt: str) -> str:
|
| 62 |
"""
|
| 63 |
+
Generate image URL using SDXL LITE (gratis) on Replicate (for chapter illustrations).
|
| 64 |
+
Jika gagal, return "".
|
| 65 |
"""
|
| 66 |
+
try:
|
| 67 |
+
output = replicate.run(
|
| 68 |
+
"stability-ai/sdxl-lite",
|
| 69 |
+
input={"prompt": prompt}
|
| 70 |
+
)
|
| 71 |
+
if isinstance(output, list) and len(output) > 0:
|
| 72 |
+
return output[0]
|
| 73 |
+
except Exception:
|
| 74 |
+
return ""
|
| 75 |
return ""
|
| 76 |
|
| 77 |
def download_image(url: str, filename: str) -> str:
|
| 78 |
"""
|
| 79 |
Download image from URL to local file.
|
| 80 |
+
Jika gagal, return "".
|
| 81 |
"""
|
| 82 |
if not url:
|
| 83 |
return ""
|
|
|
|
| 252 |
pdf = EbookPDF(format="A4")
|
| 253 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 254 |
|
| 255 |
+
# ===== COVER IMAGE VIA FLUX (FREE) =====
|
| 256 |
cover_prompt = f"{cover_brief} | {goal} | {genre} | {style} | pastel, kid-friendly, Islamic"
|
| 257 |
cover_url = generate_image_flux(cover_prompt)
|
| 258 |
cover_path = download_image(cover_url, f"cover_{uuid.uuid4().hex}.png")
|
|
|
|
| 260 |
# COVER PAGE
|
| 261 |
pdf.add_page()
|
| 262 |
if cover_path:
|
|
|
|
| 263 |
pdf.image(cover_path, x=0, y=0, w=210)
|
| 264 |
pdf.ln(200)
|
| 265 |
else:
|
|
|
|
| 293 |
"mengandung nilai-nilai kebaikan, dan mengajak pembaca untuk merenungkan makna di balik setiap peristiwa."
|
| 294 |
)
|
| 295 |
|
| 296 |
+
# CHAPTERS WITH SDXL LITE IMAGES
|
| 297 |
for idx, ch in enumerate(chapters, start=1):
|
| 298 |
pdf.add_page()
|
| 299 |
pdf.set_font("Helvetica", "B", 16)
|
| 300 |
pdf.cell(0, 10, ch, 0, 1)
|
| 301 |
pdf.ln(4)
|
| 302 |
|
|
|
|
| 303 |
illus_prompt = (
|
| 304 |
f"Illustration for chapter {idx} of an Islamic children's story about {goal}. "
|
| 305 |
f"Scene: {ch}. Style: {style}, tone {tone}, pastel colors, kid-friendly, Islamic."
|
|
|
|
| 308 |
illus_path = download_image(illus_url, f"chapter_{idx}_{uuid.uuid4().hex}.png")
|
| 309 |
|
| 310 |
if illus_path:
|
| 311 |
+
y_start = pdf.get_y()
|
| 312 |
+
pdf.image(illus_path, x=20, y=y_start, w=170)
|
| 313 |
+
pdf.set_y(y_start + 80)
|
| 314 |
|
| 315 |
pdf.set_font("Helvetica", "", 12)
|
| 316 |
for _ in range(5):
|
|
|
|
| 350 |
# MAIN UI (hidden first)
|
| 351 |
with gr.Group(visible=False) as main_ui:
|
| 352 |
|
| 353 |
+
gr.Markdown("# π AIPromptLab β JSON & PDF E-Book Generator (Replicate FREE Models)")
|
| 354 |
|
| 355 |
with gr.Tabs():
|
| 356 |
|