# app.py import streamlit as st import base64 from annotations import analyze_pdf def display_pdf(pdf_bytes): """Displays the PDF in the browser using an embed tag.""" if pdf_bytes and len(pdf_bytes) > 0: # Encode the PDF to base64 base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8') pdf_display = f""" """ st.components.v1.html(pdf_display, height=800, width=700, scrolling=True) else: st.info("No annotated PDF to display.") def main(): st.set_page_config( page_title="PDF Analyzer", page_icon="📄", layout="wide", ) st.title("📄 PDF Analyzer") st.markdown(""" Upload a PDF to analyze its language, highlight errors, and view detailed error reports. """) uploaded_file = st.file_uploader("Upload your PDF file", type=["pdf"]) if uploaded_file is not None: with st.spinner("Analyzing PDF..."): language_results, annotated_pdf = analyze_pdf(uploaded_file) if "error" in language_results: st.error("An error occurred during analysis:") st.code(language_results["error"]) else: st.success("Analysis complete!") # Debugging: Show the size of annotated_pdf if annotated_pdf and len(annotated_pdf) > 0: st.write(f"Annotated PDF size: {len(annotated_pdf)} bytes") else: st.write("Annotated PDF is empty.") # Display the annotated PDF st.subheader("📄 Annotated PDF") display_pdf(annotated_pdf) # Sidebar for error details st.sidebar.header("📝 Error Details") if language_results.get("total_issues", 0) > 0: for idx, issue in enumerate(language_results["issues"], 1): with st.sidebar.expander(f"Issue {idx}"): st.markdown(f"**Message:** {issue['message']}") st.markdown(f"**Category:** {issue['category']}") st.markdown(f"**Suggestions:** {', '.join(issue['suggestions']) if issue['suggestions'] else 'No suggestions'}") st.markdown(f"**Sentence:** {issue['context']}") else: st.sidebar.success("No language issues found!") # Option to download the annotated PDF if annotated_pdf and len(annotated_pdf) > 0: st.download_button( label="📥 Download Annotated PDF", data=annotated_pdf, file_name="annotated.pdf", mime="application/pdf", ) else: st.info("No annotated PDF available for download.") if __name__ == "__main__": main()