ChBysk commited on
Commit
a7639f7
·
verified ·
1 Parent(s): a466065

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -60
app.py CHANGED
@@ -1,73 +1,57 @@
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())
 
1
  import gradio as gr
2
+ import markdown
3
+ from weasyprint import HTML
4
  import os
 
 
 
5
 
6
+ def convert_md_to_pdf(md_file):
7
+ if md_file is None:
 
 
 
 
 
 
 
8
  return None
9
 
10
+ # 1. Read the Markdown file
11
+ with open(md_file.name, 'r', encoding='utf-8') as f:
12
+ md_text = f.read()
13
 
14
+ # 2. Convert to HTML
15
+ raw_html = markdown.markdown(md_text, extensions=['extra', 'codehilite'])
16
+
17
+ # 3. Add some "Professional" styling
18
+ styled_html = f"""
19
+ <html>
20
+ <head>
21
+ <style>
22
+ body {{ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; padding: 50px; color: #333; }}
23
+ h1 {{ color: #2c3e50; border-bottom: 2px solid #eee; }}
24
+ code {{ background: #f4f4f4; padding: 2px 5px; border-radius: 3px; font-family: monospace; }}
25
+ pre {{ background: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto; }}
26
+ img {{ max-width: 100%; height: auto; }}
27
+ table {{ border-collapse: collapse; width: 100%; margin-bottom: 20px; }}
28
+ th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
29
+ th {{ background-color: #f2f2f2; }}
30
+ </style>
31
+ </head>
32
+ <body>
33
+ {raw_html}
34
+ </body>
35
+ </html>
36
+ """
37
+
38
+ # 4. Generate PDF
39
+ output_path = "document.pdf"
40
+ HTML(string=styled_html).write_pdf(output_path)
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  return output_path
43
 
44
+ # --- UI Layout ---
45
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
46
+ gr.Markdown("# 📄 Professional MD to PDF Converter")
47
+ gr.Markdown("Upload a `.md` file and get a styled PDF document instantly.")
48
 
49
  with gr.Row():
50
+ file_input = gr.File(label="Upload Markdown (.md)", file_types=[".md"])
51
+ file_output = gr.File(label="Download Result (.pdf)")
52
+
53
+ convert_btn = gr.Button("Convert to PDF", variant="primary")
54
+ convert_btn.click(fn=convert_md_to_pdf, inputs=file_input, outputs=file_output)
 
 
 
 
 
 
 
 
55
 
56
  if __name__ == "__main__":
57
+ demo.launch()