Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,22 +40,23 @@ def translate_text(text, target_language):
|
|
40 |
return text
|
41 |
|
42 |
try:
|
43 |
-
# Use Google Translator for better handling of idioms and context
|
44 |
translator = GoogleTranslator(source=source_lang, target=LANGUAGES[target_language])
|
45 |
translation = translator.translate(text)
|
46 |
return translation
|
47 |
except Exception as e:
|
48 |
return f"Translation failed: {str(e)}"
|
49 |
|
50 |
-
def extract_text_from_document(
|
51 |
-
if not
|
52 |
return ""
|
53 |
|
|
|
|
|
54 |
_, file_extension = os.path.splitext(file_path)
|
55 |
|
56 |
if file_extension.lower() == '.txt':
|
57 |
-
with open(file_path, 'r', encoding='utf-8', errors='replace') as
|
58 |
-
return
|
59 |
elif file_extension.lower() in ['.docx', '.doc']:
|
60 |
return docx2txt.process(file_path)
|
61 |
else:
|
@@ -66,10 +67,8 @@ def text_to_pdf(text, output_path):
|
|
66 |
pdf.add_page()
|
67 |
pdf.set_font("Arial", size=12)
|
68 |
|
69 |
-
# Split text into lines and add to PDF
|
70 |
lines = text.split('\n')
|
71 |
for line in lines:
|
72 |
-
# Handle non-ASCII characters
|
73 |
try:
|
74 |
pdf.multi_cell(0, 10, line.encode('latin-1', 'replace').decode('latin-1'))
|
75 |
except Exception:
|
@@ -79,7 +78,6 @@ def text_to_pdf(text, output_path):
|
|
79 |
return output_path
|
80 |
|
81 |
def translate_and_save(input_text, input_file, target_language):
|
82 |
-
# Determine input source (text or file)
|
83 |
if input_text:
|
84 |
text_to_translate = input_text
|
85 |
elif input_file is not None:
|
@@ -87,10 +85,8 @@ def translate_and_save(input_text, input_file, target_language):
|
|
87 |
else:
|
88 |
return None, "Please provide either text or a document for translation."
|
89 |
|
90 |
-
# Perform translation
|
91 |
translated_text = translate_text(text_to_translate, target_language)
|
92 |
|
93 |
-
# Save as PDF
|
94 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
|
95 |
pdf_path = temp_pdf.name
|
96 |
|
@@ -98,16 +94,16 @@ def translate_and_save(input_text, input_file, target_language):
|
|
98 |
|
99 |
return pdf_path, translated_text
|
100 |
|
101 |
-
# Create Gradio interface
|
102 |
with gr.Blocks(title="Context-Aware Translation Tool") as demo:
|
103 |
gr.Markdown("# Context-Aware Language Translation")
|
104 |
gr.Markdown("This tool translates text while preserving context, idioms, and phrases.")
|
105 |
|
106 |
with gr.Row():
|
107 |
with gr.Column():
|
108 |
-
# Input components with explicit types
|
109 |
input_text = gr.Textbox(label="Enter text to translate", lines=5)
|
110 |
-
|
|
|
111 |
target_language = gr.Dropdown(
|
112 |
label="Target Language",
|
113 |
choices=list(LANGUAGES.keys()),
|
@@ -116,17 +112,14 @@ with gr.Blocks(title="Context-Aware Translation Tool") as demo:
|
|
116 |
translate_button = gr.Button("Translate")
|
117 |
|
118 |
with gr.Column():
|
119 |
-
# Output components with explicit types
|
120 |
output_text = gr.Textbox(label="Translation", lines=5)
|
121 |
output_pdf = gr.File(label="Download as PDF")
|
122 |
|
123 |
-
# Connect the components with properly typed inputs and outputs
|
124 |
translate_button.click(
|
125 |
fn=translate_and_save,
|
126 |
inputs=[input_text, input_file, target_language],
|
127 |
outputs=[output_pdf, output_text]
|
128 |
)
|
129 |
|
130 |
-
# Launch with specific options to avoid errors
|
131 |
if __name__ == "__main__":
|
132 |
demo.launch(show_error=True)
|
|
|
40 |
return text
|
41 |
|
42 |
try:
|
|
|
43 |
translator = GoogleTranslator(source=source_lang, target=LANGUAGES[target_language])
|
44 |
translation = translator.translate(text)
|
45 |
return translation
|
46 |
except Exception as e:
|
47 |
return f"Translation failed: {str(e)}"
|
48 |
|
49 |
+
def extract_text_from_document(file):
|
50 |
+
if not file:
|
51 |
return ""
|
52 |
|
53 |
+
# Since file is now a Gradio file object, use its name attribute
|
54 |
+
file_path = file.name
|
55 |
_, file_extension = os.path.splitext(file_path)
|
56 |
|
57 |
if file_extension.lower() == '.txt':
|
58 |
+
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
|
59 |
+
return f.read()
|
60 |
elif file_extension.lower() in ['.docx', '.doc']:
|
61 |
return docx2txt.process(file_path)
|
62 |
else:
|
|
|
67 |
pdf.add_page()
|
68 |
pdf.set_font("Arial", size=12)
|
69 |
|
|
|
70 |
lines = text.split('\n')
|
71 |
for line in lines:
|
|
|
72 |
try:
|
73 |
pdf.multi_cell(0, 10, line.encode('latin-1', 'replace').decode('latin-1'))
|
74 |
except Exception:
|
|
|
78 |
return output_path
|
79 |
|
80 |
def translate_and_save(input_text, input_file, target_language):
|
|
|
81 |
if input_text:
|
82 |
text_to_translate = input_text
|
83 |
elif input_file is not None:
|
|
|
85 |
else:
|
86 |
return None, "Please provide either text or a document for translation."
|
87 |
|
|
|
88 |
translated_text = translate_text(text_to_translate, target_language)
|
89 |
|
|
|
90 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
|
91 |
pdf_path = temp_pdf.name
|
92 |
|
|
|
94 |
|
95 |
return pdf_path, translated_text
|
96 |
|
97 |
+
# Create Gradio interface
|
98 |
with gr.Blocks(title="Context-Aware Translation Tool") as demo:
|
99 |
gr.Markdown("# Context-Aware Language Translation")
|
100 |
gr.Markdown("This tool translates text while preserving context, idioms, and phrases.")
|
101 |
|
102 |
with gr.Row():
|
103 |
with gr.Column():
|
|
|
104 |
input_text = gr.Textbox(label="Enter text to translate", lines=5)
|
105 |
+
# Changed type="filepath" to default behavior
|
106 |
+
input_file = gr.File(label="Or upload a document (.txt, .docx)")
|
107 |
target_language = gr.Dropdown(
|
108 |
label="Target Language",
|
109 |
choices=list(LANGUAGES.keys()),
|
|
|
112 |
translate_button = gr.Button("Translate")
|
113 |
|
114 |
with gr.Column():
|
|
|
115 |
output_text = gr.Textbox(label="Translation", lines=5)
|
116 |
output_pdf = gr.File(label="Download as PDF")
|
117 |
|
|
|
118 |
translate_button.click(
|
119 |
fn=translate_and_save,
|
120 |
inputs=[input_text, input_file, target_language],
|
121 |
outputs=[output_pdf, output_text]
|
122 |
)
|
123 |
|
|
|
124 |
if __name__ == "__main__":
|
125 |
demo.launch(show_error=True)
|