AvtnshM commited on
Commit
ef348e0
·
verified ·
1 Parent(s): 1602163
Files changed (1) hide show
  1. app.py +55 -41
app.py CHANGED
@@ -22,19 +22,19 @@ def load_model():
22
  torch_dtype=torch.float32,
23
  low_cpu_mem_usage=True
24
  )
25
- print("Model loaded successfully!")
26
  return model
27
  except Exception as e:
28
- print(f"Error loading model: {e}")
29
  print("Trying alternative loading method...")
30
 
31
  try:
32
  # Fallback: Load without low memory optimization
33
  model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
34
- print("Model loaded with fallback method!")
35
  return model
36
  except Exception as e2:
37
- print(f"All loading methods failed: {e2}")
38
  return None
39
 
40
  # Load the model
@@ -53,7 +53,7 @@ LANGUAGE_OPTIONS = {
53
  "ಕನ್ನಡ (Kannada)": "kn",
54
  "മലയാളം (Malayalam)": "ml",
55
  "অসমীয়া (Assamese)": "as",
56
- "उ्दू (Urdu)": "ur",
57
  "नेपाली (Nepali)": "ne",
58
  "संस्कृत (Sanskrit)": "sa"
59
  }
@@ -63,12 +63,12 @@ def transcribe_audio(audio_file, language_choice, decoding_method):
63
  Transcribe audio file to text using the AI4Bharat IndicConformer model
64
  """
65
  if model is None:
66
- return "Error: Model not loaded properly. Please check the logs."
67
 
68
  try:
69
  # Handle different input types
70
  if audio_file is None:
71
- return "⚠️ कृपया एक ऑडियो फ़ाइल प्रदान करें (Please provide an audio file)"
72
 
73
  # Get language code
74
  lang_code = LANGUAGE_OPTIONS.get(language_choice, "hi") # Default to Hindi
@@ -87,7 +87,7 @@ def transcribe_audio(audio_file, language_choice, decoding_method):
87
 
88
  # Ensure audio is not empty
89
  if wav.numel() == 0:
90
- return "⚠️ ऑडियो फ़ाइल खाली है (Audio file is empty)"
91
 
92
  # Convert to mono if stereo
93
  if wav.dim() > 1 and wav.size(0) > 1:
@@ -122,56 +122,70 @@ def transcribe_audio(audio_file, language_choice, decoding_method):
122
  transcription = str(transcription).strip()
123
 
124
  if not transcription:
125
- return "⚠️ कोई भाषण नहीं मिला या ट्रांस्क्रिप्शन खाली है। कृपया अधिक स्पष्ट रूप से बोलने का प्रयास करें। (No speech detected or transcription is empty. Please try speaking more clearly.)"
126
 
127
- return f"**ट्रांस्क्रिप्शन ({decoding_method}):** {transcription}"
128
 
129
  except Exception as e:
130
  error_msg = str(e)
131
  print(f"Error during transcription: {error_msg}")
132
- return f"ऑडियो प्रसंस्करण त्रुटि (Audio processing error): {error_msg}"
133
 
134
  def transcribe_microphone(audio, language_choice, decoding_method):
135
  """Transcribe audio from microphone input"""
136
  if audio is None:
137
- return "⚠️ कोई ऑडियो रिकॉर्ड नहीं हुआ। कृपया माइक्रोफ़ोन पर क्लिक करें और बोलें। (No audio recorded. Please click the microphone and speak.)"
138
  return transcribe_audio(audio, language_choice, decoding_method)
139
 
 
 
 
 
 
 
140
  def create_shareable_text(text, method="Voice"):
141
  """Create formatted text for sharing"""
142
  if not text or text.strip() == "":
143
- return "📤 कोई टेक्स्ट शेयर करने के लिए उपलब्ध नहीं है।"
144
 
145
  # Clean the text (remove markdown and emojis from result)
146
- clean_text = text.replace("**ट्रांस्क्रिप्शन (CTC):**", "").replace("**ट्रांस्क्रिप्शन (RNNT):**", "")
147
  clean_text = clean_text.strip()
148
 
149
  # Create shareable format
150
- share_text = f"""📱 Hindi Speech-to-Text Result
151
 
152
- 🎙️ Input Method: {method}
153
- 📝 Transcription: {clean_text}
154
 
155
- 🤖 Generated by: AI4Bharat IndicConformer
156
- 🌐 App: Hindi ASR - Hugging Face Spaces
157
 
158
  ---
159
  Share this Hindi transcription with others!"""
160
 
161
  return share_text
162
 
 
 
 
 
 
 
 
 
163
  # Model status message
164
- model_status = "Model loaded successfully!" if model is not None else "Model failed to load"
165
 
166
  # Create Gradio interface
167
  with gr.Blocks(title="भारतीय भाषा स्पीच टू टेक्स्ट (Indic Speech to Text)", theme=gr.themes.Soft()) as demo:
168
  gr.Markdown(
169
  f"""
170
- # 🎙️ भारतीय भाषा स्पीच टू टेक्स्ट कनवर्टर (Indic Speech to Text Converter)
171
 
172
  **मॉडल स्थिति (Model Status):** {model_status}
173
 
174
- {'⏳ **पहली बार लोड हो रहा है - कृपया 2-3 मिनट प्रतीक्षा करें (First time loading - please wait 2-3 minutes)**' if model is None else ''}
175
 
176
  AI4Bharat के बहुभाषी मॉडल का उपयोग करके भाषण को टेक्स्ट में बदलें।
177
  (Convert speech to text using AI4Bharat's multilingual model.)
@@ -194,17 +208,17 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
194
  language_dropdown = gr.Dropdown(
195
  choices=list(LANGUAGE_OPTIONS.keys()),
196
  value="हिंदी (Hindi)",
197
- label="🌐 भाषा चुनें (Select Language)",
198
  interactive=True
199
  )
200
  decoding_method = gr.Radio(
201
  choices=["CTC", "RNNT"],
202
  value="CTC",
203
- label="🔧 डिकोडिंग विधि (Decoding Method)",
204
  info="CTC: तेज़ (Fast), RNNT: अधिक सटीक (More Accurate)"
205
  )
206
 
207
- with gr.Tab("🎤 वॉइस इनपुट (Voice Input)"):
208
  gr.Markdown("### अपनी आवाज़ रिकॉर्ड करें और तत्काल ट्रांस्क्रिप्शन प्राप्त करें")
209
 
210
  with gr.Row():
@@ -212,7 +226,7 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
212
  microphone_input = gr.Audio(
213
  sources=["microphone"],
214
  type="numpy",
215
- label="🎤 रिकॉर्ड करने के लिए क्लिक करें",
216
  show_download_button=False,
217
  interactive=True,
218
  streaming=False,
@@ -224,17 +238,17 @@ with gr.Blocks(title="भारतीय भा���ा स्पीच टू ट
224
  )
225
 
226
  with gr.Row():
227
- mic_submit_btn = gr.Button("🔄 ट्रांसक्राइब करें", variant="primary", size="lg")
228
- clear_mic_btn = gr.Button("🗑️ साफ़ करें", variant="secondary")
229
- share_mic_btn = gr.Button("📤 शेयर करें", variant="secondary")
230
 
231
  with gr.Column(scale=1):
232
  mic_output = gr.Textbox(
233
- label="📝 ट्रांस्क्रिप्शन परिणाम (Transcription Result)",
234
  placeholder="रिकॉर्डिंग के बाद आपका ट्रांस्क्रिप्शन यहाँ दिखाई देगा...",
235
  lines=8,
236
  max_lines=15,
237
- interactive=True # Enable text selection
238
  )
239
 
240
  # Button actions for microphone tab
@@ -263,7 +277,7 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
263
  outputs=mic_output
264
  )
265
 
266
- with gr.Tab("📁 फ़ाइल अपलोड (File Upload)"):
267
  gr.Markdown("### ट्रांस्क्रिप्शन के लिए एक ऑडियो फ़ाइल अपलोड करें")
268
 
269
  with gr.Row():
@@ -271,23 +285,23 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
271
  file_input = gr.Audio(
272
  sources=["upload"],
273
  type="filepath",
274
- label="📎 ऑडियो फ़ाइल अपलोड करें",
275
  show_download_button=False,
276
  interactive=True
277
  )
278
 
279
  with gr.Row():
280
- file_submit_btn = gr.Button("🔄 फ़ाइल ट्रांसक्राइब करें", variant="primary", size="lg")
281
- clear_file_btn = gr.Button("🗑️ साफ़ करें", variant="secondary")
282
- share_file_btn = gr.Button("📤 शेयर करें", variant="secondary")
283
 
284
  with gr.Column(scale=1):
285
  file_output = gr.Textbox(
286
- label="📝 ट्रांस्क्रिप्शन परिणाम (Transcription Result)",
287
  placeholder="एक ऑडियो फ़ाइल अपलोड करें और ट्रांसक्राइब पर क्लिक करें...",
288
  lines=8,
289
  max_lines=15,
290
- interactive=True # Enable text selection
291
  )
292
 
293
  # Button actions for file tab
@@ -310,9 +324,9 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
310
  )
311
 
312
  gr.Markdown(
313
- f"""
314
  ---
315
- ### 💡 बेहतर ट्रांस्क्रिप्शन के लिए टिप्स (Tips for better transcription):
316
 
317
  **वॉइस रिकॉर्डिंग के लिए (For Voice Recording):**
318
  - स्पष्ट और मध्यम गति से बोलें (Speak clearly and at moderate pace)
@@ -336,7 +350,7 @@ with gr.Blocks(title="भारतीय भाषा स्पीच टू ट
336
 
337
  ---
338
  **मॉडल (Model)**: AI4Bharat IndicConformer-600M-Multi
339
- **फ्रेमवर्क (Framework)**: 🤗 Transformers + Gradio
340
  **परिनियोजन (Deployment)**: Hugging Face Spaces (CPU)
341
  **समर्थित भाषाएं**: भारत की 22 आधिकारिक भाषाएं
342
  """
 
22
  torch_dtype=torch.float32,
23
  low_cpu_mem_usage=True
24
  )
25
+ print("Model loaded successfully!")
26
  return model
27
  except Exception as e:
28
+ print(f"Error loading model: {e}")
29
  print("Trying alternative loading method...")
30
 
31
  try:
32
  # Fallback: Load without low memory optimization
33
  model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
34
+ print("Model loaded with fallback method!")
35
  return model
36
  except Exception as e2:
37
+ print(f"All loading methods failed: {e2}")
38
  return None
39
 
40
  # Load the model
 
53
  "ಕನ್ನಡ (Kannada)": "kn",
54
  "മലയാളം (Malayalam)": "ml",
55
  "অসমীয়া (Assamese)": "as",
56
+ "उ्दू (Urdu)": "ur",
57
  "नेपाली (Nepali)": "ne",
58
  "संस्कृत (Sanskrit)": "sa"
59
  }
 
63
  Transcribe audio file to text using the AI4Bharat IndicConformer model
64
  """
65
  if model is None:
66
+ return "Error: Model not loaded properly. Please check the logs."
67
 
68
  try:
69
  # Handle different input types
70
  if audio_file is None:
71
+ return "कृपया एक ऑडियो फ़ाइल प्रदान करें (Please provide an audio file)"
72
 
73
  # Get language code
74
  lang_code = LANGUAGE_OPTIONS.get(language_choice, "hi") # Default to Hindi
 
87
 
88
  # Ensure audio is not empty
89
  if wav.numel() == 0:
90
+ return "ऑडियो फ़ाइल खाली है (Audio file is empty)"
91
 
92
  # Convert to mono if stereo
93
  if wav.dim() > 1 and wav.size(0) > 1:
 
122
  transcription = str(transcription).strip()
123
 
124
  if not transcription:
125
+ return "कोई भाषण नहीं मिला या ट्रांस्क्रिप्शन खाली है। कृपया अधिक स्पष्ट रूप से बोलने का प्रयास करें। (No speech detected or transcription is empty. Please try speaking more clearly.)"
126
 
127
+ return f"**ट्रांस्क्रिप्शन ({decoding_method}):** {transcription}"
128
 
129
  except Exception as e:
130
  error_msg = str(e)
131
  print(f"Error during transcription: {error_msg}")
132
+ return f"ऑडियो प्रसंस्करण त्रुटि (Audio processing error): {error_msg}"
133
 
134
  def transcribe_microphone(audio, language_choice, decoding_method):
135
  """Transcribe audio from microphone input"""
136
  if audio is None:
137
+ return "कोई ऑडियो रिकॉर्ड नहीं हुआ। कृपया माइक्रोफ़ोन पर क्लिक करें और बोलें। (No audio recorded. Please click the microphone and speak.)"
138
  return transcribe_audio(audio, language_choice, decoding_method)
139
 
140
+ def transcribe_file(audio_file, language_choice, decoding_method):
141
+ """Transcribe uploaded audio file"""
142
+ if audio_file is None:
143
+ return "कोई फ़ाइल अपलोड नहीं हुई। कृपया एक ऑडियो फ़ाइल चुनें। (No file uploaded. Please select an audio file.)"
144
+ return transcribe_audio(audio_file, language_choice, decoding_method)
145
+
146
  def create_shareable_text(text, method="Voice"):
147
  """Create formatted text for sharing"""
148
  if not text or text.strip() == "":
149
+ return "कोई टेक्स्ट शेयर करने के लिए उपलब्ध नहीं है।"
150
 
151
  # Clean the text (remove markdown and emojis from result)
152
+ clean_text = text.replace("**ट्रांस्क्रिप्शन (CTC):**", "").replace("**ट्रांस्क्रिप्शन (RNNT):**", "")
153
  clean_text = clean_text.strip()
154
 
155
  # Create shareable format
156
+ share_text = f"""Hindi Speech-to-Text Result
157
 
158
+ Input Method: {method}
159
+ Transcription: {clean_text}
160
 
161
+ Generated by: AI4Bharat IndicConformer
162
+ App: Hindi ASR - Hugging Face Spaces
163
 
164
  ---
165
  Share this Hindi transcription with others!"""
166
 
167
  return share_text
168
 
169
+ def share_microphone_result(text):
170
+ """Create shareable text for microphone result"""
171
+ return create_shareable_text(text, "Voice Recording")
172
+
173
+ def share_file_result(text):
174
+ """Create shareable text for file result"""
175
+ return create_shareable_text(text, "File Upload")
176
+
177
  # Model status message
178
+ model_status = "Model loaded successfully!" if model is not None else "Model failed to load"
179
 
180
  # Create Gradio interface
181
  with gr.Blocks(title="भारतीय भाषा स्पीच टू टेक्स्ट (Indic Speech to Text)", theme=gr.themes.Soft()) as demo:
182
  gr.Markdown(
183
  f"""
184
+ # भारतीय भाषा स्पीच टू टेक्स्ट कनवर्टर (Indic Speech to Text Converter)
185
 
186
  **मॉडल स्थिति (Model Status):** {model_status}
187
 
188
+ {'पहली बार लोड हो रहा है - कृपया 2-3 मिनट प्रतीक्षा करें (First time loading - please wait 2-3 minutes)' if model is None else ''}
189
 
190
  AI4Bharat के बहुभाषी मॉडल का उपयोग करके भाषण को टेक्स्ट में बदलें।
191
  (Convert speech to text using AI4Bharat's multilingual model.)
 
208
  language_dropdown = gr.Dropdown(
209
  choices=list(LANGUAGE_OPTIONS.keys()),
210
  value="हिंदी (Hindi)",
211
+ label="भाषा चुनें (Select Language)",
212
  interactive=True
213
  )
214
  decoding_method = gr.Radio(
215
  choices=["CTC", "RNNT"],
216
  value="CTC",
217
+ label="डिकोडिंग विधि (Decoding Method)",
218
  info="CTC: तेज़ (Fast), RNNT: अधिक सटीक (More Accurate)"
219
  )
220
 
221
+ with gr.Tab("वॉइस इनपुट (Voice Input)"):
222
  gr.Markdown("### अपनी आवाज़ रिकॉर्ड करें और तत्काल ट्रांस्क्रिप्शन प्राप्त करें")
223
 
224
  with gr.Row():
 
226
  microphone_input = gr.Audio(
227
  sources=["microphone"],
228
  type="numpy",
229
+ label="रिकॉर्ड करने के लिए क्लिक करें",
230
  show_download_button=False,
231
  interactive=True,
232
  streaming=False,
 
238
  )
239
 
240
  with gr.Row():
241
+ mic_submit_btn = gr.Button("ट्रांसक्राइब करें", variant="primary", size="lg")
242
+ clear_mic_btn = gr.Button("साफ़ करें", variant="secondary")
243
+ share_mic_btn = gr.Button("शेयर करें", variant="secondary")
244
 
245
  with gr.Column(scale=1):
246
  mic_output = gr.Textbox(
247
+ label="ट्रांस्क्रिप्शन परिणाम (Transcription Result)",
248
  placeholder="रिकॉर्डिंग के बाद आपका ट्रांस्क्रिप्शन यहाँ दिखाई देगा...",
249
  lines=8,
250
  max_lines=15,
251
+ interactive=True
252
  )
253
 
254
  # Button actions for microphone tab
 
277
  outputs=mic_output
278
  )
279
 
280
+ with gr.Tab("फ़ाइल अपलोड (File Upload)"):
281
  gr.Markdown("### ट्रांस्क्रिप्शन के लिए एक ऑडियो फ़ाइल अपलोड करें")
282
 
283
  with gr.Row():
 
285
  file_input = gr.Audio(
286
  sources=["upload"],
287
  type="filepath",
288
+ label="ऑडियो फ़ाइल अपलोड करें",
289
  show_download_button=False,
290
  interactive=True
291
  )
292
 
293
  with gr.Row():
294
+ file_submit_btn = gr.Button("फ़ाइल ट्रांसक्राइब करें", variant="primary", size="lg")
295
+ clear_file_btn = gr.Button("साफ़ करें", variant="secondary")
296
+ share_file_btn = gr.Button("शेयर करें", variant="secondary")
297
 
298
  with gr.Column(scale=1):
299
  file_output = gr.Textbox(
300
+ label="ट्रांस्क्रिप्शन परिणाम (Transcription Result)",
301
  placeholder="एक ऑडियो फ़ाइल अपलोड करें और ट्रांसक्राइब पर क्लिक करें...",
302
  lines=8,
303
  max_lines=15,
304
+ interactive=True
305
  )
306
 
307
  # Button actions for file tab
 
324
  )
325
 
326
  gr.Markdown(
327
+ """
328
  ---
329
+ ### बेहतर ट्रांस्क्रिप्शन के लिए टिप्स (Tips for better transcription):
330
 
331
  **वॉइस रिकॉर्डिंग के लिए (For Voice Recording):**
332
  - स्पष्ट और मध्यम गति से बोलें (Speak clearly and at moderate pace)
 
350
 
351
  ---
352
  **मॉडल (Model)**: AI4Bharat IndicConformer-600M-Multi
353
+ **फ्रेमवर्क (Framework)**: Transformers + Gradio
354
  **परिनियोजन (Deployment)**: Hugging Face Spaces (CPU)
355
  **समर्थित भाषाएं**: भारत की 22 आधिकारिक भाषाएं
356
  """