Spaces:
Sleeping
Sleeping
File size: 2,666 Bytes
b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d b38050b f09b23d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import streamlit as st
from fpdf import FPDF
import unicodedata
# Function to sanitize text by removing unsupported characters
def sanitize_text(text):
return ''.join(
c for c in unicodedata.normalize('NFKD', text)
if ord(c) < 128 # Retain only ASCII characters
)
# Function to identify headings, subheadings, and paragraphs
def process_text(input_text):
lines = input_text.split('\n')
structured_text = []
for line in lines:
line = line.strip()
if not line:
continue
if line.isupper():
structured_text.append({'type': 'heading', 'text': line})
elif line[0].isupper():
structured_text.append({'type': 'subheading', 'text': line})
else:
structured_text.append({'type': 'paragraph', 'text': line})
return structured_text
# Function to generate a PDF
def generate_pdf(structured_text):
pdf = FPDF()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.add_page()
pdf.set_font("Arial", size=12)
for item in structured_text:
sanitized_text = sanitize_text(item['text'])
if item['type'] == 'heading':
pdf.set_font("Arial", size=16, style='B')
pdf.cell(0, 10, txt=sanitized_text, ln=True)
elif item['type'] == 'subheading':
pdf.set_font("Arial", size=14, style='B')
pdf.cell(0, 10, txt=sanitized_text, ln=True)
else: # Paragraph or body text
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, txt=sanitized_text)
# Save PDF in-memory
return pdf.output(dest='S').encode('latin1')
# Streamlit app
st.title("Intelligent Text Evaluator and PDF Generator")
st.markdown("""
This app identifies headings, subheadings, and paragraphs from the input text and generates a professionally formatted PDF.
""")
# Text input
input_text = st.text_area("Enter your text below:", height=300)
if st.button("Generate PDF"):
if input_text.strip():
# Warn the user if unsupported characters are removed
if any(ord(char) > 255 for char in input_text):
st.warning("Some special characters or emojis have been removed to generate the PDF.")
# Process the input text and generate the PDF
structured_text = process_text(input_text)
pdf_data = generate_pdf(structured_text)
# Download the PDF
st.download_button(
label="Download PDF",
data=pdf_data,
file_name="formatted_text.pdf",
mime="application/pdf",
)
else:
st.error("Please enter some text before generating the PDF.")
|