leenag commited on
Commit
0d1ca3b
·
verified ·
1 Parent(s): e870d56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -4
app.py CHANGED
@@ -1,5 +1,61 @@
 
 
 
 
 
 
1
  import gradio as gr
2
- import os
3
- gr.load("models/vrclc/Whisper-medium-Malayalam", examples = [
4
- [os.path.join(os.path.abspath(''),"./sample1.wav")]
5
- ]).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
+ # import os
3
+ # gr.load("models/vrclc/Whisper-medium-Malayalam", examples = [
4
+ # [os.path.join(os.path.abspath(''),"./sample1.wav")]
5
+ # ]).launch()
6
+
7
  import gradio as gr
8
+ import torch
9
+ import soundfile as sf
10
+ from transformers import pipeline
11
+
12
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
13
+ pipe = pipeline(
14
+ "automatic-speech-recognition",
15
+ model="vrclc/Whisper-medium-Malayalam",
16
+ chunk_length_s=10,
17
+ device=device,
18
+ )
19
+
20
+ def transcribe(audio):
21
+ """Transcribes Malayalam speech from an audio file."""
22
+ try:
23
+ if audio is None:
24
+ return "Please record or upload an audio file."
25
+
26
+ print(f"[DEBUG] Received audio: {audio}")
27
+
28
+ # Handle filepath case from Gradio
29
+ audio_path = audio if isinstance(audio, str) else audio.get("name", None)
30
+ if audio_path is None:
31
+ return "Could not read audio file."
32
+
33
+ print(f"[DEBUG] Reading audio file: {audio_path}")
34
+ audio_data, sample_rate = sf.read(audio_path)
35
+
36
+ print(f"[DEBUG] Audio sample rate: {sample_rate}, shape: {audio_data.shape}")
37
+
38
+ transcription = pipe(
39
+ {"array": audio_data, "sampling_rate": sample_rate},
40
+ chunk_length_s=30,
41
+ batch_size=8,
42
+ )["text"]
43
+
44
+ print(f"[DEBUG] Transcription: {transcription}")
45
+ return transcription
46
+
47
+ except Exception as e:
48
+ import traceback
49
+ print("[ERROR] Exception during transcription:")
50
+ traceback.print_exc()
51
+ return f"Error: {str(e)}"
52
+
53
+ iface = gr.Interface(
54
+ fn=transcribe,
55
+ inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
56
+ outputs="text",
57
+ title="Malayalam Speech Recognition",
58
+ description="Record or upload Malayalam speech and submit to get the transcribed text.",
59
+ )
60
+
61
+ iface.launch()