Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from fpdf import FPDF
|
3 |
+
|
4 |
+
# Function to identify headings and subheadings
|
5 |
+
def process_text(input_text):
|
6 |
+
lines = input_text.split('\n')
|
7 |
+
structured_text = []
|
8 |
+
for line in lines:
|
9 |
+
line = line.strip()
|
10 |
+
if not line:
|
11 |
+
continue
|
12 |
+
if line.isupper():
|
13 |
+
structured_text.append({'type': 'heading', 'text': line})
|
14 |
+
elif line[0].isupper():
|
15 |
+
structured_text.append({'type': 'subheading', 'text': line})
|
16 |
+
else:
|
17 |
+
structured_text.append({'type': 'paragraph', 'text': line})
|
18 |
+
return structured_text
|
19 |
+
|
20 |
+
# Function to generate PDF
|
21 |
+
def generate_pdf(structured_text):
|
22 |
+
pdf = FPDF()
|
23 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
24 |
+
pdf.add_page()
|
25 |
+
pdf.set_font("Arial", size=12)
|
26 |
+
|
27 |
+
for item in structured_text:
|
28 |
+
if item['type'] == 'heading':
|
29 |
+
pdf.set_font("Arial", size=16, style='B')
|
30 |
+
pdf.cell(0, 10, txt=item['text'], ln=True)
|
31 |
+
elif item['type'] == 'subheading':
|
32 |
+
pdf.set_font("Arial", size=14, style='B')
|
33 |
+
pdf.cell(0, 10, txt=item['text'], ln=True)
|
34 |
+
else: # Paragraph or body text
|
35 |
+
pdf.set_font("Arial", size=12)
|
36 |
+
pdf.multi_cell(0, 10, txt=item['text'])
|
37 |
+
|
38 |
+
# Save PDF in-memory
|
39 |
+
return pdf.output(dest='S').encode('latin1')
|
40 |
+
|
41 |
+
# Streamlit app
|
42 |
+
st.title("Intelligent Text Evaluator and PDF Generator")
|
43 |
+
|
44 |
+
st.markdown("""
|
45 |
+
This app identifies headings, subheadings, and paragraphs from the input text and generates a professionally formatted PDF.
|
46 |
+
""")
|
47 |
+
|
48 |
+
# Text input
|
49 |
+
input_text = st.text_area("Enter your text below:", height=300)
|
50 |
+
|
51 |
+
if st.button("Generate PDF"):
|
52 |
+
if input_text.strip():
|
53 |
+
structured_text = process_text(input_text)
|
54 |
+
pdf_data = generate_pdf(structured_text)
|
55 |
+
|
56 |
+
# Download the PDF
|
57 |
+
st.download_button(
|
58 |
+
label="Download PDF",
|
59 |
+
data=pdf_data,
|
60 |
+
file_name="formatted_text.pdf",
|
61 |
+
mime="application/pdf",
|
62 |
+
)
|
63 |
+
else:
|
64 |
+
st.error("Please enter some text before generating the PDF.")
|