-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
166 lines (131 loc) · 5.27 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import streamlit as st
import tempfile
import os
from loguru import logger as log
from accfix.epub import Epub
from accfix.lang import detect_epub_lang
from accfix.ace_fix import ace_fix_mec
import shutil
import telegram
from telegram.error import TelegramError
import asyncio
import dotenv
dotenv.load_dotenv()
def save_uploaded_file(uploaded_file):
with tempfile.NamedTemporaryFile(delete=False, suffix=".epub") as tmp_file:
tmp_file.write(uploaded_file.getbuffer())
return tmp_file.name
def apply_accessibility_fixes(epub):
progress_bar = st.progress(0)
status_text = st.empty()
# Create a container with custom CSS for scrolling
message_container = st.container()
message_container.markdown(
"""
<style>
.scrollable-container {
height: 200px;
overflow-y: auto;
border: 1px solid var(--text-color);
padding: 10px;
background-color: var(--background-color);
color: var(--text-color);
}
</style>
""",
unsafe_allow_html=True,
)
message_area = message_container.empty()
messages = []
for i, message in enumerate(ace_fix_mec(epub), 1):
messages.insert(0, message) # Prepend new messages
status_text.text(message)
message_area.markdown(
f'<div class="scrollable-container">{"<br>".join(messages)}</div>',
unsafe_allow_html=True,
)
progress = min(i / 10, 1.0) # Assuming about 10 main steps
progress_bar.progress(progress)
progress_bar.progress(1.0)
status_text.text("Accessibility fixes completed successfully!")
return epub
def offer_download(fixed_epub, original_filename):
# Save the fixed EPUB to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".epub") as tmp_file:
fixed_epub._zf.close() # Ensure the ZipFile is closed
shutil.copy2(fixed_epub.path, tmp_file.name)
# Log file size for debugging
log.debug(f"Fixed EPUB size: {os.path.getsize(tmp_file.name)} bytes")
# Read the temporary file
with open(tmp_file.name, "rb") as file:
fixed_epub_bytes = file.read()
# Add vertical space before the download button
st.markdown("<br>", unsafe_allow_html=True)
# Offer download
st.download_button(
label="Download Fixed EPUB",
data=fixed_epub_bytes,
file_name=f"fixed_{original_filename}",
mime="application/epub+zip",
)
# Clean up the temporary file
os.unlink(tmp_file.name)
def cleanup(tmp_file_path, epub):
try:
os.unlink(tmp_file_path)
log.info(f"Temporary file {tmp_file_path} deleted successfully")
except Exception as e:
log.warning(f"Failed to delete temporary file {tmp_file_path}: {str(e)}")
if hasattr(epub, "_clone") and epub._clone:
try:
os.unlink(epub._clone)
log.info(f"Cloned file {epub._clone} deleted successfully")
os.rmdir(os.path.dirname(epub._clone))
log.info(f"Temporary directory {os.path.dirname(epub._clone)} deleted successfully")
except Exception as e:
log.warning(f"Failed to delete cloned file or directory: {str(e)}")
async def send_telegram_notification(message, file_path=None):
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN")
chat_id = os.environ.get("TELEGRAM_CHAT_ID")
bot = telegram.Bot(token=bot_token)
try:
if file_path:
with open(file_path, "rb") as file:
await bot.send_document(chat_id=chat_id, document=file, caption=message)
else:
await bot.send_message(chat_id=chat_id, text=message)
except TelegramError as e:
log.error(f"Failed to send Telegram notification: {e}")
def send_telegram_notification_sync(message, file_path=None):
asyncio.run(send_telegram_notification(message, file_path))
def main():
st.title("EPUB Accessibility Fixer (Beta)")
st.subheader("For Fixed Layout EPUBs from [MagicEPUB](https://magicepub.com)")
uploaded_file = st.file_uploader("Upload an EPUB file", type=["epub"])
if uploaded_file is not None:
tmp_file_path = save_uploaded_file(uploaded_file)
epub = None
try:
epub = Epub(tmp_file_path, clone=True)
detected_language = detect_epub_lang(epub)
if detected_language:
st.success(f"Detected language: {detected_language}")
else:
st.warning("Unable to detect the language of the EPUB.")
if st.button("Fix Accessibility"):
fixed_epub = apply_accessibility_fixes(epub)
offer_download(fixed_epub, uploaded_file.name)
send_telegram_notification_sync(
f"File processed successfully: {uploaded_file.name}"
)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
log.exception("Error during EPUB processing")
send_telegram_notification_sync(
f"Error processing file: {uploaded_file.name}\n\nError: {str(e)}", tmp_file_path
)
finally:
if epub:
cleanup(tmp_file_path, epub)
if __name__ == "__main__":
main()