Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,24 @@ import requests
|
|
| 3 |
import base64
|
| 4 |
import uuid
|
| 5 |
import os
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
-
from huggingface_hub import
|
| 8 |
-
from fpdf import FPDF
|
| 9 |
|
| 10 |
# ============================
|
| 11 |
-
# CONFIG
|
| 12 |
# ============================
|
| 13 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 14 |
-
|
| 15 |
|
| 16 |
HEADERS = {
|
| 17 |
"Authorization": f"Bearer {HF_API_KEY}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
|
|
|
|
|
|
| 21 |
# ============================
|
| 22 |
# MODEL ENDPOINTS (FREE)
|
| 23 |
# ============================
|
|
@@ -29,7 +32,7 @@ MODEL_ENDPOINTS = {
|
|
| 29 |
}
|
| 30 |
|
| 31 |
# ============================
|
| 32 |
-
#
|
| 33 |
# ============================
|
| 34 |
def hf_generate_image(model, prompt):
|
| 35 |
url = f"https://api-inference.huggingface.co/models/{model}"
|
|
@@ -39,21 +42,54 @@ def hf_generate_image(model, prompt):
|
|
| 39 |
"options": {"wait_for_model": True}
|
| 40 |
}
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
# ============================
|
| 59 |
# GROQ TEXT GENERATOR
|
|
@@ -101,7 +137,7 @@ def build_visual_prompt(panel_text, style):
|
|
| 101 |
"""
|
| 102 |
|
| 103 |
# ============================
|
| 104 |
-
# PDF BUILDER
|
| 105 |
# ============================
|
| 106 |
class ComicPDF(FPDF):
|
| 107 |
pass
|
|
@@ -115,11 +151,11 @@ def build_pdf(story, style, model, chapters):
|
|
| 115 |
for c_idx, chapter in enumerate(panels, start=1):
|
| 116 |
pdf.add_page()
|
| 117 |
pdf.set_font("Helvetica", "B", 16)
|
| 118 |
-
pdf.cell(0, 10, f"Bab {c_idx}",
|
| 119 |
|
| 120 |
for p_idx, panel in enumerate(chapter, start=1):
|
| 121 |
pdf.set_font("Helvetica", "B", 12)
|
| 122 |
-
pdf.cell(0, 8, f"Panel {p_idx}",
|
| 123 |
|
| 124 |
visual_prompt = build_visual_prompt(panel, style)
|
| 125 |
img_path = hf_generate_image(model, visual_prompt)
|
|
@@ -249,4 +285,4 @@ with gr.Blocks() as app:
|
|
| 249 |
|
| 250 |
btn3.click(generate_storyboard, [vid_desc, vid_style, vid_model], frame_gallery)
|
| 251 |
|
| 252 |
-
app.launch()
|
|
|
|
| 3 |
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 |
# ============================
|
| 12 |
+
# CONFIG (AMAN)
|
| 13 |
# ============================
|
| 14 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 15 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 16 |
|
| 17 |
HEADERS = {
|
| 18 |
"Authorization": f"Bearer {HF_API_KEY}",
|
| 19 |
"Content-Type": "application/json"
|
| 20 |
}
|
| 21 |
|
| 22 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 23 |
+
|
| 24 |
# ============================
|
| 25 |
# MODEL ENDPOINTS (FREE)
|
| 26 |
# ============================
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
# ============================
|
| 35 |
+
# ENGINE GAMBAR v2.0 (ANTI-GAGAL)
|
| 36 |
# ============================
|
| 37 |
def hf_generate_image(model, prompt):
|
| 38 |
url = f"https://api-inference.huggingface.co/models/{model}"
|
|
|
|
| 42 |
"options": {"wait_for_model": True}
|
| 43 |
}
|
| 44 |
|
| 45 |
+
# Retry 3x untuk cold start
|
| 46 |
+
for attempt in range(3):
|
| 47 |
+
response = requests.post(url, headers=HEADERS, json=payload)
|
| 48 |
+
|
| 49 |
+
# Jika model masih loading
|
| 50 |
+
try:
|
| 51 |
+
data = response.json()
|
| 52 |
+
if "error" in data:
|
| 53 |
+
if "loading" in data["error"].lower():
|
| 54 |
+
continue
|
| 55 |
+
except:
|
| 56 |
+
pass
|
| 57 |
+
|
| 58 |
+
# Jika response adalah gambar bytes
|
| 59 |
+
if response.status_code == 200:
|
| 60 |
+
content_type = response.headers.get("Content-Type", "")
|
| 61 |
+
|
| 62 |
+
# Jika raw image
|
| 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 |
+
data = response.json()
|
| 76 |
+
if isinstance(data, dict):
|
| 77 |
+
if "generated_image" in data:
|
| 78 |
+
b64 = data["generated_image"].split(",")[-1]
|
| 79 |
+
img_bytes = base64.b64decode(b64)
|
| 80 |
|
| 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(img_bytes)
|
| 87 |
|
| 88 |
+
return filepath
|
| 89 |
+
except:
|
| 90 |
+
pass
|
| 91 |
|
| 92 |
+
return None
|
| 93 |
|
| 94 |
# ============================
|
| 95 |
# GROQ TEXT GENERATOR
|
|
|
|
| 137 |
"""
|
| 138 |
|
| 139 |
# ============================
|
| 140 |
+
# PDF BUILDER (AMAN)
|
| 141 |
# ============================
|
| 142 |
class ComicPDF(FPDF):
|
| 143 |
pass
|
|
|
|
| 151 |
for c_idx, chapter in enumerate(panels, start=1):
|
| 152 |
pdf.add_page()
|
| 153 |
pdf.set_font("Helvetica", "B", 16)
|
| 154 |
+
pdf.cell(0, 10, f"Bab {c_idx}", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
| 155 |
|
| 156 |
for p_idx, panel in enumerate(chapter, start=1):
|
| 157 |
pdf.set_font("Helvetica", "B", 12)
|
| 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 |
img_path = hf_generate_image(model, visual_prompt)
|
|
|
|
| 285 |
|
| 286 |
btn3.click(generate_storyboard, [vid_desc, vid_style, vid_model], frame_gallery)
|
| 287 |
|
| 288 |
+
app.launch(ssr_mode=False)
|