Spaces:
Sleeping
Sleeping
import streamlit as st | |
import base64 | |
from reportlab.lib.pagesizes import A4 | |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer | |
from reportlab.lib.styles import getSampleStyleSheet | |
from reportlab.lib import colors | |
import pikepdf | |
import fpdf | |
import fitz # pymupdf | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
import io | |
import os | |
# Define the 12-point ML outline with emojis | |
ml_outline = [ | |
"π 1. Mixture of Experts (MoE)", | |
"π₯ 2. Supervised Fine-Tuning (SFT) using PyTorch", | |
"π€ 3. Large Language Models (LLM) using Transformers", | |
"π 4. Self-Rewarding Learning using NPS 0-10 and Verbatims", | |
"π 5. Reinforcement Learning from Human Feedback (RLHF)", | |
"π 6. MergeKit: Merging Models to Same Embedding Space", | |
"π 7. DistillKit: Model Size Reduction with Spectrum Analysis", | |
"π§ 8. Agentic RAG Agents using Document Inputs", | |
"β³ 9. Longitudinal Data Summarization from Multiple Docs", | |
"π 10. Knowledge Extraction using Markdown Knowledge Graphs", | |
"πΊοΈ 11. Knowledge Mapping with Mermaid Diagrams", | |
"π» 12. ML Code Generation with Streamlit/Gradio/HTML5+JS" | |
] | |
# Demo functions for PDF libraries | |
def demo_pikepdf(): | |
"""Create a simple PDF with pikepdf""" | |
pdf = pikepdf.Pdf.new() | |
# Create a proper pikepdf Page object | |
page_dict = pikepdf.Dictionary( | |
Type=pikepdf.Name.Page, | |
MediaBox=[0, 0, 595, 842], | |
Contents=pdf.make_stream(b"BT /F1 12 Tf 100 700 Td (PikePDF Demo) Tj ET") | |
) | |
# Create a proper Pages dictionary | |
pages_dict = pikepdf.Dictionary( | |
Type=pikepdf.Name.Pages, | |
Count=1, | |
Kids=[pdf.make_indirect(page_dict)] | |
) | |
# Set the root | |
pdf.Root.Pages = pdf.make_indirect(pages_dict) | |
# Save to buffer | |
buffer = io.BytesIO() | |
pdf.save(buffer) | |
buffer.seek(0) | |
return buffer.getvalue() | |
def demo_fpdf(): | |
"""Create a simple PDF with fpdf""" | |
pdf = fpdf.FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=12) | |
pdf.cell(200, 10, txt="FPDF Demo", ln=True) | |
buffer = io.BytesIO() | |
pdf.output(buffer) | |
buffer.seek(0) | |
return buffer.getvalue() | |
def demo_pymupdf(): | |
"""Create a simple PDF with pymupdf""" | |
doc = fitz.open() | |
page = doc.new_page() | |
page.insert_text((100, 100), "PyMuPDF Demo") | |
buffer = io.BytesIO() | |
doc.save(buffer) | |
buffer.seek(0) | |
return buffer.getvalue() | |
# Demo function for image capture | |
def demo_image_capture(): | |
"""Generate a demo image (fake capture) since we can't access the camera in this environment""" | |
# Create a simple gradient image using numpy and PIL | |
width, height = 640, 480 | |
# Create a gradient array | |
x = np.linspace(0, 1, width) | |
y = np.linspace(0, 1, height) | |
xx, yy = np.meshgrid(x, y) | |
gradient = (xx + yy) / 2 | |
# Convert to RGB image | |
img_array = (gradient * 255).astype(np.uint8) | |
rgb_array = np.stack([img_array, img_array//2, img_array*2], axis=2) | |
# Create PIL Image | |
img = Image.fromarray(rgb_array) | |
# Add text to the image | |
from PIL import ImageDraw, ImageFont | |
draw = ImageDraw.Draw(img) | |
try: | |
font = ImageFont.truetype("arial.ttf", 30) | |
except: | |
font = ImageFont.load_default() | |
draw.text((width//4, height//2), "OpenCV Demo Image", fill=(255, 255, 255), font=font) | |
# Save to buffer | |
buffer = io.BytesIO() | |
img.save(buffer, format="JPEG") | |
buffer.seek(0) | |
return buffer.getvalue() | |
# Main PDF creation using ReportLab | |
def create_main_pdf(outline_items): | |
"""Create a two-page landscape PDF with the outline split between pages""" | |
buffer = io.BytesIO() | |
doc = SimpleDocTemplate(buffer, pagesize=(A4[1], A4[0])) # Landscape | |
styles = getSampleStyleSheet() | |
story = [] | |
# Title style | |
title_style = styles['Heading1'] | |
title_style.textColor = colors.darkblue | |
# Normal style | |
normal_style = styles['Normal'] | |
normal_style.fontSize = 12 | |
normal_style.leading = 14 | |
# Page 1: Items 1-6 | |
story.append(Paragraph("Cutting-Edge ML Areas (1-6)", title_style)) | |
story.append(Spacer(1, 12)) | |
for item in outline_items[:6]: | |
story.append(Paragraph(item, normal_style)) | |
story.append(Spacer(1, 6)) | |
# Page break | |
story.append(Spacer(1, 500)) # Force new page | |
# Page 2: Items 7-12 | |
story.append(Paragraph("Cutting-Edge ML Areas (7-12)", title_style)) | |
story.append(Spacer(1, 12)) | |
for item in outline_items[6:]: | |
story.append(Paragraph(item, normal_style)) | |
story.append(Spacer(1, 6)) | |
doc.build(story) | |
buffer.seek(0) | |
return buffer.getvalue() | |
def get_binary_file_downloader_html(bin_data, file_label='File'): | |
"""Create a download link for binary data""" | |
bin_str = base64.b64encode(bin_data).decode() | |
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label}</a>' | |
return href | |
# Streamlit UI | |
st.title("π Cutting-Edge ML Outline Generator") | |
col1, col2 = st.columns(2) | |
with col1: | |
st.header("π Markdown Outline") | |
outline_text = "\n".join(ml_outline) | |
st.markdown(outline_text) | |
# Create a download button for the markdown file | |
st.download_button( | |
label="Download Markdown", | |
data=outline_text, | |
file_name="ml_outline.md", | |
mime="text/markdown" | |
) | |
with col2: | |
st.header("π PDF Preview & Demos") | |
# Library Demos | |
st.subheader("Library Demos") | |
if st.button("Run PDF Library Demos"): | |
with st.spinner("Running demos..."): | |
# Create tabs for each demo | |
demo_tabs = st.tabs(["PikePDF", "FPDF", "PyMuPDF", "Image Demo"]) | |
with demo_tabs[0]: | |
# pikepdf demo | |
pike_pdf = demo_pikepdf() | |
st.download_button("Download pikepdf Demo", pike_pdf, "pikepdf_demo.pdf") | |
st.write("PikePDF demo created successfully!") | |
with demo_tabs[1]: | |
# fpdf demo | |
fpdf_pdf = demo_fpdf() | |
st.download_button("Download fpdf Demo", fpdf_pdf, "fpdf_demo.pdf") | |
st.write("FPDF demo created successfully!") | |
with demo_tabs[2]: | |
# pymupdf demo | |
pymupdf_pdf = demo_pymupdf() | |
st.download_button("Download pymupdf Demo", pymupdf_pdf, "pymupdf_demo.pdf") | |
st.write("PyMuPDF demo created successfully!") | |
with demo_tabs[3]: | |
# Image demo | |
img_data = demo_image_capture() | |
st.image(img_data, caption="Demo Image (Camera simulation)") | |
# Main PDF Generation | |
st.subheader("Main Outline PDF") | |
if st.button("Generate Main PDF"): | |
with st.spinner("Generating PDF..."): | |
try: | |
pdf_bytes = create_main_pdf(ml_outline) | |
st.download_button( | |
label="Download Main PDF", | |
data=pdf_bytes, | |
file_name="ml_outline.pdf", | |
mime="application/pdf" | |
) | |
# Display the PDF in the app | |
base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8') | |
pdf_display = f''' | |
<embed | |
src="data:application/pdf;base64,{base64_pdf}" | |
width="100%" | |
height="400px" | |
type="application/pdf"> | |
''' | |
st.markdown(pdf_display, unsafe_allow_html=True) | |
st.success("PDF generated successfully! You can view it above and download it using the button.") | |
except Exception as e: | |
st.error(f"Error generating PDF: {str(e)}") | |
# Add custom CSS for better appearance | |
st.markdown(""" | |
<style> | |
.stButton>button { | |
background-color: #4CAF50; | |
color: white; | |
font-weight: bold; | |
} | |
.stTabs [data-baseweb="tab-list"] { | |
gap: 2px; | |
} | |
.stTabs [data-baseweb="tab"] { | |
height: 50px; | |
white-space: pre-wrap; | |
background-color: #f0f2f6; | |
border-radius: 4px 4px 0px 0px; | |
gap: 1px; | |
padding-top: 10px; | |
padding-bottom: 10px; | |
} | |
</style> | |
""", unsafe_allow_html=True) |