Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import uuid
|
| 4 |
import os
|
| 5 |
from groq import Groq
|
|
|
|
| 6 |
from fpdf import FPDF
|
| 7 |
|
| 8 |
# ============================
|
| 9 |
# CONFIG
|
| 10 |
# ============================
|
| 11 |
-
|
| 12 |
client = Groq(api_key="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")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ============================
|
| 15 |
-
#
|
| 16 |
-
# ============================
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
os.makedirs("outputs", exist_ok=True)
|
| 37 |
-
filename = f"nano_{uuid.uuid4().hex}.jpg"
|
| 38 |
filepath = os.path.join("outputs", filename)
|
| 39 |
|
| 40 |
with open(filepath, "wb") as f:
|
| 41 |
-
f.write(
|
| 42 |
|
| 43 |
-
return
|
| 44 |
|
| 45 |
# ============================
|
| 46 |
# GROQ TEXT GENERATOR
|
|
@@ -82,8 +95,8 @@ def generate_panels(story, style, chapters):
|
|
| 82 |
# ============================
|
| 83 |
def build_visual_prompt(panel_text, style):
|
| 84 |
return f"""
|
| 85 |
-
{style},
|
| 86 |
-
cinematic depth,
|
| 87 |
Scene: {panel_text}
|
| 88 |
"""
|
| 89 |
|
|
@@ -93,7 +106,7 @@ def build_visual_prompt(panel_text, style):
|
|
| 93 |
class ComicPDF(FPDF):
|
| 94 |
pass
|
| 95 |
|
| 96 |
-
def build_pdf(story, style, chapters):
|
| 97 |
pdf = ComicPDF()
|
| 98 |
pdf.set_auto_page_break(True, margin=15)
|
| 99 |
|
|
@@ -109,10 +122,10 @@ def build_pdf(story, style, chapters):
|
|
| 109 |
pdf.cell(0, 8, f"Panel {p_idx}", 0, 1)
|
| 110 |
|
| 111 |
visual_prompt = build_visual_prompt(panel, style)
|
| 112 |
-
|
| 113 |
|
| 114 |
-
if
|
| 115 |
-
pdf.image(
|
| 116 |
|
| 117 |
pdf.set_font("Helvetica", "", 11)
|
| 118 |
pdf.multi_cell(0, 6, panel)
|
|
@@ -127,7 +140,7 @@ def build_pdf(story, style, chapters):
|
|
| 127 |
# ============================
|
| 128 |
with gr.Blocks() as app:
|
| 129 |
|
| 130 |
-
gr.Markdown("# 🌟 AIPromptLab 3.
|
| 131 |
|
| 132 |
with gr.Tabs():
|
| 133 |
|
|
@@ -149,11 +162,19 @@ with gr.Blocks() as app:
|
|
| 149 |
],
|
| 150 |
value="Pastel 3D Isometric"
|
| 151 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
chapters = gr.Slider(1, 10, value=1, step=1, label="Jumlah Bab")
|
| 153 |
btn = gr.Button("Generate Comic PDF")
|
| 154 |
out = gr.File()
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
# ============================
|
| 159 |
# TAB 2 — IMAGE GENERATOR
|
|
@@ -173,20 +194,22 @@ with gr.Blocks() as app:
|
|
| 173 |
],
|
| 174 |
value="Pastel 3D Isometric"
|
| 175 |
)
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
btn2 = gr.Button("Generate Image")
|
| 179 |
img_out = gr.Image()
|
| 180 |
|
| 181 |
-
def generate_image(p, s,
|
| 182 |
-
prompt = f"{s}
|
| 183 |
-
|
| 184 |
-
return path
|
| 185 |
|
| 186 |
-
btn2.click(generate_image, [img_prompt, img_style,
|
| 187 |
|
| 188 |
# ============================
|
| 189 |
-
# TAB 3 — VIDEO
|
| 190 |
# ============================
|
| 191 |
with gr.Tab("Video Storyboard"):
|
| 192 |
vid_desc = gr.Textbox(label="Deskripsi Video")
|
|
@@ -203,22 +226,27 @@ with gr.Blocks() as app:
|
|
| 203 |
],
|
| 204 |
value="Pastel 3D Isometric"
|
| 205 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
btn3 = gr.Button("Generate Storyboard Frames")
|
| 207 |
frame_gallery = gr.Gallery(label="Frames")
|
| 208 |
|
| 209 |
-
def generate_storyboard(desc, style):
|
| 210 |
-
prompt = f"Buatkan 8 scene
|
| 211 |
scenes = ai(prompt).split("\n")
|
| 212 |
frames = []
|
| 213 |
|
| 214 |
for s in scenes[:8]:
|
| 215 |
visual = build_visual_prompt(s, style)
|
| 216 |
-
|
| 217 |
-
if
|
| 218 |
-
frames.append(
|
| 219 |
|
| 220 |
return frames
|
| 221 |
|
| 222 |
-
btn3.click(generate_storyboard, [vid_desc, vid_style], frame_gallery)
|
| 223 |
|
| 224 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
from groq import Groq
|
| 7 |
+
from huggingface_hub import hf_hub_url, model_info
|
| 8 |
from fpdf import FPDF
|
| 9 |
|
| 10 |
# ============================
|
| 11 |
# CONFIG
|
| 12 |
# ============================
|
| 13 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 14 |
client = Groq(api_key="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")
|
| 15 |
|
| 16 |
+
HEADERS = {
|
| 17 |
+
"Authorization": f"Bearer {HF_API_KEY}",
|
| 18 |
+
"Content-Type": "application/json"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
# ============================
|
| 22 |
+
# MODEL ENDPOINTS (FREE)
|
| 23 |
+
# ============================
|
| 24 |
+
MODEL_ENDPOINTS = {
|
| 25 |
+
"SDXL": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 26 |
+
"Playground v2.5": "playgroundai/playground-v2.5-1024px-aesthetic",
|
| 27 |
+
"PixArt-XL": "PixArt-alpha/PixArt-XL-2-1024-MS",
|
| 28 |
+
"SD Turbo": "stabilityai/sd-turbo"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# ============================
|
| 32 |
+
# HF INFERENCE API ENGINE
|
| 33 |
+
# ============================
|
| 34 |
+
def hf_generate_image(model, prompt):
|
| 35 |
+
url = f"https://api-inference.huggingface.co/models/{model}"
|
|
|
|
| 36 |
|
| 37 |
+
payload = {
|
| 38 |
+
"inputs": prompt,
|
| 39 |
+
"options": {"wait_for_model": True}
|
| 40 |
+
}
|
| 41 |
|
| 42 |
+
response = requests.post(url, headers=HEADERS, json=payload)
|
| 43 |
+
|
| 44 |
+
if response.status_code != 200:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
img_bytes = response.content
|
| 48 |
+
filename = f"img_{uuid.uuid4().hex}.png"
|
| 49 |
|
| 50 |
os.makedirs("outputs", exist_ok=True)
|
|
|
|
| 51 |
filepath = os.path.join("outputs", filename)
|
| 52 |
|
| 53 |
with open(filepath, "wb") as f:
|
| 54 |
+
f.write(img_bytes)
|
| 55 |
|
| 56 |
+
return filepath
|
| 57 |
|
| 58 |
# ============================
|
| 59 |
# GROQ TEXT GENERATOR
|
|
|
|
| 95 |
# ============================
|
| 96 |
def build_visual_prompt(panel_text, style):
|
| 97 |
return f"""
|
| 98 |
+
{style}, soft lighting, clean shapes, child-friendly,
|
| 99 |
+
cinematic depth, high detail.
|
| 100 |
Scene: {panel_text}
|
| 101 |
"""
|
| 102 |
|
|
|
|
| 106 |
class ComicPDF(FPDF):
|
| 107 |
pass
|
| 108 |
|
| 109 |
+
def build_pdf(story, style, model, chapters):
|
| 110 |
pdf = ComicPDF()
|
| 111 |
pdf.set_auto_page_break(True, margin=15)
|
| 112 |
|
|
|
|
| 122 |
pdf.cell(0, 8, f"Panel {p_idx}", 0, 1)
|
| 123 |
|
| 124 |
visual_prompt = build_visual_prompt(panel, style)
|
| 125 |
+
img_path = hf_generate_image(model, visual_prompt)
|
| 126 |
|
| 127 |
+
if img_path:
|
| 128 |
+
pdf.image(img_path, x=20, w=170)
|
| 129 |
|
| 130 |
pdf.set_font("Helvetica", "", 11)
|
| 131 |
pdf.multi_cell(0, 6, panel)
|
|
|
|
| 140 |
# ============================
|
| 141 |
with gr.Blocks() as app:
|
| 142 |
|
| 143 |
+
gr.Markdown("# 🌟 AIPromptLab 3.1 — FREE MODE (HuggingFace Engine)")
|
| 144 |
|
| 145 |
with gr.Tabs():
|
| 146 |
|
|
|
|
| 162 |
],
|
| 163 |
value="Pastel 3D Isometric"
|
| 164 |
)
|
| 165 |
+
model = gr.Dropdown(
|
| 166 |
+
label="Model Gratis",
|
| 167 |
+
choices=list(MODEL_ENDPOINTS.keys()),
|
| 168 |
+
value="Playground v2.5"
|
| 169 |
+
)
|
| 170 |
chapters = gr.Slider(1, 10, value=1, step=1, label="Jumlah Bab")
|
| 171 |
btn = gr.Button("Generate Comic PDF")
|
| 172 |
out = gr.File()
|
| 173 |
|
| 174 |
+
def run_comic(story, style, model, chapters):
|
| 175 |
+
return build_pdf(story, style, MODEL_ENDPOINTS[model], chapters)
|
| 176 |
+
|
| 177 |
+
btn.click(run_comic, [story, style, model, chapters], out)
|
| 178 |
|
| 179 |
# ============================
|
| 180 |
# TAB 2 — IMAGE GENERATOR
|
|
|
|
| 194 |
],
|
| 195 |
value="Pastel 3D Isometric"
|
| 196 |
)
|
| 197 |
+
img_model = gr.Dropdown(
|
| 198 |
+
label="Model Gratis",
|
| 199 |
+
choices=list(MODEL_ENDPOINTS.keys()),
|
| 200 |
+
value="Playground v2.5"
|
| 201 |
+
)
|
| 202 |
btn2 = gr.Button("Generate Image")
|
| 203 |
img_out = gr.Image()
|
| 204 |
|
| 205 |
+
def generate_image(p, s, m):
|
| 206 |
+
prompt = f"{s}. Scene: {p}"
|
| 207 |
+
return hf_generate_image(MODEL_ENDPOINTS[m], prompt)
|
|
|
|
| 208 |
|
| 209 |
+
btn2.click(generate_image, [img_prompt, img_style, img_model], img_out)
|
| 210 |
|
| 211 |
# ============================
|
| 212 |
+
# TAB 3 — VIDEO STORYBOARD
|
| 213 |
# ============================
|
| 214 |
with gr.Tab("Video Storyboard"):
|
| 215 |
vid_desc = gr.Textbox(label="Deskripsi Video")
|
|
|
|
| 226 |
],
|
| 227 |
value="Pastel 3D Isometric"
|
| 228 |
)
|
| 229 |
+
vid_model = gr.Dropdown(
|
| 230 |
+
label="Model Gratis",
|
| 231 |
+
choices=list(MODEL_ENDPOINTS.keys()),
|
| 232 |
+
value="SD Turbo"
|
| 233 |
+
)
|
| 234 |
btn3 = gr.Button("Generate Storyboard Frames")
|
| 235 |
frame_gallery = gr.Gallery(label="Frames")
|
| 236 |
|
| 237 |
+
def generate_storyboard(desc, style, model):
|
| 238 |
+
prompt = f"Buatkan 8 scene storyboard. Deskripsi: {desc}"
|
| 239 |
scenes = ai(prompt).split("\n")
|
| 240 |
frames = []
|
| 241 |
|
| 242 |
for s in scenes[:8]:
|
| 243 |
visual = build_visual_prompt(s, style)
|
| 244 |
+
img_path = hf_generate_image(MODEL_ENDPOINTS[model], visual)
|
| 245 |
+
if img_path:
|
| 246 |
+
frames.append(img_path)
|
| 247 |
|
| 248 |
return frames
|
| 249 |
|
| 250 |
+
btn3.click(generate_storyboard, [vid_desc, vid_style, vid_model], frame_gallery)
|
| 251 |
|
| 252 |
app.launch()
|