Mgolo commited on
Commit
9bea29b
Β·
verified Β·
1 Parent(s): 774c080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  from transformers import pipeline, MarianTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
- import unicodedata
5
  import re
6
  import whisper
7
  import tempfile
@@ -118,23 +117,19 @@ def extract_text_from_file(uploaded_file):
118
  else:
119
  raise ValueError("Unsupported file type")
120
 
121
- # --- Main Gradio Function ---
122
- def process(input_mode, target_lang, text_input, audio_input, file_input):
123
  input_text = ""
124
 
125
- if input_mode == "Text" and text_input:
126
  input_text = text_input
127
- elif input_mode == "Audio" and audio_input:
128
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
129
- tmp.write(audio_input.read())
130
- tmp_path = tmp.name
131
  input_text = transcribe_audio(audio_input)
132
- os.remove(tmp_path)
133
- elif input_mode == "File" and file_input:
134
  input_text = extract_text_from_file(file_input)
135
 
136
  if not input_text.strip():
137
- return "", "No input text provided."
138
 
139
  translated_text = translate(input_text, target_lang)
140
  return input_text, translated_text
@@ -143,21 +138,22 @@ def process(input_mode, target_lang, text_input, audio_input, file_input):
143
  with gr.Blocks() as demo:
144
  gr.Markdown("## 🌐 LocaleNLP Translator β€” English ↔ Darija / Hausa / Wolof")
145
 
146
- with gr.Row():
147
- input_mode = gr.Dropdown(["Text", "Audio", "File"], label="Select input mode")
148
- target_lang = gr.Dropdown(["Darija (Morocco)", "Hausa (Nigeria)", "Wolof (Senegal)"], label="Select target language")
 
149
 
150
  with gr.Row():
151
- text_input = gr.Textbox(label="Enter English text", lines=10)
152
- audio_input = gr.Audio(type="filepath", label="Upload Audio")
153
- file_input = gr.File(label="Upload Document")
154
 
155
  with gr.Row():
156
  extracted_text = gr.Textbox(label="Extracted / Transcribed Text", lines=10)
157
  translated_output = gr.Textbox(label="Translated Text", lines=10)
158
 
159
  run_btn = gr.Button("Translate")
160
- run_btn.click(process, inputs=[input_mode, target_lang, text_input, audio_input, file_input], outputs=[extracted_text, translated_output])
161
 
162
  if __name__ == "__main__":
163
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline, MarianTokenizer, AutoModelForSeq2SeqLM
3
  import torch
 
4
  import re
5
  import whisper
6
  import tempfile
 
117
  else:
118
  raise ValueError("Unsupported file type")
119
 
120
+ # --- Main Function ---
121
+ def process(target_lang, text_input, audio_input, file_input):
122
  input_text = ""
123
 
124
+ if text_input and text_input.strip():
125
  input_text = text_input
126
+ elif audio_input:
 
 
 
127
  input_text = transcribe_audio(audio_input)
128
+ elif file_input:
 
129
  input_text = extract_text_from_file(file_input)
130
 
131
  if not input_text.strip():
132
+ return "", "No valid input provided."
133
 
134
  translated_text = translate(input_text, target_lang)
135
  return input_text, translated_text
 
138
  with gr.Blocks() as demo:
139
  gr.Markdown("## 🌐 LocaleNLP Translator β€” English ↔ Darija / Hausa / Wolof")
140
 
141
+ target_lang = gr.Dropdown(
142
+ ["Darija (Morocco)", "Hausa (Nigeria)", "Wolof (Senegal)"],
143
+ label="Select target language"
144
+ )
145
 
146
  with gr.Row():
147
+ text_input = gr.Textbox(label="✏️ Enter English text", lines=10)
148
+ audio_input = gr.Audio(type="filepath", label="πŸ”Š Upload Audio")
149
+ file_input = gr.File(label="πŸ“„ Upload Document")
150
 
151
  with gr.Row():
152
  extracted_text = gr.Textbox(label="Extracted / Transcribed Text", lines=10)
153
  translated_output = gr.Textbox(label="Translated Text", lines=10)
154
 
155
  run_btn = gr.Button("Translate")
156
+ run_btn.click(process, inputs=[target_lang, text_input, audio_input, file_input], outputs=[extracted_text, translated_output])
157
 
158
  if __name__ == "__main__":
159
  demo.launch()