Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -30,13 +30,11 @@ LANGUAGES = {
|
|
30 |
# Major Indian Languages
|
31 |
"Hindi": "hi_IN",
|
32 |
"Bengali": "bn_IN",
|
33 |
-
"Gujarati": "gu_IN",
|
34 |
"Marathi": "mr_IN",
|
35 |
"Tamil": "ta_IN",
|
36 |
"Telugu": "te_IN",
|
37 |
"Malayalam": "ml_IN",
|
38 |
-
"Punjabi": "pa_IN", # Note: Using closest available in mBART
|
39 |
-
"Kannada": "kn_IN", # Note: Using closest available in mBART
|
40 |
"Urdu": "ur_PK"
|
41 |
}
|
42 |
|
@@ -79,13 +77,24 @@ def save_as_pdf(text, output_path):
|
|
79 |
pdf.add_page()
|
80 |
pdf.set_font("Arial", size=12)
|
81 |
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
89 |
|
90 |
# Translation function
|
91 |
def translate(text, source_lang, target_lang, max_length=1024):
|
@@ -128,6 +137,9 @@ def translate(text, source_lang, target_lang, max_length=1024):
|
|
128 |
# Process uploads and handle translation
|
129 |
def process_file(file, source_lang, target_lang):
|
130 |
"""Process uploaded file and translate its content"""
|
|
|
|
|
|
|
131 |
try:
|
132 |
# Save uploaded file temporarily
|
133 |
temp_file_path = file.name
|
@@ -146,62 +158,61 @@ def process_file(file, source_lang, target_lang):
|
|
146 |
translated_text = translate(text, source_lang, target_lang)
|
147 |
|
148 |
# Save translation as PDF
|
149 |
-
output_pdf_path = temp_file_path
|
150 |
-
save_as_pdf(translated_text, output_pdf_path)
|
151 |
|
|
|
|
|
|
|
152 |
return output_pdf_path, translated_text
|
153 |
|
154 |
except Exception as e:
|
155 |
return None, f"Error processing file: {str(e)}"
|
156 |
|
157 |
-
# Gradio
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
162 |
|
163 |
-
with gr.
|
164 |
-
|
165 |
-
|
166 |
-
target_lang_text = gr.Dropdown(list(LANGUAGES.keys()), value="Hindi", label="Target Language")
|
167 |
-
|
168 |
-
with gr.Row():
|
169 |
-
input_text = gr.Textbox(label="Enter text to translate", lines=5, placeholder="Type or paste text here...")
|
170 |
-
output_text = gr.Textbox(label="Translation", lines=5)
|
171 |
-
|
172 |
-
translate_btn = gr.Button("Translate Text", variant="primary")
|
173 |
-
translate_btn.click(
|
174 |
-
fn=translate,
|
175 |
-
inputs=[input_text, source_lang_text, target_lang_text],
|
176 |
-
outputs=output_text
|
177 |
-
)
|
178 |
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
)
|
195 |
|
196 |
-
gr.
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
203 |
|
204 |
-
# Launch the
|
205 |
if __name__ == "__main__":
|
206 |
-
|
207 |
-
app.launch(share=True) # Remove share=True in production
|
|
|
30 |
# Major Indian Languages
|
31 |
"Hindi": "hi_IN",
|
32 |
"Bengali": "bn_IN",
|
33 |
+
"Gujarati": "gu_IN",
|
34 |
"Marathi": "mr_IN",
|
35 |
"Tamil": "ta_IN",
|
36 |
"Telugu": "te_IN",
|
37 |
"Malayalam": "ml_IN",
|
|
|
|
|
38 |
"Urdu": "ur_PK"
|
39 |
}
|
40 |
|
|
|
77 |
pdf.add_page()
|
78 |
pdf.set_font("Arial", size=12)
|
79 |
|
80 |
+
# Handle encoding safely
|
81 |
+
try:
|
82 |
+
# Try UTF-8 first
|
83 |
+
encoded_text = text
|
84 |
+
pdf.multi_cell(0, 10, encoded_text)
|
85 |
+
except Exception:
|
86 |
+
try:
|
87 |
+
# Fall back to latin-1 with replacement
|
88 |
+
encoded_text = text.encode('latin-1', 'replace').decode('latin-1')
|
89 |
+
pdf.multi_cell(0, 10, encoded_text)
|
90 |
+
except Exception as e:
|
91 |
+
return f"Error creating PDF: {str(e)}"
|
92 |
|
93 |
+
try:
|
94 |
+
pdf.output(output_path)
|
95 |
+
return output_path
|
96 |
+
except Exception as e:
|
97 |
+
return f"Error saving PDF: {str(e)}"
|
98 |
|
99 |
# Translation function
|
100 |
def translate(text, source_lang, target_lang, max_length=1024):
|
|
|
137 |
# Process uploads and handle translation
|
138 |
def process_file(file, source_lang, target_lang):
|
139 |
"""Process uploaded file and translate its content"""
|
140 |
+
if file is None:
|
141 |
+
return None, "No file uploaded."
|
142 |
+
|
143 |
try:
|
144 |
# Save uploaded file temporarily
|
145 |
temp_file_path = file.name
|
|
|
158 |
translated_text = translate(text, source_lang, target_lang)
|
159 |
|
160 |
# Save translation as PDF
|
161 |
+
output_pdf_path = os.path.join(os.path.dirname(temp_file_path), f"translated_{os.path.basename(temp_file_path)}.pdf")
|
162 |
+
result = save_as_pdf(translated_text, output_pdf_path)
|
163 |
|
164 |
+
if isinstance(result, str) and result.startswith("Error"):
|
165 |
+
return None, result
|
166 |
+
|
167 |
return output_pdf_path, translated_text
|
168 |
|
169 |
except Exception as e:
|
170 |
return None, f"Error processing file: {str(e)}"
|
171 |
|
172 |
+
# Create Gradio blocks
|
173 |
+
with gr.Blocks(title="Indian Language Translator") as demo:
|
174 |
+
gr.Markdown("# Indian & Global Language Translator")
|
175 |
+
gr.Markdown("Translate text with understanding of idioms and cultural expressions")
|
176 |
+
|
177 |
+
with gr.Tab("Text Translation"):
|
178 |
+
with gr.Row():
|
179 |
+
source_lang_text = gr.Dropdown(list(LANGUAGES.keys()), value="English", label="Source Language")
|
180 |
+
target_lang_text = gr.Dropdown(list(LANGUAGES.keys()), value="Hindi", label="Target Language")
|
181 |
|
182 |
+
with gr.Row():
|
183 |
+
input_text = gr.Textbox(label="Enter text to translate", lines=5, placeholder="Type or paste text here...")
|
184 |
+
output_text = gr.Textbox(label="Translation", lines=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
+
translate_btn = gr.Button("Translate Text", variant="primary")
|
187 |
+
translate_btn.click(
|
188 |
+
fn=translate,
|
189 |
+
inputs=[input_text, source_lang_text, target_lang_text],
|
190 |
+
outputs=output_text
|
191 |
+
)
|
192 |
+
|
193 |
+
with gr.Tab("Document Translation"):
|
194 |
+
with gr.Row():
|
195 |
+
source_lang_doc = gr.Dropdown(list(LANGUAGES.keys()), value="English", label="Source Language")
|
196 |
+
target_lang_doc = gr.Dropdown(list(LANGUAGES.keys()), value="Hindi", label="Target Language")
|
197 |
+
|
198 |
+
file_input = gr.File(label="Upload Document (PDF, DOCX, TXT)", file_types=[".pdf", ".docx", ".txt"])
|
199 |
+
with gr.Row():
|
200 |
+
output_file = gr.File(label="Translated PDF")
|
201 |
+
output_preview = gr.Textbox(label="Translation Preview", lines=8)
|
202 |
|
203 |
+
translate_doc_btn = gr.Button("Translate Document", variant="primary")
|
204 |
+
translate_doc_btn.click(
|
205 |
+
fn=process_file,
|
206 |
+
inputs=[file_input, source_lang_doc, target_lang_doc],
|
207 |
+
outputs=[output_file, output_preview]
|
208 |
+
)
|
209 |
|
210 |
+
gr.Markdown("### Supported File Types: PDF, DOCX, TXT")
|
211 |
+
gr.Markdown("### Features:")
|
212 |
+
gr.Markdown("- Supports major Indian languages including Hindi, Bengali, Tamil, Telugu, Malayalam")
|
213 |
+
gr.Markdown("- Context-aware translation that understands idioms and cultural expressions")
|
214 |
+
gr.Markdown("- Document translation with PDF output")
|
215 |
|
216 |
+
# Launch the interface
|
217 |
if __name__ == "__main__":
|
218 |
+
demo.launch(share=True)
|
|