HarshitJoshi commited on
Commit
f263ba4
1 Parent(s): 104c95a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -20
app.py CHANGED
@@ -5,9 +5,9 @@ import os
5
  model_id = "HarshitJoshi/whisper-small-Hindi"
6
  pipe = pipeline("automatic-speech-recognition", model=model_id)
7
 
8
- def transcribe_speech(filepath):
9
  output = pipe(
10
- filepath,
11
  max_new_tokens=256,
12
  generate_kwargs={
13
  "task": "transcribe",
@@ -21,25 +21,40 @@ def transcribe_speech(filepath):
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 handle_input(mic, upload, example):
25
- if mic is not None:
26
- return transcribe_speech(mic)
27
- elif upload is not None:
28
- return transcribe_speech(upload.name)
29
- elif example is not None:
30
- return transcribe_speech(os.path.join(example_folder, example))
31
- else:
32
- return "Please provide an input."
33
 
34
  with gr.Blocks() as demo:
35
- with gr.Row():
36
- mic = gr.Audio(type="filepath", label="Record from Microphone")
37
- upload = gr.Audio(type="file", label="Upload an Audio File")
38
- example = gr.Dropdown(choices=example_files, label="Or Select an Example")
39
-
40
- output = gr.Textbox(label="Transcription")
41
 
42
- submit_btn = gr.Button("Transcribe")
43
- submit_btn.click(handle_input, inputs=[mic, upload, example], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- demo.launch(debug=True)
 
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",
 
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(source="microphone", type="filepath", label="Audio Input")
34
+ upload_button = gr.UploadButton("Upload Audio", file_types=["audio"])
35
+ transcribe_button = gr.Button("Transcribe")
36
+ output_text = gr.Textbox(label="Transcription")
37
+
38
+ transcribe_button.click(
39
+ fn=transcribe_speech,
40
+ inputs=audio_input,
41
+ outputs=output_text
42
+ )
43
+ upload_button.upload(
44
+ fn=lambda file: file.name,
45
+ inputs=upload_button,
46
+ outputs=audio_input
47
+ )
48
+
49
+ with gr.Tab("Examples"):
50
+ example_dropdown = gr.Dropdown(choices=example_files, label="Select an example")
51
+ example_audio = gr.Audio(label="Audio Playback")
52
+ example_transcription = gr.Textbox(label="Transcription")
53
+
54
+ example_dropdown.change(
55
+ fn=play_and_transcribe,
56
+ inputs=example_dropdown,
57
+ outputs=[example_audio, example_transcription]
58
+ )
59
 
60
+ demo.launch(debug=True)