Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,34 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import pipeline
|
3 |
+
from datasets import load_dataset
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Setup device
|
7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
+
|
9 |
+
# Load the ASR model pipeline
|
10 |
+
pipe = pipeline(
|
11 |
+
"automatic-speech-recognition",
|
12 |
+
model="openai/whisper-small.en",
|
13 |
+
chunk_length_s=30,
|
14 |
+
device=device,
|
15 |
+
)
|
16 |
+
|
17 |
+
# Function to make prediction from audio input
|
18 |
+
def transcribe(audio):
|
19 |
+
# Convert Gradio input to the format expected by the ASR pipeline
|
20 |
+
prediction = pipe(audio, batch_size=8)["text"]
|
21 |
+
return prediction
|
22 |
+
|
23 |
+
# Define the Gradio interface
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=transcribe,
|
26 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
27 |
+
outputs="text",
|
28 |
+
title="Speech to Text with Whisper Model",
|
29 |
+
description="Record your voice and transcribe it to text using OpenAI Whisper model."
|
30 |
+
)
|
31 |
+
|
32 |
+
# Launch the interface
|
33 |
+
if __name__ == "__main__":
|
34 |
+
iface.launch()
|