-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
327 lines (307 loc) · 11.3 KB
/
app.py
File metadata and controls
327 lines (307 loc) · 11.3 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import streamlit as st
import pandas as pd
from deep_translator import GoogleTranslator
import PyPDF2
import os
from io import BytesIO
import time
# Theme selection (default to light, allow user toggle)
if "theme" not in st.session_state:
st.session_state.theme = "light" # Default theme
theme = st.session_state.theme
# Theme toggle button
if st.button("Toggle Theme"):
st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
st.rerun() # Refresh the app to apply the new theme
# Dynamic CSS based on theme
st.markdown(
f"""
<style>
.main {{
background-color: {'#ffffff' if theme == 'light' else '#1a1a1a'};
}}
.title {{
font-family: 'Arial', sans-serif;
color: {'#2c3e50' if theme == 'light' else '#ecf0f1'};
text-align: center;
font-size: 36px;
font-weight: bold;
margin-bottom: 10px;
}}
.subtitle {{
font-family: 'Arial', sans-serif;
color: {'#34495e' if theme == 'light' else '#bdc3c7'};
text-align: center;
font-size: 18px;
margin-bottom: 20px;
}}
.upload-box {{
background: linear-gradient(135deg, {'#3498db' if theme == 'light' else '#2980b9'}, {'#8e44ad' if theme == 'light' else '#6c3483'});
padding: 20px;
border-radius: 10px;
color: {'#ffffff' if theme == 'light' else '#ecf0f1'};
font-size: 16px;
text-align: center;
margin-bottom: 20px;
}}
.restrictions {{
font-family: 'Arial', sans-serif;
color: {'#e74c3c' if theme == 'light' else '#e74c3c'};
font-size: 14px;
text-align: center;
margin-bottom: 20px;
}}
.footer {{
text-align: center;
font-family: 'Arial', sans-serif;
color: {'#7f8c8d' if theme == 'light' else '#bdc3c7'};
font-size: 14px;
margin-top: 30px;
padding-top: 10px;
border-top: 1px solid {'#ecf0f1' if theme == 'light' else '#2c3e50'};
}}
.footer a {{
color: {'#3498db' if theme == 'light' else '#3498db'};
text-decoration: none;
font-weight: bold;
}}
.footer a:hover {{
color: {'#2980b9' if theme == 'light' else '#2980b9'};
text-decoration: underline;
}}
.stButton>button {{
background-color: {'#2ecc71' if theme == 'light' else '#27ae60'};
color: {'#ffffff' if theme == 'light' else '#ffffff'};
border-radius: 5px;
font-size: 16px;
padding: 10px 20px;
}}
.stButton>button:hover {{
background-color: {'#27ae60' if theme == 'light' else '#219653'};
color: {'#ffffff' if theme == 'light' else '#ffffff'};
}}
.stSelectbox label {{
color: {'#2c3e50' if theme == 'light' else '#ecf0f1'};
}}
.stFileUploader label {{
color: {'#2c3e50' if theme == 'light' else '#ecf0f1'};
}}
</style>
""",
unsafe_allow_html=True
)
# App header
st.markdown(
f"""
<div class="title">LinguaLens AI Translator</div>
<div class="subtitle">Break language barriers effortlessly – translate your files instantly!</div>
""",
unsafe_allow_html=True
)
# Upload prompt
st.markdown(
f"""
<div class="upload-box">
Upload your file (TXT, PDF, XLSX, CSV) and pick a language to translate it into magic!
</div>
""",
unsafe_allow_html=True
)
# Restrictions
st.markdown(
f"""
<div class="restrictions">
<strong>Note:</strong> Files must be under 1MB. Text content is limited to 5,000 characters per translation request for optimal performance.
</div>
""",
unsafe_allow_html=True
)
# File uploader with fixed label
file = st.file_uploader("Upload File", type=["txt", "pdf", "xlsx", "csv"], label_visibility="collapsed")
MAX_FILE_SIZE = 1 * 1024 * 1024 # 1MB in bytes
# Language selection
languages = {
"English": "en",
"Spanish": "es",
"French": "fr",
"German": "de",
"Italian": "it",
"Chinese (Simplified)": "zh-CN",
"Japanese": "ja",
"Russian": "ru",
"Portuguese": "pt",
"Hindi": "hi"
}
target_lang_name = st.selectbox("Choose your target language", options=list(languages.keys()))
target_lang_code = languages[target_lang_name]
# Initialize translator
translator = GoogleTranslator(source="auto", target=target_lang_code)
# Translation function with retry and error logging
def translate_text(text, translator, max_retries=3):
if not text.strip():
return text
try:
if len(text) > 5000:
chunks = [text[i:i+5000] for i in range(0, len(text), 5000)]
translated = ""
for chunk in chunks:
for attempt in range(max_retries):
try:
translated += translator.translate(chunk)
break
except Exception as e:
st.warning(f"Translation failed for chunk: {str(e)}. Returning original text.")
return text
time.sleep(1)
return translated
for attempt in range(max_retries):
try:
return translator.translate(text)
except Exception as e:
st.warning(f"Translation failed: {str(e)}. Returning original text.")
return text
time.sleep(1)
except Exception as e:
st.warning(f"Unexpected translation error: {str(e)}. Returning original text.")
return text
# Improved file processing functions
def process_txt(file):
try:
content = file.read().decode("utf-8", errors="ignore")
translated = translate_text(content, translator)
return translated.encode("utf-8")
except UnicodeDecodeError:
st.error("Unable to decode text file. Please ensure it’s a valid UTF-8 encoded file.")
return None
def process_pdf(file):
try:
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page_num, page in enumerate(pdf_reader.pages, 1):
page_text = page.extract_text() or ""
if not page_text.strip():
st.warning(f"Page {page_num} contains unextractable text (e.g., scanned images or formatting).")
text += page_text + "\n"
if not text.strip():
st.error("No extractable text found in PDF.")
return None
translated = translate_text(text, translator)
return translated.encode("utf-8")
except Exception as e:
st.error(f"Error processing PDF: {str(e)}")
return None
def process_excel(file):
try:
df = pd.read_excel(file)
if df.empty:
st.error("Excel file is empty.")
return None
batch_size = 100
for col in df.columns: # Process all columns, not just object types
for i in range(0, len(df), batch_size):
start_idx = i
end_idx = min(i + batch_size, len(df))
batch = df[col][start_idx:end_idx]
# Convert to string for translation, preserve non-string types
df.loc[start_idx:end_idx - 1, col] = batch.apply(
lambda x: translate_text(str(x), translator) if pd.notnull(x) and isinstance(x, str) else x
)
output = BytesIO()
df.to_excel(output, index=False)
return output.getvalue()
except Exception as e:
st.error(f"Error processing Excel: {str(e)}")
return None
def process_csv(file):
try:
df = pd.read_csv(file)
if df.empty:
st.error("CSV file is empty.")
return None
batch_size = 100
for col in df.columns: # Process all columns
for i in range(0, len(df), batch_size):
start_idx = i
end_idx = min(i + batch_size, len(df))
batch = df[col][start_idx:end_idx]
df.loc[start_idx:end_idx - 1, col] = batch.apply(
lambda x: translate_text(str(x), translator) if pd.notnull(x) and isinstance(x, str) else x
)
output = BytesIO()
df.to_csv(output, index=False, encoding="utf-8")
return output.getvalue()
except Exception as e:
st.error(f"Error processing CSV: {str(e)}")
return None
# Process uploaded file with improved translation detection
if file and st.button("Translate Now!"):
with st.spinner("Translating your file..."):
try:
file.seek(0, os.SEEK_END)
file_size = file.tell()
file.seek(0)
if file_size > MAX_FILE_SIZE:
st.error(f"File size ({file_size/1024/1024:.2f} MB) exceeds 1MB limit.")
st.stop()
file_ext = file.name.split(".")[-1].lower()
output = None
output_name = f"translated_{file.name}"
if file_ext == "txt":
output = process_txt(file)
mime = "text/plain"
elif file_ext == "pdf":
output = process_pdf(file)
mime = "text/plain"
output_name = output_name.replace(".pdf", ".txt")
elif file_ext == "xlsx":
output = process_excel(file)
mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
elif file_ext == "csv":
output = process_csv(file)
mime = "text/csv"
if output:
translation_occurred = True
if file_ext in ["xlsx", "csv"]:
file.seek(0)
original_df = pd.read_excel(file) if file_ext == "xlsx" else pd.read_csv(file)
output_io = BytesIO(output)
translated_df = pd.read_excel(output_io) if file_ext == "xlsx" else pd.read_csv(output_io)
# Check if any string column changed
for col in original_df.columns:
if original_df[col].dtype == "object":
if not original_df[col].equals(translated_df[col]):
translation_occurred = True
break
else:
translation_occurred = False
elif file_ext in ["txt", "pdf"]:
file.seek(0)
if output == file.read():
translation_occurred = False
if not translation_occurred:
st.success("Returning original file due to translation failure. See warnings above.")
else:
st.success("Translation complete!")
file.seek(0)
st.download_button(
label="Download Translated File",
data=output,
file_name=output_name,
mime=mime
)
else:
st.error("Failed to process file. See warnings above.")
except Exception as e:
st.error(f"Error processing file: {str(e)}")
# Footer
st.markdown(
f"""
<div class="footer">
<h3>Powered by LinguaLens AI</h3><br>
Built by <strong>Adarsh Singh</strong> |
<a href="mailto:adarsh36jnp@gmail.com">Email</a> |
<a href="https://www.linkedin.com/in/adarsh-kumar-singh-data/" target="_blank">LinkedIn</a>
</div>
""",
unsafe_allow_html=True
)