Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
gr.Markdown("
|
|
|
|
| 35 |
|
| 36 |
with gr.Row():
|
| 37 |
-
with gr.Column(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
file_types=["image"]
|
| 42 |
-
)
|
| 43 |
-
process_btn = gr.Button("Step 2: Process & Zip", variant="primary")
|
| 44 |
|
| 45 |
-
with gr.Column(
|
| 46 |
-
|
| 47 |
-
preview_gallery = gr.Gallery(
|
| 48 |
-
label="Image Preview",
|
| 49 |
-
columns=4,
|
| 50 |
-
height="auto",
|
| 51 |
-
object_fit="contain"
|
| 52 |
-
)
|
| 53 |
-
# The final download button/file
|
| 54 |
-
zip_output = gr.File(label="Step 3: Download ZIP")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
outputs=[zip_output, preview_gallery]
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import base64
|
| 4 |
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
+
def image_to_base64(img):
|
| 8 |
+
buffered = BytesIO()
|
| 9 |
+
# Downscale for mobile brochure feel (max width 600px)
|
| 10 |
+
img.thumbnail((600, 600))
|
| 11 |
+
img.save(buffered, format="PNG")
|
| 12 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
| 13 |
+
|
| 14 |
+
def generate_brochure(image_files, info_file):
|
| 15 |
+
if not image_files or not info_file:
|
| 16 |
+
return None
|
| 17 |
|
| 18 |
+
# 1. Read and split the info text
|
| 19 |
+
with open(info_file.name, 'r') as f:
|
| 20 |
+
text_content = f.read().split('\n\n') # Assumes double new-line separates info
|
| 21 |
|
| 22 |
+
md_content = "# Project Brochure\n\n"
|
| 23 |
|
| 24 |
+
# 2. Logic to group 3 images and 3 text blocks
|
| 25 |
+
# We loop through images in steps of 3
|
| 26 |
+
for i in range(0, len(image_files), 3):
|
| 27 |
+
md_content += '<div style="max-width: 400px; margin: auto; border: 1px solid #ccc; padding: 10px;">\n\n'
|
| 28 |
+
md_content += f"## Page { (i//3) + 1 }\n\n"
|
| 29 |
+
|
| 30 |
+
# Get the next 3 images and next 3 text blocks
|
| 31 |
+
current_images = image_files[i:i+3]
|
| 32 |
+
current_texts = text_content[i:i+3]
|
| 33 |
+
|
| 34 |
+
for img_path, txt in zip(current_images, current_texts):
|
| 35 |
+
img = Image.open(img_path)
|
| 36 |
+
b64 = image_to_base64(img)
|
| 37 |
|
| 38 |
+
# Add to Markdown with HTML for mobile-style sizing
|
| 39 |
+
md_content += f'<img src="data:image/png;base64,{b64}" style="width:100%; border-radius:8px;">\n\n'
|
| 40 |
+
md_content += f"{txt}\n\n---\n\n"
|
| 41 |
+
|
| 42 |
+
md_content += '</div>\n<div style="page-break-after: always;"></div>\n\n'
|
| 43 |
+
|
| 44 |
+
# 3. Save the result
|
| 45 |
+
output_path = "brochure.md"
|
| 46 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 47 |
+
f.write(md_content)
|
| 48 |
+
|
| 49 |
+
return output_path
|
| 50 |
|
| 51 |
+
# --- Gradio UI ---
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("# 📱 Mobile Brochure Generator")
|
| 54 |
+
gr.Markdown("Upload images and an `info.txt` file to generate a self-contained Markdown brochure.")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
img_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 59 |
+
txt_input = gr.File(label="Upload info.txt", file_types=[".txt"])
|
| 60 |
+
gen_btn = gr.Button("Generate Brochure", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
with gr.Column():
|
| 63 |
+
out_file = gr.File(label="Download .md Brochure")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
gen_btn.click(
|
| 66 |
+
fn=generate_brochure,
|
| 67 |
+
inputs=[img_input, txt_input],
|
| 68 |
+
outputs=out_file
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
# Note: Using the new Gradio 6.0+ launch style
|
| 73 |
+
demo.launch(theme=gr.themes.Soft())
|