Update app.py
Browse files
app.py
CHANGED
@@ -7,12 +7,6 @@ import re
|
|
7 |
import torch
|
8 |
from datetime import datetime
|
9 |
import pytz
|
10 |
-
from reportlab.pdfgen import canvas
|
11 |
-
from reportlab.pdfbase import pdfmetrics
|
12 |
-
from reportlab.pdfbase.ttfonts import TTFont
|
13 |
-
from reportlab.lib.pagesizes import A4
|
14 |
-
from reportlab.lib.units import inch
|
15 |
-
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
|
16 |
from io import BytesIO
|
17 |
from docx import Document
|
18 |
import tempfile
|
@@ -303,7 +297,7 @@ def translate_text(text, src_lang, tgt_lang):
|
|
303 |
return f"Error during translation: {str(e)}"
|
304 |
|
305 |
# Document translation function
|
306 |
-
def translate_document(file, source_lang, target_lang
|
307 |
try:
|
308 |
if file is None:
|
309 |
return "Please upload a file", None
|
@@ -318,30 +312,24 @@ def translate_document(file, source_lang, target_lang, output_format="txt"):
|
|
318 |
# Translate
|
319 |
translated_text = translate_text(input_text, source_lang, target_lang)
|
320 |
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
# Create temporary file
|
322 |
temp_dir = tempfile.gettempdir()
|
|
|
323 |
|
324 |
-
#
|
325 |
if input_ext == '.pdf':
|
326 |
-
#
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
doc = Document()
|
331 |
-
for paragraph in translated_text.split('\n'):
|
332 |
-
if paragraph.strip():
|
333 |
-
doc.add_paragraph(paragraph)
|
334 |
-
doc.save(output_path)
|
335 |
-
else: # default to txt
|
336 |
-
output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}.txt"
|
337 |
-
output_path = os.path.join(temp_dir, output_filename)
|
338 |
-
with open(output_path, 'w', encoding='utf-8') as f:
|
339 |
-
f.write(translated_text)
|
340 |
-
|
341 |
elif input_ext == '.docx':
|
342 |
# Create DOCX
|
343 |
-
output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}.docx"
|
344 |
-
output_path = os.path.join(temp_dir, output_filename)
|
345 |
doc = Document()
|
346 |
for paragraph in translated_text.split('\n'):
|
347 |
if paragraph.strip():
|
@@ -349,9 +337,7 @@ def translate_document(file, source_lang, target_lang, output_format="txt"):
|
|
349 |
doc.save(output_path)
|
350 |
|
351 |
else:
|
352 |
-
# Default to txt
|
353 |
-
output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}.txt"
|
354 |
-
output_path = os.path.join(temp_dir, output_filename)
|
355 |
with open(output_path, 'w', encoding='utf-8') as f:
|
356 |
f.write(translated_text)
|
357 |
|
@@ -377,8 +363,8 @@ def create_interface():
|
|
377 |
header = gr.Markdown(
|
378 |
f"""
|
379 |
# Document Translation Toolkit
|
380 |
-
|
381 |
-
|
382 |
"""
|
383 |
)
|
384 |
|
|
|
7 |
import torch
|
8 |
from datetime import datetime
|
9 |
import pytz
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from io import BytesIO
|
11 |
from docx import Document
|
12 |
import tempfile
|
|
|
297 |
return f"Error during translation: {str(e)}"
|
298 |
|
299 |
# Document translation function
|
300 |
+
def translate_document(file, source_lang, target_lang):
|
301 |
try:
|
302 |
if file is None:
|
303 |
return "Please upload a file", None
|
|
|
312 |
# Translate
|
313 |
translated_text = translate_text(input_text, source_lang, target_lang)
|
314 |
|
315 |
+
# Create output file with .txt extension for PDF inputs, otherwise same as input
|
316 |
+
if input_ext == '.pdf':
|
317 |
+
output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}.txt"
|
318 |
+
else:
|
319 |
+
output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}{input_ext}"
|
320 |
+
|
321 |
# Create temporary file
|
322 |
temp_dir = tempfile.gettempdir()
|
323 |
+
output_path = os.path.join(temp_dir, output_filename)
|
324 |
|
325 |
+
# Create appropriate file format
|
326 |
if input_ext == '.pdf':
|
327 |
+
# Save as txt file
|
328 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
329 |
+
f.write(translated_text)
|
330 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
elif input_ext == '.docx':
|
332 |
# Create DOCX
|
|
|
|
|
333 |
doc = Document()
|
334 |
for paragraph in translated_text.split('\n'):
|
335 |
if paragraph.strip():
|
|
|
337 |
doc.save(output_path)
|
338 |
|
339 |
else:
|
340 |
+
# Default to txt
|
|
|
|
|
341 |
with open(output_path, 'w', encoding='utf-8') as f:
|
342 |
f.write(translated_text)
|
343 |
|
|
|
363 |
header = gr.Markdown(
|
364 |
f"""
|
365 |
# Document Translation Toolkit
|
366 |
+
*Current Date and Time (UTC):* {get_current_time()}
|
367 |
+
*Current User's Login:* gauravchand
|
368 |
"""
|
369 |
)
|
370 |
|