Spaces:
Sleeping
Sleeping
HarshitJoshi
commited on
Commit
•
84ee29a
1
Parent(s):
54996d1
Update app.py
Browse files
app.py
CHANGED
@@ -2,53 +2,50 @@ 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
output = pipe(
|
10 |
filepath,
|
11 |
max_new_tokens=256,
|
12 |
generate_kwargs={
|
13 |
"task": "transcribe",
|
14 |
-
"language": "hindi",
|
15 |
},
|
16 |
-
chunk_length_s=10,
|
17 |
-
batch_size=4,
|
18 |
)
|
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 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
fn=play_and_transcribe,
|
45 |
-
inputs=gr.Dropdown(choices=example_files, label="Select an example"),
|
46 |
-
outputs=[gr.Audio(label="Audio Playback"), gr.Textbox(label="Transcription")],
|
47 |
-
)
|
48 |
-
|
49 |
-
with demo:
|
50 |
-
gr.TabbedInterface(
|
51 |
-
[mic_transcribe, file_transcribe, example_transcribe],
|
52 |
-
["Transcribe Microphone", "Transcribe Audio File", "Transcribe Example"],
|
53 |
-
)
|
54 |
demo.launch(debug=True)
|
|
|
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(source, source_type):
|
9 |
+
if source_type == "Microphone":
|
10 |
+
filepath = source
|
11 |
+
elif source_type == "Upload":
|
12 |
+
filepath = source.name
|
13 |
+
elif source_type == "Example":
|
14 |
+
filepath = os.path.join(example_folder, source)
|
15 |
+
|
16 |
output = pipe(
|
17 |
filepath,
|
18 |
max_new_tokens=256,
|
19 |
generate_kwargs={
|
20 |
"task": "transcribe",
|
21 |
+
"language": "hindi",
|
22 |
},
|
23 |
+
chunk_length_s=10,
|
24 |
+
batch_size=4,
|
25 |
)
|
26 |
return output["text"]
|
27 |
|
28 |
+
example_folder = "./examples"
|
29 |
example_files = [f for f in os.listdir(example_folder) if f.endswith('.wav') or f.endswith('.mp3')]
|
30 |
|
31 |
+
def handle_input(mic, upload, example):
|
32 |
+
if mic is not None:
|
33 |
+
return transcribe_speech(mic, "Microphone")
|
34 |
+
elif upload is not None:
|
35 |
+
return transcribe_speech(upload, "Upload")
|
36 |
+
elif example is not None:
|
37 |
+
return transcribe_speech(example, "Example")
|
38 |
+
else:
|
39 |
+
return "Please provide an input."
|
40 |
+
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
mic = gr.Audio(source="microphone", type="filepath", label="Record from Microphone")
|
43 |
+
upload = gr.Audio(source="upload", type="file", label="Upload an Audio File")
|
44 |
+
example = gr.Dropdown(choices=example_files, label="Or Select an Example")
|
45 |
+
|
46 |
+
output = gr.Textbox(label="Transcription")
|
47 |
+
|
48 |
+
submit_btn = gr.Button("Transcribe")
|
49 |
+
submit_btn.click(handle_input, inputs=[mic, upload, example], outputs=output)
|
50 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
demo.launch(debug=True)
|