Update app.py
Browse files
app.py
CHANGED
@@ -195,6 +195,40 @@ def translate_speech(audio_file, source_language="English", target_language="Ara
|
|
195 |
"translated_text": translation
|
196 |
}
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
# Gradio Interface
|
199 |
def create_ui():
|
200 |
with gr.Blocks(title="Police Vision Translator") as app:
|
@@ -218,7 +252,7 @@ def create_ui():
|
|
218 |
translated_info = gr.JSON(label="Translated Information")
|
219 |
|
220 |
process_btn.click(
|
221 |
-
fn=
|
222 |
inputs=[doc_input, source_lang, target_lang],
|
223 |
outputs=[doc_output, doc_type, extracted_info, translated_info]
|
224 |
)
|
@@ -238,7 +272,7 @@ def create_ui():
|
|
238 |
translated_text = gr.Textbox(label="Translated Text")
|
239 |
|
240 |
translate_btn.click(
|
241 |
-
fn=
|
242 |
inputs=[audio_input, speech_source_lang, speech_target_lang],
|
243 |
outputs=[original_text, translated_text]
|
244 |
)
|
|
|
195 |
"translated_text": translation
|
196 |
}
|
197 |
|
198 |
+
# Modified document processing wrapper function
|
199 |
+
def process_doc_wrapper(img, src, tgt):
|
200 |
+
if img is None:
|
201 |
+
# Return empty values if no image is provided
|
202 |
+
return None, "No document", {}, {}
|
203 |
+
|
204 |
+
try:
|
205 |
+
result = process_document(img, src, tgt)
|
206 |
+
return (
|
207 |
+
result["annotated_image"], # For doc_output (Image)
|
208 |
+
result["document_type"], # For doc_type (Textbox)
|
209 |
+
result["extracted_text"], # For extracted_info (JSON)
|
210 |
+
result["translated_text"] # For translated_info (JSON)
|
211 |
+
)
|
212 |
+
except Exception as e:
|
213 |
+
print(f"Error in document processing: {e}")
|
214 |
+
return None, f"Error: {str(e)}", {}, {}
|
215 |
+
|
216 |
+
# Modified speech translation wrapper function
|
217 |
+
def speech_translate_wrapper(audio, src, tgt):
|
218 |
+
if audio is None:
|
219 |
+
# Return empty values if no audio is provided
|
220 |
+
return "No speech detected", ""
|
221 |
+
|
222 |
+
try:
|
223 |
+
result = translate_speech(audio, src, tgt)
|
224 |
+
return (
|
225 |
+
result["original_text"], # For original_text (Textbox)
|
226 |
+
result["translated_text"] # For translated_text (Textbox)
|
227 |
+
)
|
228 |
+
except Exception as e:
|
229 |
+
print(f"Error in speech translation: {e}")
|
230 |
+
return f"Error: {str(e)}", ""
|
231 |
+
|
232 |
# Gradio Interface
|
233 |
def create_ui():
|
234 |
with gr.Blocks(title="Police Vision Translator") as app:
|
|
|
252 |
translated_info = gr.JSON(label="Translated Information")
|
253 |
|
254 |
process_btn.click(
|
255 |
+
fn=process_doc_wrapper,
|
256 |
inputs=[doc_input, source_lang, target_lang],
|
257 |
outputs=[doc_output, doc_type, extracted_info, translated_info]
|
258 |
)
|
|
|
272 |
translated_text = gr.Textbox(label="Translated Text")
|
273 |
|
274 |
translate_btn.click(
|
275 |
+
fn=speech_translate_wrapper,
|
276 |
inputs=[audio_input, speech_source_lang, speech_target_lang],
|
277 |
outputs=[original_text, translated_text]
|
278 |
)
|