-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.py
119 lines (101 loc) · 4.49 KB
/
App.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import logging
import requests
import streamlit as st
from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.enums import TA_JUSTIFY
from dotenv import load_dotenv
# Configure Logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Load Environment Variables
load_dotenv(".env")
class Textify_UI:
def __init__(self) -> None:
"""Initialize the Textify_UI class by loading Environment Variables."""
self.AppName = os.getenv("AppName")
self.AppImgUpload = os.getenv("AppImgUpload")
self.AppImgUploaded = os.getenv("AppImgUploaded")
self.Buffer = BytesIO()
self.ImageToFormat_API = os.getenv("ImageToFormat_API")
def ConvertToPDF(self, text: str) -> bytes:
"""
Converts the provided text into a PDF document.
"""
try:
# Create PDF Document in Memory
pdf = SimpleDocTemplate(self.Buffer, pagesize=letter, title="Document")
# Define Styles for the Document
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justified', alignment=TA_JUSTIFY, fontName='Times-Roman', fontSize=12, leading=24))
# Split Text into Paragraphs and add to Flowable Objects
paragraphs = text.split('\n\n')
flowables = []
for para in paragraphs:
flowables.append(Paragraph(para, styles['Justified']))
flowables.append(Spacer(1, 12))
# Build the PDF Document
pdf.build(flowables)
self.Buffer.seek(0)
return self.Buffer.getvalue()
except Exception as e:
logging.error("An Error Occurred during PDF Generation.", exc_info=True)
raise e
def AppHeader(self) -> None:
"""
Define Main Header.
"""
try:
st.title(self.AppName)
except Exception as e:
logging.error("An Error Occurred while setting the App Header.", exc_info=True)
raise e
def AppSubHeader(self, subtitle: str) -> None:
"""
Define Subheader.
"""
try:
st.subheader(subtitle)
except Exception as e:
logging.error("An Error Occurred while setting the App Subheader.", exc_info=True)
raise e
def run(self) -> None:
"""
Runs the Main Application Logic, Handling Image Uploads and PDF Generation.
"""
try:
# Display the Main Header and Image Upload Prompt
self.AppHeader()
self.AppSubHeader(self.AppImgUpload)
uploaded_file = st.file_uploader("Choose an Image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Uploaded Image and Prompt
self.AppSubHeader(self.AppImgUploaded)
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
# Prepare file for Upload
files = {"file": (uploaded_file.name, uploaded_file, uploaded_file.type)}
upload_button = st.button("Upload Image")
if upload_button:
with st.spinner("Generating PDF..."):
response = requests.post(self.ImageToFormat_API, files=files)
if response.status_code == 200:
# Convert Response Text to PDF
pdf_buffer = self.ConvertToPDF(response.json()['response'])
st.download_button(
label="Download PDF",
data=pdf_buffer,
file_name="Document.pdf",
mime="application/pdf",
)
else:
st.error(f"Failed to Upload Image. Status Code: {response.status_code}")
st.text(response.text)
except Exception:
logging.error("An Error Occurred while running the App.", exc_info=True)
st.error("Failed to Upload Image. Please try Again!")
if __name__ == '__main__':
App = Textify_UI()
App.run()