Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import io
|
|
2 |
import os
|
3 |
import re
|
4 |
import glob
|
|
|
5 |
from datetime import datetime
|
6 |
from pathlib import Path
|
7 |
|
@@ -9,6 +10,7 @@ import streamlit as st
|
|
9 |
import pandas as pd
|
10 |
from PIL import Image
|
11 |
from reportlab.pdfgen import canvas
|
|
|
12 |
from reportlab.lib.utils import ImageReader
|
13 |
import mistune
|
14 |
from gtts import gTTS
|
@@ -24,37 +26,55 @@ def delete_asset(path):
|
|
24 |
st.rerun()
|
25 |
|
26 |
# Tabs setup
|
27 |
-
tab1, tab2 = st.tabs(["π PDF Composer", "π§ͺ
|
28 |
|
29 |
with tab1:
|
30 |
st.header("π PDF Composer & Voice Generator π")
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
md_file = st.file_uploader("Upload Markdown (.md)", type=["md"])
|
33 |
if md_file:
|
34 |
md_text = md_file.getvalue().decode("utf-8")
|
|
|
35 |
else:
|
36 |
md_text = st.text_area("Or enter markdown text directly", height=200)
|
|
|
37 |
|
|
|
38 |
renderer = mistune.HTMLRenderer()
|
39 |
markdown = mistune.create_markdown(renderer=renderer)
|
40 |
html = markdown(md_text or "")
|
41 |
plain_text = re.sub(r'<[^>]+>', '', html)
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
if plain_text.strip():
|
45 |
-
voice_file = f"
|
46 |
-
tts = gTTS(text=plain_text, lang=
|
47 |
tts.save(voice_file)
|
48 |
st.audio(voice_file)
|
|
|
|
|
49 |
else:
|
50 |
st.warning("No text to generate voice from.")
|
51 |
|
|
|
52 |
imgs = st.file_uploader("Upload Images for PDF", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
53 |
ordered_images = []
|
54 |
if imgs:
|
55 |
df_imgs = pd.DataFrame([{"name": f.name, "order": i} for i, f in enumerate(imgs)])
|
56 |
edited = st.data_editor(df_imgs, use_container_width=True)
|
57 |
-
for _, row in edited.sort_values(
|
58 |
for f in imgs:
|
59 |
if f.name == row['name']:
|
60 |
ordered_images.append(f)
|
@@ -63,24 +83,47 @@ with tab1:
|
|
63 |
if st.button("ποΈ Generate PDF with Markdown & Images"):
|
64 |
buf = io.BytesIO()
|
65 |
c = canvas.Canvas(buf)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
for img_f in ordered_images:
|
74 |
try:
|
75 |
img = Image.open(img_f)
|
76 |
w, h = img.size
|
77 |
c.showPage()
|
|
|
78 |
c.drawImage(ImageReader(img), 0, 0, w, h, preserveAspectRatio=True, mask='auto')
|
79 |
except:
|
80 |
continue
|
|
|
81 |
c.save()
|
82 |
buf.seek(0)
|
83 |
-
pdf_name = f"
|
84 |
st.download_button("β¬οΈ Download PDF", data=buf, file_name=pdf_name, mime="application/pdf")
|
85 |
|
86 |
st.markdown("---")
|
@@ -95,6 +138,8 @@ with tab1:
|
|
95 |
cols[1].download_button("π₯", data=fp, file_name=a, mime="application/pdf")
|
96 |
elif ext == 'mp3':
|
97 |
cols[1].audio(a)
|
|
|
|
|
98 |
cols[2].button("ποΈ", key=f"del_{a}", on_click=delete_asset, args=(a,))
|
99 |
|
100 |
with tab2:
|
@@ -123,11 +168,9 @@ with col2:
|
|
123 |
return re.findall(r"```python\s*(.*?)```", md, re.DOTALL)
|
124 |
|
125 |
def execute_code(code: str) -> tuple:
|
126 |
-
buf = io.StringIO()
|
127 |
-
local_vars = {}
|
128 |
try:
|
129 |
-
with redirect_stdout(buf):
|
130 |
-
exec(code, {}, local_vars)
|
131 |
return buf.getvalue(), None
|
132 |
except Exception as e:
|
133 |
return None, str(e)
|
@@ -135,7 +178,6 @@ with col2:
|
|
135 |
up = st.file_uploader("Upload .py or .md", type=['py', 'md'])
|
136 |
if 'code' not in st.session_state:
|
137 |
st.session_state.code = DEFAULT_CODE
|
138 |
-
|
139 |
if up:
|
140 |
text = up.getvalue().decode()
|
141 |
if up.type == 'text/markdown':
|
@@ -158,4 +200,4 @@ with col2:
|
|
158 |
st.success("Executed with no output.")
|
159 |
if c2.button("ποΈ Clear Code"):
|
160 |
st.session_state.code = ''
|
161 |
-
st.rerun()
|
|
|
2 |
import os
|
3 |
import re
|
4 |
import glob
|
5 |
+
import textwrap
|
6 |
from datetime import datetime
|
7 |
from pathlib import Path
|
8 |
|
|
|
10 |
import pandas as pd
|
11 |
from PIL import Image
|
12 |
from reportlab.pdfgen import canvas
|
13 |
+
from reportlab.lib.pagesizes import letter
|
14 |
from reportlab.lib.utils import ImageReader
|
15 |
import mistune
|
16 |
from gtts import gTTS
|
|
|
26 |
st.rerun()
|
27 |
|
28 |
# Tabs setup
|
29 |
+
tab1, tab2 = st.tabs(["π PDF Composer", "π§ͺ Code Interpreter"])
|
30 |
|
31 |
with tab1:
|
32 |
st.header("π PDF Composer & Voice Generator π")
|
33 |
|
34 |
+
# Sidebar PDF text settings
|
35 |
+
columns = st.sidebar.slider("Text columns", 1, 3, 1)
|
36 |
+
font_family = st.sidebar.selectbox("Font", ["Helvetica","Times-Roman","Courier"])
|
37 |
+
font_size = st.sidebar.slider("Font size", 6, 24, 12)
|
38 |
+
|
39 |
+
# Markdown input
|
40 |
md_file = st.file_uploader("Upload Markdown (.md)", type=["md"])
|
41 |
if md_file:
|
42 |
md_text = md_file.getvalue().decode("utf-8")
|
43 |
+
stem = Path(md_file.name).stem
|
44 |
else:
|
45 |
md_text = st.text_area("Or enter markdown text directly", height=200)
|
46 |
+
stem = datetime.now().strftime('%Y%m%d_%H%M%S')
|
47 |
|
48 |
+
# Convert Markdown to plain text
|
49 |
renderer = mistune.HTMLRenderer()
|
50 |
markdown = mistune.create_markdown(renderer=renderer)
|
51 |
html = markdown(md_text or "")
|
52 |
plain_text = re.sub(r'<[^>]+>', '', html)
|
53 |
|
54 |
+
# Voice settings
|
55 |
+
languages = {"English (US)": "en", "English (UK)": "en-uk", "Spanish": "es"}
|
56 |
+
voice_choice = st.selectbox("Voice Language", list(languages.keys()))
|
57 |
+
voice_lang = languages[voice_choice]
|
58 |
+
slow = st.checkbox("Slow Speech")
|
59 |
+
|
60 |
+
if st.button("π Generate & Download Voice MP3 from Text"):
|
61 |
if plain_text.strip():
|
62 |
+
voice_file = f"{stem}.mp3"
|
63 |
+
tts = gTTS(text=plain_text, lang=voice_lang, slow=slow)
|
64 |
tts.save(voice_file)
|
65 |
st.audio(voice_file)
|
66 |
+
with open(voice_file, 'rb') as mp3:
|
67 |
+
st.download_button("π₯ Download MP3", data=mp3, file_name=voice_file, mime="audio/mpeg")
|
68 |
else:
|
69 |
st.warning("No text to generate voice from.")
|
70 |
|
71 |
+
# Image uploads and ordering
|
72 |
imgs = st.file_uploader("Upload Images for PDF", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
73 |
ordered_images = []
|
74 |
if imgs:
|
75 |
df_imgs = pd.DataFrame([{"name": f.name, "order": i} for i, f in enumerate(imgs)])
|
76 |
edited = st.data_editor(df_imgs, use_container_width=True)
|
77 |
+
for _, row in edited.sort_values("order").iterrows():
|
78 |
for f in imgs:
|
79 |
if f.name == row['name']:
|
80 |
ordered_images.append(f)
|
|
|
83 |
if st.button("ποΈ Generate PDF with Markdown & Images"):
|
84 |
buf = io.BytesIO()
|
85 |
c = canvas.Canvas(buf)
|
86 |
+
|
87 |
+
# Render text with columns
|
88 |
+
page_w, page_h = letter
|
89 |
+
margin = 40
|
90 |
+
gutter = 20
|
91 |
+
col_w = (page_w - 2*margin - (columns-1)*gutter) / columns
|
92 |
+
c.setFont(font_family, font_size)
|
93 |
+
line_height = font_size * 1.2
|
94 |
+
col = 0
|
95 |
+
x = margin
|
96 |
+
y = page_h - margin
|
97 |
+
wrap_width = int(col_w / (font_size * 0.6))
|
98 |
+
|
99 |
+
for paragraph in plain_text.split("\n"):
|
100 |
+
for line in textwrap.wrap(paragraph, wrap_width):
|
101 |
+
if y < margin:
|
102 |
+
col += 1
|
103 |
+
if col >= columns:
|
104 |
+
c.showPage()
|
105 |
+
c.setFont(font_family, font_size)
|
106 |
+
col = 0
|
107 |
+
x = margin + col*(col_w+gutter)
|
108 |
+
y = page_h - margin
|
109 |
+
c.drawString(x, y, line)
|
110 |
+
y -= line_height
|
111 |
+
y -= line_height
|
112 |
+
|
113 |
+
# Autosize pages to each image
|
114 |
for img_f in ordered_images:
|
115 |
try:
|
116 |
img = Image.open(img_f)
|
117 |
w, h = img.size
|
118 |
c.showPage()
|
119 |
+
c.setPageSize((w, h))
|
120 |
c.drawImage(ImageReader(img), 0, 0, w, h, preserveAspectRatio=True, mask='auto')
|
121 |
except:
|
122 |
continue
|
123 |
+
|
124 |
c.save()
|
125 |
buf.seek(0)
|
126 |
+
pdf_name = f"{stem}.pdf"
|
127 |
st.download_button("β¬οΈ Download PDF", data=buf, file_name=pdf_name, mime="application/pdf")
|
128 |
|
129 |
st.markdown("---")
|
|
|
138 |
cols[1].download_button("π₯", data=fp, file_name=a, mime="application/pdf")
|
139 |
elif ext == 'mp3':
|
140 |
cols[1].audio(a)
|
141 |
+
with open(a, 'rb') as mp3:
|
142 |
+
cols[1].download_button("π₯", data=mp3, file_name=a, mime="audio/mpeg")
|
143 |
cols[2].button("ποΈ", key=f"del_{a}", on_click=delete_asset, args=(a,))
|
144 |
|
145 |
with tab2:
|
|
|
168 |
return re.findall(r"```python\s*(.*?)```", md, re.DOTALL)
|
169 |
|
170 |
def execute_code(code: str) -> tuple:
|
171 |
+
buf = io.StringIO(); local_vars = {}
|
|
|
172 |
try:
|
173 |
+
with redirect_stdout(buf): exec(code, {}, local_vars)
|
|
|
174 |
return buf.getvalue(), None
|
175 |
except Exception as e:
|
176 |
return None, str(e)
|
|
|
178 |
up = st.file_uploader("Upload .py or .md", type=['py', 'md'])
|
179 |
if 'code' not in st.session_state:
|
180 |
st.session_state.code = DEFAULT_CODE
|
|
|
181 |
if up:
|
182 |
text = up.getvalue().decode()
|
183 |
if up.type == 'text/markdown':
|
|
|
200 |
st.success("Executed with no output.")
|
201 |
if c2.button("ποΈ Clear Code"):
|
202 |
st.session_state.code = ''
|
203 |
+
st.rerun()
|