Spaces:
Sleeping
Sleeping
| 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.") | |