Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,7 +24,9 @@ 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 |
-
|
|
|
|
|
28 |
|
29 |
# Left page (1-6)
|
30 |
pdf.set_xy(10, 10)
|
@@ -41,7 +43,6 @@ def create_pdf_with_library(library_name, outline_items):
|
|
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)
|
@@ -50,10 +51,8 @@ def create_pdf_with_library(library_name, outline_items):
|
|
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"
|
@@ -64,13 +63,12 @@ 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:
|
@@ -78,7 +76,6 @@ with col1:
|
|
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)
|
@@ -90,25 +87,27 @@ with col2:
|
|
90 |
|
91 |
if st.button("Generate PDF"):
|
92 |
with st.spinner("Generating PDF..."):
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
110 |
|
111 |
-
# Add some style
|
112 |
st.markdown("""
|
113 |
<style>
|
114 |
.stButton>button {
|
|
|
24 |
if library_name == "fpdf":
|
25 |
pdf = FPDF(orientation='L', unit='mm', format='A4')
|
26 |
pdf.add_page()
|
27 |
+
# Add Unicode font support
|
28 |
+
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
|
29 |
+
pdf.set_font('DejaVu', '', 10)
|
30 |
|
31 |
# Left page (1-6)
|
32 |
pdf.set_xy(10, 10)
|
|
|
43 |
elif library_name == "pikepdf":
|
44 |
pdf = pikepdf.Pdf.new()
|
45 |
page = pikepdf.Page(pdf.make_stream(b""))
|
|
|
46 |
pdf.pages.append(page)
|
47 |
filename = "ml_outline_pikepdf.pdf"
|
48 |
pdf.save(filename)
|
|
|
51 |
elif library_name == "pymupdf":
|
52 |
doc = fitz.open()
|
53 |
page = doc.new_page(width=842, height=595) # A4 landscape
|
|
|
54 |
page.insert_text((20, 20), "Cutting-Edge ML Areas (1-6)\n\n" + "\n".join(outline_items[:6]),
|
55 |
fontsize=10)
|
|
|
56 |
page.insert_text((422, 20), "Cutting-Edge ML Areas (7-12)\n\n" + "\n".join(outline_items[6:]),
|
57 |
fontsize=10)
|
58 |
filename = "ml_outline_pymupdf.pdf"
|
|
|
63 |
with open(bin_file, 'rb') as f:
|
64 |
data = f.read()
|
65 |
bin_str = base64.b64encode(data).decode()
|
66 |
+
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label aprovada}</a>'
|
67 |
return href
|
68 |
|
69 |
# Streamlit UI
|
70 |
st.title("🚀 Cutting-Edge ML Outline Generator")
|
71 |
|
|
|
72 |
col1, col2 = st.columns(2)
|
73 |
|
74 |
with col1:
|
|
|
76 |
outline_text = "\n".join(ml_outline)
|
77 |
st.markdown(outline_text)
|
78 |
|
|
|
79 |
md_file = "ml_outline.md"
|
80 |
with open(md_file, "w") as f:
|
81 |
f.write(outline_text)
|
|
|
87 |
|
88 |
if st.button("Generate PDF"):
|
89 |
with st.spinner("Generating PDF..."):
|
90 |
+
try:
|
91 |
+
pdf_file = create_pdf_with_library(pdf_library, ml_outline)
|
92 |
+
|
93 |
+
with open(pdf_file, "rb") as f:
|
94 |
+
pdf_bytes = f.read()
|
95 |
+
|
96 |
+
st.download_button(
|
97 |
+
label="Download PDF",
|
98 |
+
data=pdf_bytes,
|
99 |
+
file_name=pdf_file,
|
100 |
+
mime="application/pdf"
|
101 |
+
)
|
102 |
+
|
103 |
+
base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
|
104 |
+
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="400" type="application/pdf"></iframe>'
|
105 |
+
st.markdown(pdf_display, unsafe_allow_html=True)
|
106 |
+
except FileNotFoundError:
|
107 |
+
st.error("Please download the DejaVuSans.ttf font and place it in the working directory for FPDF Unicode support")
|
108 |
+
except Exception as e:
|
109 |
+
st.error(f"Error generating PDF: {str(e)}")
|
110 |
|
|
|
111 |
st.markdown("""
|
112 |
<style>
|
113 |
.stButton>button {
|