Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -144,6 +144,9 @@ def update_staging_and_manuscript(files, current_manuscript):
|
|
| 144 |
text_gallery, image_gallery, pdf_gallery = [], [], []
|
| 145 |
text_content_to_add = []
|
| 146 |
|
|
|
|
|
|
|
|
|
|
| 147 |
sorted_files = sorted(files, key=lambda x: Path(x.name).suffix)
|
| 148 |
|
| 149 |
for file_obj in sorted_files:
|
|
@@ -318,7 +321,110 @@ def generate_pdfs_api(files, ai_content, layouts, fonts, num_columns, header_tex
|
|
| 318 |
AVAILABLE_FONTS = register_local_fonts()
|
| 319 |
|
| 320 |
def get_theme():
|
| 321 |
-
|
|
|
|
|
|
|
| 322 |
font_family = fallback
|
|
|
|
|
|
|
| 323 |
if any(desired in s for s in AVAILABLE_FONTS):
|
| 324 |
-
font_family = (gr.themes.GoogleFont(desired
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
text_gallery, image_gallery, pdf_gallery = [], [], []
|
| 145 |
text_content_to_add = []
|
| 146 |
|
| 147 |
+
if not files:
|
| 148 |
+
return text_gallery, image_gallery, pdf_gallery, current_manuscript
|
| 149 |
+
|
| 150 |
sorted_files = sorted(files, key=lambda x: Path(x.name).suffix)
|
| 151 |
|
| 152 |
for file_obj in sorted_files:
|
|
|
|
| 321 |
AVAILABLE_FONTS = register_local_fonts()
|
| 322 |
|
| 323 |
def get_theme():
|
| 324 |
+
"""Dynamically selects a font for the theme to avoid warnings for missing fonts."""
|
| 325 |
+
desired = "MedievalSharp"
|
| 326 |
+
fallback = ("ui-sans-serif", "system-ui", "sans-serif")
|
| 327 |
font_family = fallback
|
| 328 |
+
|
| 329 |
+
# Check if the desired font is in the list of registered fonts
|
| 330 |
if any(desired in s for s in AVAILABLE_FONTS):
|
| 331 |
+
font_family = (gr.themes.GoogleFont(desired),) + fallback
|
| 332 |
+
elif AVAILABLE_FONTS:
|
| 333 |
+
# If desired font is not found, use the first available font as a better fallback
|
| 334 |
+
first_font = AVAILABLE_FONTS[0]
|
| 335 |
+
print(f"WARNING: '{desired}' font not found. Using '{first_font}' for UI theme instead.")
|
| 336 |
+
font_family = (gr.themes.GoogleFont(first_font),) + fallback
|
| 337 |
+
else:
|
| 338 |
+
# Absolute fallback if no custom fonts are found
|
| 339 |
+
print(f"WARNING: No custom fonts found. Using system default for UI.")
|
| 340 |
+
|
| 341 |
+
return gr.themes.Base(
|
| 342 |
+
primary_hue=gr.themes.colors.purple,
|
| 343 |
+
secondary_hue=gr.themes.colors.indigo,
|
| 344 |
+
neutral_hue=gr.themes.colors.gray,
|
| 345 |
+
font=font_family
|
| 346 |
+
).set(
|
| 347 |
+
body_background_fill="#111827",
|
| 348 |
+
body_text_color="#d1d5db",
|
| 349 |
+
button_primary_background_fill="#a855f7",
|
| 350 |
+
button_primary_text_color="#ffffff",
|
| 351 |
+
button_secondary_background_fill="#6366f1",
|
| 352 |
+
button_secondary_text_color="#ffffff",
|
| 353 |
+
block_background_fill="#1f2937",
|
| 354 |
+
block_label_background_fill="#1f2937",
|
| 355 |
+
block_title_text_color="#a855f7",
|
| 356 |
+
input_background_fill="#374151"
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
with gr.Blocks(theme=get_theme(), title="The PDF Alchemist") as demo:
|
| 360 |
+
gr.Markdown("# โจ The PDF Alchemist โจ")
|
| 361 |
+
gr.Markdown("A single-page grimoire to turn your ideas into beautifully crafted PDF scrolls. Use the power of AI or upload your own treasures.")
|
| 362 |
+
|
| 363 |
+
with gr.Row(equal_height=False):
|
| 364 |
+
# --- LEFT COLUMN: INPUTS & CONTROLS ---
|
| 365 |
+
with gr.Column(scale=2):
|
| 366 |
+
with gr.Group():
|
| 367 |
+
with gr.Accordion("๐ Content Crucible (Your Ingredients)", open=True):
|
| 368 |
+
gr.Markdown("### ๐ค Command Your Idea Golem")
|
| 369 |
+
ai_prompt = gr.Textbox(label="Incantation (Prompt)", placeholder="e.g., 'A recipe for a dragon's breath chili...'")
|
| 370 |
+
generate_ai_btn = gr.Button("๐ง Animate Golem!")
|
| 371 |
+
ai_content_output = gr.Textbox(label="Golem's Manuscript (Editable)", lines=10, interactive=True, value="# The Golem awaits your command!\n\n")
|
| 372 |
+
gr.Markdown("<hr style='border-color: #374151; margin-top: 20px; margin-bottom: 20px;'>")
|
| 373 |
+
gr.Markdown("### ๐ค Add Your Physical Treasures")
|
| 374 |
+
uploaded_files = gr.File(label="Upload Files (TXT, MD, Code, Images, PDFs)", file_count="multiple", file_types=['text', 'image', '.pdf', '.py', '.js', '.html', '.css', '.json'])
|
| 375 |
+
|
| 376 |
+
with gr.Accordion("๐ Arcane Blueprints (Layout & Structure)", open=True):
|
| 377 |
+
selected_layouts = gr.CheckboxGroup(choices=list(LAYOUTS.keys()), label="Page Layouts", value=["A4 Portrait"])
|
| 378 |
+
num_columns_slider = gr.Slider(label="Number of Text Columns", minimum=1, maximum=4, step=1, value=1)
|
| 379 |
+
header_input = gr.Textbox(label="Header Inscription", value="[Title]", placeholder="e.g., Arcane Folio - [Page #]")
|
| 380 |
+
footer_input = gr.Textbox(label="Footer Inscription", value="Page [Page #] of [Total Pages]", placeholder="e.g., Top Secret - Page [Page #]")
|
| 381 |
+
|
| 382 |
+
with gr.Accordion("๐
Stylist's Sanctum (Fonts & Margins)", open=True):
|
| 383 |
+
use_svg_engine_toggle = gr.Checkbox(label="Use SVG Emoji Engine (Vector Quality)", value=True, info="Toggle for higher quality, resolution-independent emojis. May be slower.")
|
| 384 |
+
selected_fonts = gr.CheckboxGroup(choices=AVAILABLE_FONTS, label="Fonts", value=[AVAILABLE_FONTS[0]] if AVAILABLE_FONTS else [])
|
| 385 |
+
with gr.Row():
|
| 386 |
+
font_size_body_slider = gr.Slider(label="Body (pt)", minimum=8, maximum=16, step=1, value=10)
|
| 387 |
+
font_size_h1_slider = gr.Slider(label="H1 (pt)", minimum=16, maximum=32, step=1, value=24)
|
| 388 |
+
with gr.Row():
|
| 389 |
+
font_size_h2_slider = gr.Slider(label="H2 (pt)", minimum=14, maximum=28, step=1, value=18)
|
| 390 |
+
font_size_h3_slider = gr.Slider(label="H3 (pt)", minimum=12, maximum=24, step=1, value=14)
|
| 391 |
+
with gr.Row():
|
| 392 |
+
margin_top_slider = gr.Slider(label="Margin Top (in)", minimum=0.25, maximum=1.5, step=0.05, value=0.75)
|
| 393 |
+
margin_bottom_slider = gr.Slider(label="Margin Bottom (in)", minimum=0.25, maximum=1.5, step=0.05, value=0.75)
|
| 394 |
+
with gr.Row():
|
| 395 |
+
margin_left_slider = gr.Slider(label="Margin Left (in)", minimum=0.25, maximum=1.5, step=0.05, value=0.75)
|
| 396 |
+
margin_right_slider = gr.Slider(label="Margin Right (in)", minimum=0.25, maximum=1.5, step=0.05, value=0.75)
|
| 397 |
+
|
| 398 |
+
generate_pdfs_btn = gr.Button("๐ฎ Alchemize PDF!", variant="primary", size="lg")
|
| 399 |
+
|
| 400 |
+
# --- RIGHT COLUMN: STAGING & OUTPUTS ---
|
| 401 |
+
with gr.Column(scale=3):
|
| 402 |
+
with gr.Group():
|
| 403 |
+
with gr.Accordion("๐ Staging Area (Your Uploaded Treasures)", open=True):
|
| 404 |
+
text_gallery = gr.Gallery(label="๐ Manuscripts & Code", show_label=True, columns=4, height=120, object_fit="contain")
|
| 405 |
+
image_gallery = gr.Gallery(label="๐ผ๏ธ Images & Glyphs", show_label=True, columns=4, height=120, object_fit="contain")
|
| 406 |
+
pdf_gallery = gr.Gallery(label="๐ Imported Scrolls", show_label=True, columns=4, height=120, object_fit="contain")
|
| 407 |
+
|
| 408 |
+
with gr.Group():
|
| 409 |
+
gr.Markdown("### โ๏ธ Transmuted Scrolls (Final PDFs)")
|
| 410 |
+
final_gallery_output = gr.Gallery(label="PDF Previews", show_label=False, elem_id="gallery", columns=2, height=400, object_fit="contain")
|
| 411 |
+
log_output = gr.Markdown(label="Alchemist's Log", value="Your log of successful transmutations will appear here...")
|
| 412 |
+
downloadable_files_output = gr.Files(label="Collect Your Scrolls")
|
| 413 |
+
|
| 414 |
+
# --- API Calls & Examples ---
|
| 415 |
+
font_size_inputs = [font_size_body_slider, font_size_h1_slider, font_size_h2_slider, font_size_h3_slider]
|
| 416 |
+
margin_inputs = [margin_top_slider, margin_bottom_slider, margin_left_slider, margin_right_slider]
|
| 417 |
+
|
| 418 |
+
uploaded_files.upload(update_staging_and_manuscript, inputs=[uploaded_files, ai_content_output], outputs=[text_gallery, image_gallery, pdf_gallery, ai_content_output])
|
| 419 |
+
generate_ai_btn.click(generate_ai_content_api, inputs=[ai_prompt], outputs=[ai_content_output])
|
| 420 |
+
|
| 421 |
+
generate_pdfs_btn.click(generate_pdfs_api,
|
| 422 |
+
inputs=[uploaded_files, ai_content_output, selected_layouts, selected_fonts, num_columns_slider, header_input, footer_input] + font_size_inputs + margin_inputs + [use_svg_engine_toggle],
|
| 423 |
+
outputs=[final_gallery_output, log_output, downloadable_files_output])
|
| 424 |
+
|
| 425 |
+
gr.Examples(examples=[["A technical summary of how alchemy works"],["A short poem about a grumpy gnome"]], inputs=[ai_prompt], outputs=[ai_content_output], fn=generate_ai_content_api, cache_examples=False)
|
| 426 |
+
|
| 427 |
+
if __name__ == "__main__":
|
| 428 |
+
if not EMOJI_FONT_PATH:
|
| 429 |
+
print("\n" + "="*80 + "\nCRITICAL WARNING: 'NotoColorEmoji-Regular.ttf' not found.\nThe application will fail to generate PDFs with color emojis.\n" + "="*80 + "\n")
|
| 430 |
+
demo.launch(debug=True)
|