Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,8 @@ from reportlab.lib import colors
|
|
7 |
import io
|
8 |
import re
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
|
13 |
## Core ML Techniques
|
14 |
1. π **Mixture of Experts (MoE)**
|
@@ -77,7 +77,6 @@ ml_markdown = """# Cutting-Edge ML Outline
|
|
77 |
|
78 |
# Process multilevel markdown for PDF output
|
79 |
def markdown_to_pdf_content(markdown_text):
|
80 |
-
"""Convert markdown text to a format suitable for PDF generation"""
|
81 |
lines = markdown_text.strip().split('\n')
|
82 |
pdf_content = []
|
83 |
in_list_item = False
|
@@ -122,9 +121,8 @@ def markdown_to_pdf_content(markdown_text):
|
|
122 |
|
123 |
return left_column, right_column
|
124 |
|
125 |
-
# Main PDF creation
|
126 |
-
def create_main_pdf(markdown_text):
|
127 |
-
"""Create a single-page landscape PDF with the outline in two columns"""
|
128 |
buffer = io.BytesIO()
|
129 |
doc = SimpleDocTemplate(
|
130 |
buffer,
|
@@ -141,12 +139,11 @@ def create_main_pdf(markdown_text):
|
|
141 |
page_height = A4[0] - 72
|
142 |
title_height = 20
|
143 |
spacer_height = 10
|
144 |
-
available_content_height = page_height - title_height - spacer_height
|
145 |
|
146 |
-
# Process columns
|
147 |
left_column, right_column = markdown_to_pdf_content(markdown_text)
|
148 |
|
149 |
-
# Calculate total items
|
150 |
total_items = 0
|
151 |
for col in (left_column, right_column):
|
152 |
for item in col:
|
@@ -156,17 +153,21 @@ def create_main_pdf(markdown_text):
|
|
156 |
else:
|
157 |
total_items += 1
|
158 |
|
159 |
-
#
|
160 |
-
|
|
|
|
|
|
|
161 |
item_font_size = base_font_size
|
162 |
subitem_font_size = base_font_size * 0.9
|
163 |
section_font_size = base_font_size * 1.2
|
|
|
164 |
|
165 |
# Create custom styles
|
166 |
title_style = styles['Heading1']
|
167 |
title_style.textColor = colors.darkblue
|
168 |
-
title_style.alignment = 1
|
169 |
-
title_style.fontSize =
|
170 |
|
171 |
section_style = ParagraphStyle(
|
172 |
'SectionStyle',
|
@@ -259,16 +260,63 @@ def create_main_pdf(markdown_text):
|
|
259 |
# Streamlit UI
|
260 |
st.title("π Cutting-Edge ML Outline Generator")
|
261 |
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import io
|
8 |
import re
|
9 |
|
10 |
+
# Initial markdown content
|
11 |
+
default_markdown = """# Cutting-Edge ML Outline
|
12 |
|
13 |
## Core ML Techniques
|
14 |
1. π **Mixture of Experts (MoE)**
|
|
|
77 |
|
78 |
# Process multilevel markdown for PDF output
|
79 |
def markdown_to_pdf_content(markdown_text):
|
|
|
80 |
lines = markdown_text.strip().split('\n')
|
81 |
pdf_content = []
|
82 |
in_list_item = False
|
|
|
121 |
|
122 |
return left_column, right_column
|
123 |
|
124 |
+
# Main PDF creation with parameterized text sizes
|
125 |
+
def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
|
126 |
buffer = io.BytesIO()
|
127 |
doc = SimpleDocTemplate(
|
128 |
buffer,
|
|
|
139 |
page_height = A4[0] - 72
|
140 |
title_height = 20
|
141 |
spacer_height = 10
|
|
|
142 |
|
143 |
+
# Process columns
|
144 |
left_column, right_column = markdown_to_pdf_content(markdown_text)
|
145 |
|
146 |
+
# Calculate total items for autosizing
|
147 |
total_items = 0
|
148 |
for col in (left_column, right_column):
|
149 |
for item in col:
|
|
|
153 |
else:
|
154 |
total_items += 1
|
155 |
|
156 |
+
# π§ Adjust this multiplier to control autosizing sensitivity
|
157 |
+
if auto_size:
|
158 |
+
base_font_size = max(6, min(12, 200 / total_items)) # Range: 6-12 points
|
159 |
+
|
160 |
+
# π§ Font size parameters - tweak these ratios as needed
|
161 |
item_font_size = base_font_size
|
162 |
subitem_font_size = base_font_size * 0.9
|
163 |
section_font_size = base_font_size * 1.2
|
164 |
+
title_font_size = min(16, base_font_size * 1.5)
|
165 |
|
166 |
# Create custom styles
|
167 |
title_style = styles['Heading1']
|
168 |
title_style.textColor = colors.darkblue
|
169 |
+
title_style.alignment = 1
|
170 |
+
title_style.fontSize = title_font_size
|
171 |
|
172 |
section_style = ParagraphStyle(
|
173 |
'SectionStyle',
|
|
|
260 |
# Streamlit UI
|
261 |
st.title("π Cutting-Edge ML Outline Generator")
|
262 |
|
263 |
+
# Sidebar for settings
|
264 |
+
with st.sidebar:
|
265 |
+
st.header("PDF Settings")
|
266 |
+
auto_size = st.checkbox("Auto-size text", value=True)
|
267 |
+
if not auto_size:
|
268 |
+
base_font_size = st.slider("Base Font Size (points)", min_value=6, max_value=16, value=10, step=1)
|
269 |
+
else:
|
270 |
+
base_font_size = 10 # Default, overridden by auto-sizing
|
271 |
+
# π§ Adjust autosizing formula in create_main_pdf if needed
|
272 |
+
st.info("Font size will auto-adjust between 6-12 points based on content length.")
|
273 |
+
|
274 |
+
# Use session state to persist markdown content
|
275 |
+
if 'markdown_content' not in st.session_state:
|
276 |
+
st.session_state.markdown_content = default_markdown
|
277 |
+
|
278 |
+
# Generate PDF
|
279 |
+
with st.spinner("Generating PDF..."):
|
280 |
+
pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
|
281 |
+
base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
|
282 |
+
|
283 |
+
# Display PDF full-screen
|
284 |
+
st.subheader("PDF Preview")
|
285 |
+
pdf_display = f"""
|
286 |
+
<embed
|
287 |
+
src="data:application/pdf;base64,{base64_pdf}"
|
288 |
+
width="100%"
|
289 |
+
height="600px"
|
290 |
+
type="application/pdf"
|
291 |
+
style="border: none;">
|
292 |
+
"""
|
293 |
+
st.markdown(pdf_display, unsafe_allow_html=True)
|
294 |
+
|
295 |
+
# Download button
|
296 |
+
st.download_button(
|
297 |
+
label="Download PDF",
|
298 |
+
data=pdf_bytes,
|
299 |
+
file_name="ml_outline.pdf",
|
300 |
+
mime="application/pdf"
|
301 |
+
)
|
302 |
+
|
303 |
+
# Markdown editor
|
304 |
+
st.subheader("Edit Markdown Outline")
|
305 |
+
edited_markdown = st.text_area(
|
306 |
+
"Modify the markdown content below:",
|
307 |
+
value=st.session_state.markdown_content,
|
308 |
+
height=300
|
309 |
+
)
|
310 |
+
|
311 |
+
# Update markdown and regenerate PDF on change
|
312 |
+
if st.button("Update PDF"):
|
313 |
+
st.session_state.markdown_content = edited_markdown
|
314 |
+
st.rerun() # Rerun to update PDF with new markdown
|
315 |
+
|
316 |
+
# Save markdown option
|
317 |
+
st.download_button(
|
318 |
+
label="Save Markdown",
|
319 |
+
data=st.session_state.markdown_content,
|
320 |
+
file_name="ml_outline.md",
|
321 |
+
mime="text/markdown"
|
322 |
+
)
|