awacke1 commited on
Commit
85a6b6e
·
verified ·
1 Parent(s): 787b7f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -121
app.py CHANGED
@@ -1,134 +1,122 @@
1
  import streamlit as st
2
- import cv2
3
- from PIL import Image
4
- import numpy as np
5
- from imutils.video import VideoStream
6
  import pikepdf
7
- import fpdf
8
  import fitz # pymupdf
9
- import vidgear.gears
10
 
11
- # 🌟🔥 Set up the Streamlit UI with an impressive demo style!
12
- st.title("🚀 Impressive Library Demo Extravaganza")
13
- st.write("Watch these libraries flex their muscles with live demos producing real outputs!")
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # 📜 Demo Functions
16
- def demo_pikepdf():
17
- pdf = pikepdf.Pdf.new()
18
- pdf.pages.append(pikepdf.Page(pikepdf.Dictionary()))
19
- pdf.save("demo_pikepdf.pdf")
20
- return "demo_pikepdf.pdf"
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def demo_fpdf():
23
- pdf = fpdf.FPDF()
24
- pdf.add_page()
25
- pdf.set_font("Arial", size=12)
26
- pdf.cell(200, 10, txt="FPDF rocks!", ln=True)
27
- pdf.output("demo_fpdf.pdf")
28
- return "demo_fpdf.pdf"
 
29
 
30
- def demo_pymupdf():
31
- doc = fitz.open()
32
- page = doc.new_page()
33
- page.insert_text((100, 100), "Pymupdf shines!")
34
- doc.save("demo_pymupdf.pdf")
35
- return "demo_pymupdf.pdf"
 
 
 
 
 
 
36
 
37
- def demo_opencv_image():
38
- cap = cv2.VideoCapture(0)
39
- ret, frame = cap.read()
40
- if ret:
41
- cv2.imwrite("demo_opencv.jpg", frame)
42
- cap.release()
43
- return "demo_opencv.jpg"
44
- cap.release()
45
- return None
46
 
47
- def demo_imutils():
48
- vs = VideoStream(src=0).start()
49
- frame = vs.read()
50
- vs.stop()
51
- if frame is not None:
52
- cv2.imwrite("demo_imutils.jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
53
- return "demo_imutils.jpg"
54
- return None
55
 
56
- def demo_vidgear():
57
- stream = vidgear.gears.CamGear(source=0).start()
58
- frame = stream.read()
59
- stream.stop()
60
- if frame is not None:
61
- cv2.imwrite("demo_vidgear.jpg", frame)
62
- return "demo_vidgear.jpg"
63
- return None
64
 
65
- # 🌟 Main Demo Interface
66
- st.sidebar.header("🎮 Demo Controls")
67
- demo_type = st.sidebar.selectbox("Choose a Demo Category", ["PDF", "Image Capture", "Video Capture"])
 
 
 
 
 
 
 
68
 
69
- # 📜 PDF Demos
70
- if demo_type == "PDF":
71
- st.subheader("📜 PDF Powerhouse")
72
- demo = st.selectbox("Pick a PDF Demo", ["pikepdf", "fpdf2", "pymupdf"])
73
- if st.button("Run PDF Demo"):
74
- with st.spinner("Running demo..."):
75
- try:
76
- if demo == "pikepdf":
77
- result = demo_pikepdf()
78
- st.download_button("Download PDF", open(result, "rb").read(), file_name=result)
79
- elif demo == "fpdf2":
80
- result = demo_fpdf()
81
- st.download_button("Download PDF", open(result, "rb").read(), file_name=result)
82
- elif demo == "pymupdf":
83
- result = demo_pymupdf()
84
- st.download_button("Download PDF", open(result, "rb").read(), file_name=result)
85
- except Exception as e:
86
- st.error(f"Demo failed: {str(e)}")
 
 
 
 
 
87
 
88
- # 📸 Image Capture Demos
89
- elif demo_type == "Image Capture":
90
- st.subheader("📸 Image Snappers")
91
- demo = st.selectbox("Pick an Image Demo", ["opencv-python", "imutils"])
92
- if st.button("Run Image Demo"):
93
- with st.spinner("Capturing image..."):
94
- try:
95
- if demo == "opencv-python":
96
- result = demo_opencv_image()
97
- if result:
98
- st.image(result, caption="Snapped with OpenCV!")
99
- else:
100
- st.error("Camera not detected!")
101
- elif demo == "imutils":
102
- result = demo_imutils()
103
- if result:
104
- st.image(result, caption="Snapped with Imutils!")
105
- else:
106
- st.error("Camera not detected!")
107
- except Exception as e:
108
- st.error(f"Demo failed: {str(e)}")
109
-
110
- # 🎥 Video Capture Demos
111
- elif demo_type == "Video Capture":
112
- st.subheader("🎥 Video Vanguards")
113
- demo = st.selectbox("Pick a Video Demo", ["opencv-python", "vidgear"])
114
- if st.button("Run Video Demo"):
115
- with st.spinner("Capturing video frame..."):
116
- try:
117
- if demo == "opencv-python":
118
- result = demo_opencv_image() # Using image capture as a proxy
119
- if result:
120
- st.image(result, caption="Frame from OpenCV Video!")
121
- else:
122
- st.error("Camera not detected!")
123
- elif demo == "vidgear":
124
- result = demo_vidgear()
125
- if result:
126
- st.image(result, caption="Frame from VidGear Video!")
127
- else:
128
- st.error("Camera not detected!")
129
- except Exception as e:
130
- st.error(f"Demo failed: {str(e)}")
131
-
132
- # 📊 Results Display
133
- st.write("### Results")
134
- st.write("Check out your impressive outputs above!")
 
1
  import streamlit as st
2
+ import base64
3
+ from fpdf import FPDF
 
 
4
  import pikepdf
 
5
  import fitz # pymupdf
 
6
 
7
+ # Define the 12-point ML outline with emojis
8
+ ml_outline = [
9
+ "🌟 1. Mixture of Experts (MoE)",
10
+ "🔥 2. Supervised Fine-Tuning (SFT) using PyTorch",
11
+ "🤖 3. Large Language Models (LLM) using Transformers",
12
+ "📊 4. Self-Rewarding Learning using NPS 0-10 and Verbatims",
13
+ "👍 5. Reinforcement Learning from Human Feedback (RLHF)",
14
+ "🔗 6. MergeKit: Merging Models to Same Embedding Space",
15
+ "📏 7. DistillKit: Model Size Reduction with Spectrum Analysis",
16
+ "🧠 8. Agentic RAG Agents using Document Inputs",
17
+ "⏳ 9. Longitudinal Data Summarization from Multiple Docs",
18
+ "📑 10. Knowledge Extraction using Markdown Knowledge Graphs",
19
+ "🗺️ 11. Knowledge Mapping with Mermaid Diagrams",
20
+ "💻 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
21
+ ]
22
 
23
+ def create_pdf_with_library(library_name, outline_items):
24
+ if library_name == "fpdf":
25
+ pdf = FPDF(orientation='L', unit='mm', format='A4')
26
+ pdf.add_page()
27
+ pdf.set_font("Arial", size=10)
28
+
29
+ # Left page (1-6)
30
+ pdf.set_xy(10, 10)
31
+ pdf.multi_cell(140, 5, "Cutting-Edge ML Areas (1-6)\n\n" + "\n".join(outline_items[:6]))
32
+
33
+ # Right page (7-12)
34
+ pdf.set_xy(150, 10)
35
+ pdf.multi_cell(140, 5, "Cutting-Edge ML Areas (7-12)\n\n" + "\n".join(outline_items[6:]))
36
+
37
+ filename = "ml_outline_fpdf.pdf"
38
+ pdf.output(filename)
39
+ return filename
40
 
41
+ elif library_name == "pikepdf":
42
+ pdf = pikepdf.Pdf.new()
43
+ page = pikepdf.Page(pdf.make_stream(b""))
44
+ # Simplified content for pikepdf
45
+ pdf.pages.append(page)
46
+ filename = "ml_outline_pikepdf.pdf"
47
+ pdf.save(filename)
48
+ return filename
49
 
50
+ elif library_name == "pymupdf":
51
+ doc = fitz.open()
52
+ page = doc.new_page(width=842, height=595) # A4 landscape
53
+ # Left column
54
+ page.insert_text((20, 20), "Cutting-Edge ML Areas (1-6)\n\n" + "\n".join(outline_items[:6]),
55
+ fontsize=10)
56
+ # Right column
57
+ page.insert_text((422, 20), "Cutting-Edge ML Areas (7-12)\n\n" + "\n".join(outline_items[6:]),
58
+ fontsize=10)
59
+ filename = "ml_outline_pymupdf.pdf"
60
+ doc.save(filename)
61
+ return filename
62
 
63
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
64
+ with open(bin_file, 'rb') as f:
65
+ data = f.read()
66
+ bin_str = base64.b64encode(data).decode()
67
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label}</a>'
68
+ return href
 
 
 
69
 
70
+ # Streamlit UI
71
+ st.title("🚀 Cutting-Edge ML Outline Generator")
 
 
 
 
 
 
72
 
73
+ # Two-column layout
74
+ col1, col2 = st.columns(2)
 
 
 
 
 
 
75
 
76
+ with col1:
77
+ st.header("📝 Markdown Outline")
78
+ outline_text = "\n".join(ml_outline)
79
+ st.markdown(outline_text)
80
+
81
+ # Download markdown file
82
+ md_file = "ml_outline.md"
83
+ with open(md_file, "w") as f:
84
+ f.write(outline_text)
85
+ st.markdown(get_binary_file_downloader_html(md_file, "ml_outline.md"), unsafe_allow_html=True)
86
 
87
+ with col2:
88
+ st.header("📑 PDF Preview")
89
+ pdf_library = st.selectbox("Choose PDF Library", ["fpdf", "pikepdf", "pymupdf"])
90
+
91
+ if st.button("Generate PDF"):
92
+ with st.spinner("Generating PDF..."):
93
+ pdf_file = create_pdf_with_library(pdf_library, ml_outline)
94
+
95
+ # Auto-save and display PDF
96
+ with open(pdf_file, "rb") as f:
97
+ pdf_bytes = f.read()
98
+
99
+ st.download_button(
100
+ label="Download PDF",
101
+ data=pdf_bytes,
102
+ file_name=pdf_file,
103
+ mime="application/pdf"
104
+ )
105
+
106
+ # Display PDF
107
+ base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
108
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="400" type="application/pdf"></iframe>'
109
+ st.markdown(pdf_display, unsafe_allow_html=True)
110
 
111
+ # Add some style
112
+ st.markdown("""
113
+ <style>
114
+ .stButton>button {
115
+ background-color: #4CAF50;
116
+ color: white;
117
+ }
118
+ .stSelectbox {
119
+ max-width: 300px;
120
+ }
121
+ </style>
122
+ """, unsafe_allow_html=True)