HarshitJoshi commited on
Commit
bd7df7f
1 Parent(s): 294ff0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -34
app.py CHANGED
@@ -1,13 +1,19 @@
 
1
  from transformers import pipeline
2
  import gradio as gr
3
  import os
4
 
5
- model_id = "HarshitJoshi/whisper-small-Hindi"
6
- pipe = pipeline("automatic-speech-recognition", model=model_id)
 
 
 
 
 
7
 
8
- def transcribe_speech(audio):
9
  output = pipe(
10
- audio,
11
  max_new_tokens=256,
12
  generate_kwargs={
13
  "task": "transcribe",
@@ -19,36 +25,18 @@ def transcribe_speech(audio):
19
  return output["text"]
20
 
21
  example_folder = "./examples"
22
- example_files = [f for f in os.listdir(example_folder) if f.endswith('.wav') or f.endswith('.mp3')]
23
 
24
- def play_and_transcribe(filename):
25
- filepath = os.path.join(example_folder, filename)
26
- transcription = transcribe_speech(filepath)
27
- return filepath, transcription
28
-
29
- with gr.Blocks() as demo:
30
- gr.Markdown("# Hindi Speech Transcription")
31
-
32
- with gr.Tab("Transcribe"):
33
- audio_input = gr.Audio(type="filepath", label="Audio Input")
34
- transcribe_button = gr.Button("Transcribe")
35
- output_text = gr.Textbox(label="Transcription")
36
-
37
- transcribe_button.click(
38
- fn=transcribe_speech,
39
- inputs=audio_input,
40
- outputs=output_text
41
- )
42
-
43
- with gr.Tab("Examples"):
44
- example_dropdown = gr.Dropdown(choices=example_files, label="Select an example")
45
- example_audio = gr.Audio(label="Audio Playback")
46
- example_transcription = gr.Textbox(label="Transcription")
47
-
48
- example_dropdown.change(
49
- fn=play_and_transcribe,
50
- inputs=example_dropdown,
51
- outputs=[example_audio, example_transcription]
52
- )
53
 
54
  demo.launch(debug=True)
 
1
+ import torch
2
  from transformers import pipeline
3
  import gradio as gr
4
  import os
5
 
6
+ MODEL_NAME = "HarshitJoshi/whisper-small-Hindi"
7
+ device = 0 if torch.cuda.is_available() else "cpu"
8
+ pipe = pipeline(
9
+ task="automatic-speech-recognition",
10
+ model=MODEL_NAME,
11
+ device=device,
12
+ )
13
 
14
+ def transcribe_speech(filepath):
15
  output = pipe(
16
+ filepath,
17
  max_new_tokens=256,
18
  generate_kwargs={
19
  "task": "transcribe",
 
25
  return output["text"]
26
 
27
  example_folder = "./examples"
 
28
 
29
+ demo = gr.Interface(
30
+ fn=transcribe_speech,
31
+ inputs=gr.Audio(label="Audio Input", type="filepath"),
32
+ outputs=gr.Textbox(label="Transcription"),
33
+ title="Hindi Speech Transcription",
34
+ description=(
35
+ "Upload an audio file or record using your microphone to transcribe Hindi speech."
36
+ ),
37
+ examples=example_folder,
38
+ cache_examples=True,
39
+ allow_flagging="never",
40
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  demo.launch(debug=True)