Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,6 +41,31 @@ def now_iso():
|
|
| 41 |
def ensure_outputs_dir():
|
| 42 |
os.makedirs("outputs", exist_ok=True)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# ============================
|
| 45 |
# REPLICATE (FREE MODELS)
|
| 46 |
# ============================
|
|
@@ -145,20 +170,14 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 145 |
for i, ch in enumerate(chapters, start=1):
|
| 146 |
pdf.cell(0, 8, safe_text(f"{i}. {ch}"), 0, 1)
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
body_paragraph = (
|
| 150 |
-
f"Cerita ini mengisahkan tentang {goal.lower()}, disampaikan dengan gaya {style.lower()} "
|
| 151 |
-
f"dan nuansa {tone.lower()}. Cerita dirancang agar mudah dipahami anak-anak, "
|
| 152 |
-
"mengandung nilai-nilai kebaikan."
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
# CHAPTERS
|
| 156 |
for idx, ch in enumerate(chapters, start=1):
|
| 157 |
pdf.add_page()
|
| 158 |
pdf.set_font("Helvetica", "B", 16)
|
| 159 |
pdf.cell(0, 10, safe_text(ch), 0, 1)
|
| 160 |
pdf.ln(4)
|
| 161 |
|
|
|
|
| 162 |
illus_prompt = (
|
| 163 |
f"Illustration for chapter {idx} of an Islamic children's story about {goal}. "
|
| 164 |
f"Scene: {ch}. Style: {style}, tone {tone}, pastel colors."
|
|
@@ -171,10 +190,12 @@ def build_ebook_pdf_with_images(username, goal, genre, tone, style, length,
|
|
| 171 |
pdf.image(illus_path, x=20, y=y_start, w=170)
|
| 172 |
pdf.set_y(y_start + 80)
|
| 173 |
|
|
|
|
|
|
|
|
|
|
| 174 |
pdf.set_font("Helvetica", "", 12)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
pdf.ln(2)
|
| 178 |
|
| 179 |
filename = os.path.join("outputs", f"ebook_{uuid.uuid4().hex}.pdf")
|
| 180 |
pdf.output(filename)
|
|
|
|
| 41 |
def ensure_outputs_dir():
|
| 42 |
os.makedirs("outputs", exist_ok=True)
|
| 43 |
|
| 44 |
+
# ============================
|
| 45 |
+
# AI TEXT GENERATOR (LLaMA‑3)
|
| 46 |
+
# ============================
|
| 47 |
+
def ai_generate(prompt):
|
| 48 |
+
try:
|
| 49 |
+
output = replicate.run(
|
| 50 |
+
"meta/meta-llama-3-8b-instruct",
|
| 51 |
+
input={"prompt": prompt, "max_tokens": 500}
|
| 52 |
+
)
|
| 53 |
+
return "".join(output)
|
| 54 |
+
except:
|
| 55 |
+
return "AI gagal menghasilkan teks."
|
| 56 |
+
|
| 57 |
+
def generate_chapter_text(goal, style, tone, chapter_title):
|
| 58 |
+
prompt = f"""
|
| 59 |
+
Buatkan 1 bab cerita anak Islami.
|
| 60 |
+
Judul bab: {chapter_title}
|
| 61 |
+
Tema utama: {goal}
|
| 62 |
+
Gaya: {style}
|
| 63 |
+
Tone: {tone}
|
| 64 |
+
Panjang: 4 paragraf.
|
| 65 |
+
Bahasa Indonesia.
|
| 66 |
+
"""
|
| 67 |
+
return ai_generate(prompt)
|
| 68 |
+
|
| 69 |
# ============================
|
| 70 |
# REPLICATE (FREE MODELS)
|
| 71 |
# ============================
|
|
|
|
| 170 |
for i, ch in enumerate(chapters, start=1):
|
| 171 |
pdf.cell(0, 8, safe_text(f"{i}. {ch}"), 0, 1)
|
| 172 |
|
| 173 |
+
# CHAPTERS (LANGSUNG BAB 1–N, TANPA RINGKASAN/OUTLINE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
for idx, ch in enumerate(chapters, start=1):
|
| 175 |
pdf.add_page()
|
| 176 |
pdf.set_font("Helvetica", "B", 16)
|
| 177 |
pdf.cell(0, 10, safe_text(ch), 0, 1)
|
| 178 |
pdf.ln(4)
|
| 179 |
|
| 180 |
+
# ILLUSTRATION
|
| 181 |
illus_prompt = (
|
| 182 |
f"Illustration for chapter {idx} of an Islamic children's story about {goal}. "
|
| 183 |
f"Scene: {ch}. Style: {style}, tone {tone}, pastel colors."
|
|
|
|
| 190 |
pdf.image(illus_path, x=20, y=y_start, w=170)
|
| 191 |
pdf.set_y(y_start + 80)
|
| 192 |
|
| 193 |
+
# AI CHAPTER TEXT
|
| 194 |
+
chapter_text = generate_chapter_text(goal, style, tone, ch)
|
| 195 |
+
|
| 196 |
pdf.set_font("Helvetica", "", 12)
|
| 197 |
+
pdf.multi_cell(180, 7, safe_text(chapter_text), 0)
|
| 198 |
+
pdf.ln(2)
|
|
|
|
| 199 |
|
| 200 |
filename = os.path.join("outputs", f"ebook_{uuid.uuid4().hex}.pdf")
|
| 201 |
pdf.output(filename)
|