Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py for Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
|
| 6 |
+
def generate_pdf(names, image_files):
|
| 7 |
+
names_list = [n.strip() for n in names.split(',') if n.strip()]
|
| 8 |
+
|
| 9 |
+
if not image_files:
|
| 10 |
+
return "Please upload at least one image."
|
| 11 |
+
|
| 12 |
+
if len(names_list) != len(image_files):
|
| 13 |
+
return f"Mismatch: {len(names_list)} names but {len(image_files)} images. Please provide matching numbers."
|
| 14 |
+
|
| 15 |
+
pdf = FPDF()
|
| 16 |
+
|
| 17 |
+
for name, img_path in zip(names_list, image_files):
|
| 18 |
+
pdf.add_page()
|
| 19 |
+
pdf.set_font('helvetica', 'B', 16)
|
| 20 |
+
pdf.cell(0, 10, name, ln=1, align='C')
|
| 21 |
+
try:
|
| 22 |
+
pdf.image(img_path, x=50, y=30, w=100) # Adjust positions and size as needed
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error adding image: {str(e)}"
|
| 25 |
+
|
| 26 |
+
output_path = "output.pdf"
|
| 27 |
+
pdf.output(output_path)
|
| 28 |
+
return output_path
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("# PDF Generator App")
|
| 32 |
+
gr.Markdown("Enter comma-separated names and upload corresponding images to generate a PDF.")
|
| 33 |
+
|
| 34 |
+
names_input = gr.Textbox(label="Names (comma-separated)", placeholder="Name1, Name2, Name3")
|
| 35 |
+
images_input = gr.File(label="Upload Images", file_count="multiple", file_types=[".jpg", ".png", ".jpeg"])
|
| 36 |
+
generate_btn = gr.Button("Generate PDF")
|
| 37 |
+
output_file = gr.File(label="Download PDF")
|
| 38 |
+
|
| 39 |
+
generate_btn.click(fn=generate_pdf, inputs=[names_input, images_input], outputs=output_file)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|