awacke1 commited on
Commit
fbe4960
Β·
verified Β·
1 Parent(s): cd13cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +322 -288
app.py CHANGED
@@ -1,134 +1,8 @@
1
- # Streamlit UI
2
- st.title("πŸš€ Cutting-Edge ML Outline Generator")
3
-
4
- col1, col2 = st.columns(2)
5
-
6
- with col1:
7
- st.header("πŸ“ Markdown Outline")
8
-
9
- # Display the markdown content
10
- st.markdown(ml_markdown)
11
-
12
- # Create a download button for the markdown file
13
- st.download_button(
14
- label="Download Markdown",
15
- data=ml_markdown,
16
- file_name="ml_outline.md",
17
- mime="text/markdown"
18
- )
19
-
20
- # Show the markdown source code in an expandable section
21
- with st.expander("View Markdown Source"):
22
- st.code(ml_markdown, language="markdown")
23
-
24
- with col2:
25
- st.header("πŸ“‘ PDF Preview & Demos")
26
-
27
- # Library Demos
28
- st.subheader("Library Demos")
29
- if st.button("Run PDF Library Demos"):
30
- with st.spinner("Running demos..."):
31
- # Create tabs for each demo
32
- demo_tabs = st.tabs(["PikePDF", "FPDF", "PyMuPDF", "Image Demo"])
33
-
34
- with demo_tabs[0]:
35
- # pikepdf demo
36
- pike_pdf = demo_pikepdf()
37
- st.download_button("Download pikepdf Demo", pike_pdf, "pikepdf_demo.pdf")
38
- st.write("PikePDF demo created successfully!")
39
- st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
40
-
41
- with demo_tabs[1]:
42
- # fpdf demo
43
- fpdf_pdf = demo_fpdf()
44
- st.download_button("Download fpdf Demo", fpdf_pdf, "fpdf_demo.pdf")
45
- st.write("FPDF demo created successfully!")
46
- st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
47
-
48
- with demo_tabs[2]:
49
- # pymupdf demo
50
- pymupdf_pdf = demo_pymupdf()
51
- st.download_button("Download pymupdf Demo", pymupdf_pdf, "pymupdf_demo.pdf")
52
- st.write("PyMuPDF demo created successfully!")
53
- st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
54
-
55
- with demo_tabs[3]:
56
- # Image demo
57
- img_data = demo_image_capture()
58
- st.image(img_data, caption="Demo Image (Camera simulation)")
59
-
60
- # Main PDF Generation
61
- st.subheader("Main Outline PDF")
62
- if st.button("Generate Main PDF"):
63
- with st.spinner("Generating PDF..."):
64
- try:
65
- pdf_bytes = create_main_pdf(ml_markdown)
66
-
67
- st.download_button(
68
- label="Download Main PDF",
69
- data=pdf_bytes,
70
- file_name="ml_outline.pdf",
71
- mime="application/pdf"
72
- )
73
-
74
- # Display the PDF in the app
75
- base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
76
- pdf_display = f'''
77
- <embed
78
- src="data:application/pdf;base64,{base64_pdf}"
79
- width="100%"
80
- height="400px"
81
- type="application/pdf">
82
- '''
83
- st.markdown(pdf_display, unsafe_allow_html=True)
84
-
85
- st.success("PDF generated successfully! The PDF displays the multilevel markdown outline in a two-column layout.")
86
- except Exception as e:
87
- st.error(f"Error generating PDF: {str(e)}")
88
-
89
- # Show the PDF rendering code in an expandable section
90
- with st.expander("View PDF Rendering Code"):
91
- st.code("""
92
- # Process multilevel markdown for PDF output
93
- def markdown_to_pdf_content(markdown_text):
94
- # Convert markdown headers to styled text for PDF
95
- lines = markdown_text.strip().split('\\n')
96
- pdf_content = []
97
-
98
- for line in lines:
99
- if line.startswith('# '):
100
- # Main header - will be handled separately
101
- pass
102
- elif line.startswith('## '):
103
- # Section header - add as a bold item
104
- section = line.replace('## ', '').strip()
105
- pdf_content.append(f"<b>{section}</b>")
106
- elif line.startswith('- '):
107
- # List item - add as a normal item
108
- item = line.replace('- ', '').strip()
109
- pdf_content.append(item)
110
-
111
- # Split the content for two columns
112
- mid_point = len(pdf_content) // 2
113
- left_column = pdf_content[:mid_point]
114
- right_column = pdf_content[mid_point:]
115
-
116
- return left_column, right_column
117
- """, language="python")
118
-
119
- # Add custom CSS for better appearance
120
- st.markdown("""
121
- <style>
122
- .stButton>button {
123
- background-color: #4CAF50;
124
- color: white;
125
- font-weight: bold;
126
- }
127
- .stTabsimport streamlit as st
128
  import base64
129
  from reportlab.lib.pagesizes import A4
130
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
131
- from reportlab.lib.styles import getSampleStyleSheet
132
  from reportlab.lib import colors
133
  import pikepdf
134
  import fpdf
@@ -138,91 +12,76 @@ import numpy as np
138
  from PIL import Image
139
  import io
140
  import os
 
141
 
142
  # Define the ML outline as a markdown string for multilevel content
143
  ml_markdown = """# Cutting-Edge ML Outline
144
 
145
  ## Core ML Techniques
146
- - 🌟 **1. Mixture of Experts (MoE)**
147
- - Conditional computation techniques
148
- - Sparse gating mechanisms
149
- - Training specialized sub-models
150
 
151
- - πŸ”₯ **2. Supervised Fine-Tuning (SFT) using PyTorch**
152
- - Loss function customization
153
- - Gradient accumulation strategies
154
- - Learning rate schedulers
155
 
156
- - πŸ€– **3. Large Language Models (LLM) using Transformers**
157
- - Attention mechanisms
158
- - Tokenization strategies
159
- - Position encodings
160
 
161
  ## Training Methods
162
- - πŸ“Š **4. Self-Rewarding Learning using NPS 0-10 and Verbatims**
163
- - Custom reward functions
164
- - Feedback categorization
165
- - Signal extraction from text
166
 
167
- - πŸ‘ **5. Reinforcement Learning from Human Feedback (RLHF)**
168
- - Preference datasets
169
- - PPO implementation
170
- - KL divergence constraints
171
 
172
- - πŸ”— **6. MergeKit: Merging Models to Same Embedding Space**
173
- - TIES merging
174
- - Task arithmetic
175
- - SLERP interpolation
176
 
177
  ## Optimization & Deployment
178
- - πŸ“ **7. DistillKit: Model Size Reduction with Spectrum Analysis**
179
- - Knowledge distillation
180
- - Quantization techniques
181
- - Model pruning strategies
182
 
183
- - 🧠 **8. Agentic RAG Agents using Document Inputs**
184
- - Vector database integration
185
- - Query planning
186
- - Self-reflection mechanisms
187
 
188
- - ⏳ **9. Longitudinal Data Summarization from Multiple Docs**
189
- - Multi-document compression
190
- - Timeline extraction
191
- - Entity tracking
192
 
193
  ## Knowledge Representation
194
- - πŸ“‘ **10. Knowledge Extraction using Markdown Knowledge Graphs**
195
- - Entity recognition
196
- - Relationship mapping
197
- - Hierarchical structuring
198
-
199
- - πŸ—ΊοΈ **11. Knowledge Mapping with Mermaid Diagrams**
200
- - Flowchart generation
201
- - Sequence diagram creation
202
- - State diagrams
203
-
204
- - πŸ’» **12. ML Code Generation with Streamlit/Gradio/HTML5+JS**
205
- - Code completion
206
- - Unit test generation
207
- - Documentation synthesis
208
  """
209
 
210
- # For compatibility with previous code, also maintain the list version
211
- ml_outline = [
212
- "🌟 1. Mixture of Experts (MoE)",
213
- "πŸ”₯ 2. Supervised Fine-Tuning (SFT) using PyTorch",
214
- "πŸ€– 3. Large Language Models (LLM) using Transformers",
215
- "πŸ“Š 4. Self-Rewarding Learning using NPS 0-10 and Verbatims",
216
- "πŸ‘ 5. Reinforcement Learning from Human Feedback (RLHF)",
217
- "πŸ”— 6. MergeKit: Merging Models to Same Embedding Space",
218
- "πŸ“ 7. DistillKit: Model Size Reduction with Spectrum Analysis",
219
- "🧠 8. Agentic RAG Agents using Document Inputs",
220
- "⏳ 9. Longitudinal Data Summarization from Multiple Docs",
221
- "πŸ“‘ 10. Knowledge Extraction using Markdown Knowledge Graphs",
222
- "πŸ—ΊοΈ 11. Knowledge Mapping with Mermaid Diagrams",
223
- "πŸ’» 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
224
- ]
225
-
226
  # Process multilevel markdown for PDF output
227
  def markdown_to_pdf_content(markdown_text):
228
  """Convert markdown text to a format suitable for PDF generation"""
@@ -231,25 +90,49 @@ def markdown_to_pdf_content(markdown_text):
231
  # Convert markdown headers to styled text for PDF
232
  lines = markdown_text.strip().split('\n')
233
  pdf_content = []
 
 
 
234
 
235
  for line in lines:
 
 
 
 
236
  if line.startswith('# '):
237
  # Main header - will be handled separately in the PDF generation
238
  pass
239
  elif line.startswith('## '):
240
  # Section header - add as a bold item
 
 
 
 
 
 
241
  section = line.replace('## ', '').strip()
242
  pdf_content.append(f"<b>{section}</b>")
243
- elif line.startswith('- '):
244
- # List item - add as a normal item
245
- item = line.replace('- ', '').strip()
246
- pdf_content.append(item)
247
- elif line.strip() == '':
248
- # Add a small spacer for empty lines
249
- pass
 
 
 
 
 
 
 
 
 
 
250
 
251
- # Remove empty items
252
- pdf_content = [item for item in pdf_content if item.strip()]
 
253
 
254
  # Split the content for two columns
255
  mid_point = len(pdf_content) // 2
@@ -271,7 +154,8 @@ def demo_pikepdf():
271
  # Set up fonts and colors
272
  title_font = "helv-b"
273
  section_font = "helv-b"
274
- normal_font = "helv"
 
275
  blue_color = (0, 0, 0.8)
276
  black_color = (0, 0, 0)
277
 
@@ -280,25 +164,65 @@ def demo_pikepdf():
280
 
281
  # First column
282
  x1, y1 = 50, 80
283
- for i, item in enumerate(left_column):
284
- if item.startswith('<b>'):
 
 
285
  # Section header
 
 
 
 
286
  text = item.replace('<b>', '').replace('</b>', '')
287
- page.insert_text((x1, y1 + i*25), text, fontname=section_font, fontsize=14, color=blue_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  else:
289
- # Normal item
290
- page.insert_text((x1, y1 + i*25), item, fontname=normal_font, fontsize=11, color=black_color)
 
291
 
292
  # Second column
293
  x2, y2 = 450, 80
294
- for i, item in enumerate(right_column):
295
- if item.startswith('<b>'):
 
 
296
  # Section header
 
 
 
 
297
  text = item.replace('<b>', '').replace('</b>', '')
298
- page.insert_text((x2, y2 + i*25), text, fontname=section_font, fontsize=14, color=blue_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  else:
300
- # Normal item
301
- page.insert_text((x2, y2 + i*25), item, fontname=normal_font, fontsize=11, color=black_color)
 
302
 
303
  # Draw a dividing line
304
  page.draw_line((421, 70), (421, 550))
@@ -330,45 +254,57 @@ def demo_fpdf():
330
  pdf.cell(0, 10, txt="Cutting-Edge ML Outline (FPDF Demo)", ln=True, align='C')
331
  pdf.ln(10)
332
 
333
- # First column
334
  x_col1 = 20
335
- y_start = pdf.get_y()
336
-
337
- for item in left_column:
338
- if item.startswith('<b>'):
339
- # Section header
340
- text = item.replace('<b>', '').replace('</b>', '')
341
- pdf.set_font("Arial", 'B', size=14)
342
- pdf.set_text_color(0, 0, 128) # Dark blue
343
- else:
344
- # Normal item
345
- text = item
346
- pdf.set_font("Arial", size=11)
347
- pdf.set_text_color(0, 0, 0) # Black
348
-
349
- pdf.set_x(x_col1)
350
- pdf.multi_cell(180, 10, txt=text, align='L')
351
- pdf.ln(2)
352
-
353
- # Second column
354
  x_col2 = pdf.w / 2 + 10
355
- pdf.set_y(y_start)
356
 
357
- for item in right_column:
358
- if item.startswith('<b>'):
359
- # Section header
360
- text = item.replace('<b>', '').replace('</b>', '')
361
- pdf.set_font("Arial", 'B', size=14)
362
- pdf.set_text_color(0, 0, 128) # Dark blue
363
- else:
364
- # Normal item
365
- text = item
366
- pdf.set_font("Arial", size=11)
367
- pdf.set_text_color(0, 0, 0) # Black
368
 
369
- pdf.set_x(x_col2)
370
- pdf.multi_cell(180, 10, txt=text, align='L')
371
- pdf.ln(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
 
373
  # Draw a dividing line
374
  pdf.line(pdf.w/2, 30, pdf.w/2, 280)
@@ -389,7 +325,8 @@ def demo_pymupdf():
389
  # Set up fonts and colors
390
  title_font = "helv-b"
391
  section_font = "helv-b"
392
- normal_font = "helv"
 
393
  blue_color = (0, 0, 0.8)
394
  black_color = (0, 0, 0)
395
 
@@ -398,42 +335,65 @@ def demo_pymupdf():
398
 
399
  # First column
400
  x1, y1 = 50, 80
401
- line_height = 25
402
  current_y = y1
403
 
404
  for item in left_column:
405
- if item.startswith('<b>'):
 
406
  # Add extra space before sections (except the first one)
407
  if current_y > y1:
408
  current_y += 10
409
 
410
- # Section header
411
  text = item.replace('<b>', '').replace('</b>', '')
412
  page.insert_text((x1, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  else:
414
- # Normal item
415
- page.insert_text((x1 + 10, current_y), item, fontname=normal_font, fontsize=11, color=black_color)
416
-
417
- current_y += line_height
418
 
419
  # Second column
420
  x2, y2 = 450, 80
421
  current_y = y2
422
 
423
  for item in right_column:
424
- if item.startswith('<b>'):
 
425
  # Add extra space before sections (except the first one)
426
  if current_y > y2:
427
  current_y += 10
428
 
429
- # Section header
430
  text = item.replace('<b>', '').replace('</b>', '')
431
  page.insert_text((x2, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  else:
433
- # Normal item
434
- page.insert_text((x2 + 10, current_y), item, fontname=normal_font, fontsize=11, color=black_color)
435
-
436
- current_y += line_height
437
 
438
  # Draw a dividing line
439
  page.draw_line((421, 70), (421, 550))
@@ -518,7 +478,15 @@ def create_main_pdf(markdown_text):
518
  parent=styles['Normal'],
519
  fontSize=11,
520
  leading=14,
521
- leftIndent=10
 
 
 
 
 
 
 
 
522
  )
523
 
524
  # Add title
@@ -526,31 +494,55 @@ def create_main_pdf(markdown_text):
526
  story.append(Spacer(1, 20))
527
 
528
  # Prepare data for table
529
- table_data = []
530
- max_rows = max(len(left_column), len(right_column))
531
-
532
- # Process left and right columns into paragraphs
533
- left_paragraphs = []
534
  for item in left_column:
535
- if item.startswith('<b>'):
 
536
  text = item.replace('<b>', '').replace('</b>', '')
537
- left_paragraphs.append(Paragraph(text, section_style))
 
 
 
 
 
 
 
 
538
  else:
539
- left_paragraphs.append(Paragraph(item, item_style))
 
540
 
541
- right_paragraphs = []
542
  for item in right_column:
543
- if item.startswith('<b>'):
 
544
  text = item.replace('<b>', '').replace('</b>', '')
545
- right_paragraphs.append(Paragraph(text, section_style))
 
 
 
 
 
 
 
 
546
  else:
547
- right_paragraphs.append(Paragraph(item, item_style))
548
-
549
- # Create one row per paragraph
550
- for i in range(max_rows):
551
- left_p = left_paragraphs[i] if i < len(left_paragraphs) else ""
552
- right_p = right_paragraphs[i] if i < len(right_paragraphs) else ""
553
- table_data.append([left_p, right_p])
 
 
 
 
 
 
 
 
 
554
 
555
  # Calculate column widths
556
  col_width = (A4[1] - 120) / 2.0 # Page width minus margins divided by 2
@@ -587,16 +579,21 @@ col1, col2 = st.columns(2)
587
 
588
  with col1:
589
  st.header("πŸ“ Markdown Outline")
590
- outline_text = "\n".join(ml_outline)
591
- st.markdown(outline_text)
 
592
 
593
  # Create a download button for the markdown file
594
  st.download_button(
595
  label="Download Markdown",
596
- data=outline_text,
597
  file_name="ml_outline.md",
598
  mime="text/markdown"
599
  )
 
 
 
 
600
 
601
  with col2:
602
  st.header("πŸ“‘ PDF Preview & Demos")
@@ -613,18 +610,21 @@ with col2:
613
  pike_pdf = demo_pikepdf()
614
  st.download_button("Download pikepdf Demo", pike_pdf, "pikepdf_demo.pdf")
615
  st.write("PikePDF demo created successfully!")
 
616
 
617
  with demo_tabs[1]:
618
  # fpdf demo
619
  fpdf_pdf = demo_fpdf()
620
  st.download_button("Download fpdf Demo", fpdf_pdf, "fpdf_demo.pdf")
621
  st.write("FPDF demo created successfully!")
 
622
 
623
  with demo_tabs[2]:
624
  # pymupdf demo
625
  pymupdf_pdf = demo_pymupdf()
626
  st.download_button("Download pymupdf Demo", pymupdf_pdf, "pymupdf_demo.pdf")
627
  st.write("PyMuPDF demo created successfully!")
 
628
 
629
  with demo_tabs[3]:
630
  # Image demo
@@ -636,7 +636,7 @@ with col2:
636
  if st.button("Generate Main PDF"):
637
  with st.spinner("Generating PDF..."):
638
  try:
639
- pdf_bytes = create_main_pdf(ml_outline)
640
 
641
  st.download_button(
642
  label="Download Main PDF",
@@ -656,10 +656,44 @@ with col2:
656
  '''
657
  st.markdown(pdf_display, unsafe_allow_html=True)
658
 
659
- st.success("PDF generated successfully! You can view it above and download it using the button.")
660
  except Exception as e:
661
  st.error(f"Error generating PDF: {str(e)}")
662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
663
  # Add custom CSS for better appearance
664
  st.markdown("""
665
  <style>
 
1
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import base64
3
  from reportlab.lib.pagesizes import A4
4
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
5
+ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
6
  from reportlab.lib import colors
7
  import pikepdf
8
  import fpdf
 
12
  from PIL import Image
13
  import io
14
  import os
15
+ import re
16
 
17
  # Define the ML outline as a markdown string for multilevel content
18
  ml_markdown = """# Cutting-Edge ML Outline
19
 
20
  ## Core ML Techniques
21
+ 1. 🌟 **Mixture of Experts (MoE)**
22
+ - Conditional computation techniques
23
+ - Sparse gating mechanisms
24
+ - Training specialized sub-models
25
 
26
+ 2. πŸ”₯ **Supervised Fine-Tuning (SFT) using PyTorch**
27
+ - Loss function customization
28
+ - Gradient accumulation strategies
29
+ - Learning rate schedulers
30
 
31
+ 3. πŸ€– **Large Language Models (LLM) using Transformers**
32
+ - Attention mechanisms
33
+ - Tokenization strategies
34
+ - Position encodings
35
 
36
  ## Training Methods
37
+ 4. πŸ“Š **Self-Rewarding Learning using NPS 0-10 and Verbatims**
38
+ - Custom reward functions
39
+ - Feedback categorization
40
+ - Signal extraction from text
41
 
42
+ 5. πŸ‘ **Reinforcement Learning from Human Feedback (RLHF)**
43
+ - Preference datasets
44
+ - PPO implementation
45
+ - KL divergence constraints
46
 
47
+ 6. πŸ”— **MergeKit: Merging Models to Same Embedding Space**
48
+ - TIES merging
49
+ - Task arithmetic
50
+ - SLERP interpolation
51
 
52
  ## Optimization & Deployment
53
+ 7. πŸ“ **DistillKit: Model Size Reduction with Spectrum Analysis**
54
+ - Knowledge distillation
55
+ - Quantization techniques
56
+ - Model pruning strategies
57
 
58
+ 8. 🧠 **Agentic RAG Agents using Document Inputs**
59
+ - Vector database integration
60
+ - Query planning
61
+ - Self-reflection mechanisms
62
 
63
+ 9. ⏳ **Longitudinal Data Summarization from Multiple Docs**
64
+ - Multi-document compression
65
+ - Timeline extraction
66
+ - Entity tracking
67
 
68
  ## Knowledge Representation
69
+ 10. πŸ“‘ **Knowledge Extraction using Markdown Knowledge Graphs**
70
+ - Entity recognition
71
+ - Relationship mapping
72
+ - Hierarchical structuring
73
+
74
+ 11. πŸ—ΊοΈ **Knowledge Mapping with Mermaid Diagrams**
75
+ - Flowchart generation
76
+ - Sequence diagram creation
77
+ - State diagrams
78
+
79
+ 12. πŸ’» **ML Code Generation with Streamlit/Gradio/HTML5+JS**
80
+ - Code completion
81
+ - Unit test generation
82
+ - Documentation synthesis
83
  """
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # Process multilevel markdown for PDF output
86
  def markdown_to_pdf_content(markdown_text):
87
  """Convert markdown text to a format suitable for PDF generation"""
 
90
  # Convert markdown headers to styled text for PDF
91
  lines = markdown_text.strip().split('\n')
92
  pdf_content = []
93
+ in_list_item = False
94
+ current_item = None
95
+ sub_items = []
96
 
97
  for line in lines:
98
+ line = line.strip()
99
+ if not line:
100
+ continue
101
+
102
  if line.startswith('# '):
103
  # Main header - will be handled separately in the PDF generation
104
  pass
105
  elif line.startswith('## '):
106
  # Section header - add as a bold item
107
+ if current_item and sub_items:
108
+ # Store the previous item with its sub-items
109
+ pdf_content.append([current_item, sub_items])
110
+ sub_items = []
111
+ current_item = None
112
+
113
  section = line.replace('## ', '').strip()
114
  pdf_content.append(f"<b>{section}</b>")
115
+ in_list_item = False
116
+ elif re.match(r'^\d+\.', line):
117
+ # Numbered list item
118
+ if current_item and sub_items:
119
+ # Store the previous item with its sub-items
120
+ pdf_content.append([current_item, sub_items])
121
+ sub_items = []
122
+
123
+ current_item = line.strip()
124
+ in_list_item = True
125
+ elif line.startswith('- ') and in_list_item:
126
+ # Sub-item under a numbered list item
127
+ sub_items.append(line.strip())
128
+ else:
129
+ # Regular line
130
+ if not in_list_item:
131
+ pdf_content.append(line.strip())
132
 
133
+ # Add the last item if there is one
134
+ if current_item and sub_items:
135
+ pdf_content.append([current_item, sub_items])
136
 
137
  # Split the content for two columns
138
  mid_point = len(pdf_content) // 2
 
154
  # Set up fonts and colors
155
  title_font = "helv-b"
156
  section_font = "helv-b"
157
+ item_font = "helv-b"
158
+ subitem_font = "helv"
159
  blue_color = (0, 0, 0.8)
160
  black_color = (0, 0, 0)
161
 
 
164
 
165
  # First column
166
  x1, y1 = 50, 80
167
+ current_y = y1
168
+
169
+ for item in left_column:
170
+ if isinstance(item, str) and item.startswith('<b>'):
171
  # Section header
172
+ # Add extra space before sections (except the first one)
173
+ if current_y > y1:
174
+ current_y += 10
175
+
176
  text = item.replace('<b>', '').replace('</b>', '')
177
+ page.insert_text((x1, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
178
+ current_y += 25
179
+ elif isinstance(item, list):
180
+ # Main item with sub-items
181
+ main_item, sub_items = item
182
+ page.insert_text((x1, current_y), main_item, fontname=item_font, fontsize=12, color=black_color)
183
+ current_y += 20
184
+
185
+ # Add sub-items
186
+ for sub_item in sub_items:
187
+ page.insert_text((x1 + 20, current_y), sub_item, fontname=subitem_font, fontsize=10, color=black_color)
188
+ current_y += 15
189
+
190
+ current_y += 5 # Extra space after a group
191
  else:
192
+ # Regular item
193
+ page.insert_text((x1, current_y), item, fontname=item_font, fontsize=12, color=black_color)
194
+ current_y += 20
195
 
196
  # Second column
197
  x2, y2 = 450, 80
198
+ current_y = y2
199
+
200
+ for item in right_column:
201
+ if isinstance(item, str) and item.startswith('<b>'):
202
  # Section header
203
+ # Add extra space before sections (except the first one)
204
+ if current_y > y2:
205
+ current_y += 10
206
+
207
  text = item.replace('<b>', '').replace('</b>', '')
208
+ page.insert_text((x2, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
209
+ current_y += 25
210
+ elif isinstance(item, list):
211
+ # Main item with sub-items
212
+ main_item, sub_items = item
213
+ page.insert_text((x2, current_y), main_item, fontname=item_font, fontsize=12, color=black_color)
214
+ current_y += 20
215
+
216
+ # Add sub-items
217
+ for sub_item in sub_items:
218
+ page.insert_text((x2 + 20, current_y), sub_item, fontname=subitem_font, fontsize=10, color=black_color)
219
+ current_y += 15
220
+
221
+ current_y += 5 # Extra space after a group
222
  else:
223
+ # Regular item
224
+ page.insert_text((x2, current_y), item, fontname=item_font, fontsize=12, color=black_color)
225
+ current_y += 20
226
 
227
  # Draw a dividing line
228
  page.draw_line((421, 70), (421, 550))
 
254
  pdf.cell(0, 10, txt="Cutting-Edge ML Outline (FPDF Demo)", ln=True, align='C')
255
  pdf.ln(10)
256
 
257
+ # Define coordinates for columns
258
  x_col1 = 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  x_col2 = pdf.w / 2 + 10
260
+ y_start = pdf.get_y()
261
 
262
+ # Function to render a column
263
+ def render_column(items, x_start, y_start):
264
+ y_pos = y_start
 
 
 
 
 
 
 
 
265
 
266
+ for item in items:
267
+ if isinstance(item, str) and item.startswith('<b>'):
268
+ # Section header
269
+ text = item.replace('<b>', '').replace('</b>', '')
270
+ pdf.set_font("Arial", 'B', size=14)
271
+ pdf.set_text_color(0, 0, 128) # Dark blue
272
+ pdf.set_xy(x_start, y_pos)
273
+ pdf.cell(0, 10, txt=text, ln=True)
274
+ y_pos += 10
275
+ elif isinstance(item, list):
276
+ # Main item with sub-items
277
+ main_item, sub_items = item
278
+
279
+ # Main item
280
+ pdf.set_font("Arial", 'B', size=11)
281
+ pdf.set_text_color(0, 0, 0) # Black
282
+ pdf.set_xy(x_start, y_pos)
283
+ pdf.multi_cell(180, 6, txt=main_item, align='L')
284
+ main_height = pdf.get_y() - y_pos
285
+ y_pos += main_height + 2
286
+
287
+ # Sub-items
288
+ pdf.set_font("Arial", size=10)
289
+ for sub_item in sub_items:
290
+ pdf.set_xy(x_start + 10, y_pos)
291
+ pdf.multi_cell(170, 5, txt=sub_item, align='L')
292
+ sub_height = pdf.get_y() - y_pos
293
+ y_pos += sub_height + 1
294
+
295
+ y_pos += 2 # Extra space after a group
296
+ else:
297
+ # Regular item
298
+ pdf.set_font("Arial", 'B', size=11)
299
+ pdf.set_text_color(0, 0, 0) # Black
300
+ pdf.set_xy(x_start, y_pos)
301
+ pdf.multi_cell(180, 6, txt=item, align='L')
302
+ item_height = pdf.get_y() - y_pos
303
+ y_pos += item_height + 4
304
+
305
+ # Render both columns
306
+ render_column(left_column, x_col1, y_start)
307
+ render_column(right_column, x_col2, y_start)
308
 
309
  # Draw a dividing line
310
  pdf.line(pdf.w/2, 30, pdf.w/2, 280)
 
325
  # Set up fonts and colors
326
  title_font = "helv-b"
327
  section_font = "helv-b"
328
+ item_font = "helv-b"
329
+ subitem_font = "helv"
330
  blue_color = (0, 0, 0.8)
331
  black_color = (0, 0, 0)
332
 
 
335
 
336
  # First column
337
  x1, y1 = 50, 80
 
338
  current_y = y1
339
 
340
  for item in left_column:
341
+ if isinstance(item, str) and item.startswith('<b>'):
342
+ # Section header
343
  # Add extra space before sections (except the first one)
344
  if current_y > y1:
345
  current_y += 10
346
 
 
347
  text = item.replace('<b>', '').replace('</b>', '')
348
  page.insert_text((x1, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
349
+ current_y += 25
350
+ elif isinstance(item, list):
351
+ # Main item with sub-items
352
+ main_item, sub_items = item
353
+ page.insert_text((x1, current_y), main_item, fontname=item_font, fontsize=12, color=black_color)
354
+ current_y += 20
355
+
356
+ # Add sub-items
357
+ for sub_item in sub_items:
358
+ page.insert_text((x1 + 20, current_y), sub_item, fontname=subitem_font, fontsize=10, color=black_color)
359
+ current_y += 15
360
+
361
+ current_y += 5 # Extra space after a group
362
  else:
363
+ # Regular item
364
+ page.insert_text((x1, current_y), item, fontname=item_font, fontsize=12, color=black_color)
365
+ current_y += 20
 
366
 
367
  # Second column
368
  x2, y2 = 450, 80
369
  current_y = y2
370
 
371
  for item in right_column:
372
+ if isinstance(item, str) and item.startswith('<b>'):
373
+ # Section header
374
  # Add extra space before sections (except the first one)
375
  if current_y > y2:
376
  current_y += 10
377
 
 
378
  text = item.replace('<b>', '').replace('</b>', '')
379
  page.insert_text((x2, current_y), text, fontname=section_font, fontsize=14, color=blue_color)
380
+ current_y += 25
381
+ elif isinstance(item, list):
382
+ # Main item with sub-items
383
+ main_item, sub_items = item
384
+ page.insert_text((x2, current_y), main_item, fontname=item_font, fontsize=12, color=black_color)
385
+ current_y += 20
386
+
387
+ # Add sub-items
388
+ for sub_item in sub_items:
389
+ page.insert_text((x2 + 20, current_y), sub_item, fontname=subitem_font, fontsize=10, color=black_color)
390
+ current_y += 15
391
+
392
+ current_y += 5 # Extra space after a group
393
  else:
394
+ # Regular item
395
+ page.insert_text((x2, current_y), item, fontname=item_font, fontsize=12, color=black_color)
396
+ current_y += 20
 
397
 
398
  # Draw a dividing line
399
  page.draw_line((421, 70), (421, 550))
 
478
  parent=styles['Normal'],
479
  fontSize=11,
480
  leading=14,
481
+ fontName='Helvetica-Bold'
482
+ )
483
+
484
+ subitem_style = ParagraphStyle(
485
+ 'SubItemStyle',
486
+ parent=styles['Normal'],
487
+ fontSize=10,
488
+ leading=12,
489
+ leftIndent=20
490
  )
491
 
492
  # Add title
 
494
  story.append(Spacer(1, 20))
495
 
496
  # Prepare data for table
497
+ left_cells = []
 
 
 
 
498
  for item in left_column:
499
+ if isinstance(item, str) and item.startswith('<b>'):
500
+ # Section header
501
  text = item.replace('<b>', '').replace('</b>', '')
502
+ left_cells.append(Paragraph(text, section_style))
503
+ elif isinstance(item, list):
504
+ # Main item with sub-items
505
+ main_item, sub_items = item
506
+ left_cells.append(Paragraph(main_item, item_style))
507
+
508
+ # Sub items
509
+ for sub_item in sub_items:
510
+ left_cells.append(Paragraph(sub_item, subitem_style))
511
  else:
512
+ # Regular item
513
+ left_cells.append(Paragraph(item, item_style))
514
 
515
+ right_cells = []
516
  for item in right_column:
517
+ if isinstance(item, str) and item.startswith('<b>'):
518
+ # Section header
519
  text = item.replace('<b>', '').replace('</b>', '')
520
+ right_cells.append(Paragraph(text, section_style))
521
+ elif isinstance(item, list):
522
+ # Main item with sub-items
523
+ main_item, sub_items = item
524
+ right_cells.append(Paragraph(main_item, item_style))
525
+
526
+ # Sub items
527
+ for sub_item in sub_items:
528
+ right_cells.append(Paragraph(sub_item, subitem_style))
529
  else:
530
+ # Regular item
531
+ right_cells.append(Paragraph(item, item_style))
532
+
533
+ # Make sure both columns have the same number of rows by adding empty cells
534
+ max_cells = max(len(left_cells), len(right_cells))
535
+ if len(left_cells) < max_cells:
536
+ for i in range(max_cells - len(left_cells)):
537
+ left_cells.append("")
538
+ if len(right_cells) < max_cells:
539
+ for i in range(max_cells - len(right_cells)):
540
+ right_cells.append("")
541
+
542
+ # Create table data (one row per cell)
543
+ table_data = []
544
+ for i in range(max_cells):
545
+ table_data.append([left_cells[i], right_cells[i]])
546
 
547
  # Calculate column widths
548
  col_width = (A4[1] - 120) / 2.0 # Page width minus margins divided by 2
 
579
 
580
  with col1:
581
  st.header("πŸ“ Markdown Outline")
582
+
583
+ # Display the markdown content
584
+ st.markdown(ml_markdown)
585
 
586
  # Create a download button for the markdown file
587
  st.download_button(
588
  label="Download Markdown",
589
+ data=ml_markdown,
590
  file_name="ml_outline.md",
591
  mime="text/markdown"
592
  )
593
+
594
+ # Show the markdown source code in an expandable section
595
+ with st.expander("View Markdown Source"):
596
+ st.code(ml_markdown, language="markdown")
597
 
598
  with col2:
599
  st.header("πŸ“‘ PDF Preview & Demos")
 
610
  pike_pdf = demo_pikepdf()
611
  st.download_button("Download pikepdf Demo", pike_pdf, "pikepdf_demo.pdf")
612
  st.write("PikePDF demo created successfully!")
613
+ st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
614
 
615
  with demo_tabs[1]:
616
  # fpdf demo
617
  fpdf_pdf = demo_fpdf()
618
  st.download_button("Download fpdf Demo", fpdf_pdf, "fpdf_demo.pdf")
619
  st.write("FPDF demo created successfully!")
620
+ st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
621
 
622
  with demo_tabs[2]:
623
  # pymupdf demo
624
  pymupdf_pdf = demo_pymupdf()
625
  st.download_button("Download pymupdf Demo", pymupdf_pdf, "pymupdf_demo.pdf")
626
  st.write("PyMuPDF demo created successfully!")
627
+ st.info("This PDF contains the multilevel markdown outline in a two-column layout.")
628
 
629
  with demo_tabs[3]:
630
  # Image demo
 
636
  if st.button("Generate Main PDF"):
637
  with st.spinner("Generating PDF..."):
638
  try:
639
+ pdf_bytes = create_main_pdf(ml_markdown)
640
 
641
  st.download_button(
642
  label="Download Main PDF",
 
656
  '''
657
  st.markdown(pdf_display, unsafe_allow_html=True)
658
 
659
+ st.success("PDF generated successfully! The PDF displays the multilevel markdown outline in a two-column layout.")
660
  except Exception as e:
661
  st.error(f"Error generating PDF: {str(e)}")
662
 
663
+ # Show the PDF rendering code in an expandable section
664
+ with st.expander("View PDF Rendering Code"):
665
+ st.code("""
666
+ # Process multilevel markdown for PDF output
667
+ def markdown_to_pdf_content(markdown_text):
668
+ # Convert markdown headers to styled text for PDF
669
+ lines = markdown_text.strip().split('\\n')
670
+ pdf_content = []
671
+
672
+ for line in lines:
673
+ if line.startswith('# '):
674
+ # Main header - will be handled separately
675
+ pass
676
+ elif line.startswith('## '):
677
+ # Section header - add as a bold item
678
+ section = line.replace('## ', '').strip()
679
+ pdf_content.append(f"<b>{section}</b>")
680
+ elif re.match(r'^\\d+\\.', line):
681
+ # Numbered list item
682
+ item = line.strip()
683
+ pdf_content.append(item)
684
+ elif line.startswith('- '):
685
+ # Sub-item under a numbered list item
686
+ sub_item = line.strip()
687
+ pdf_content.append(" " + sub_item)
688
+
689
+ # Split the content for two columns
690
+ mid_point = len(pdf_content) // 2
691
+ left_column = pdf_content[:mid_point]
692
+ right_column = pdf_content[mid_point:]
693
+
694
+ return left_column, right_column
695
+ """, language="python")
696
+
697
  # Add custom CSS for better appearance
698
  st.markdown("""
699
  <style>