Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
4 |
+
|
5 |
+
# Load Whisper model and processor from Hugging Face
|
6 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
7 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base").to("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
|
9 |
+
def transcribe(audio):
|
10 |
+
try:
|
11 |
+
# Load audio
|
12 |
+
audio_input = processor(audio, sampling_rate=16000, return_tensors="pt")
|
13 |
+
|
14 |
+
# Move to appropriate device
|
15 |
+
audio_input = audio_input.input_features.to(model.device)
|
16 |
+
|
17 |
+
# Generate transcription
|
18 |
+
predicted_ids = model.generate(audio_input)
|
19 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
20 |
+
|
21 |
+
return transcription
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error: {str(e)}"
|
24 |
+
|
25 |
+
# Create a Gradio interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=transcribe,
|
28 |
+
inputs=gr.inputs.Audio(source="microphone", type="filepath"),
|
29 |
+
outputs="text",
|
30 |
+
title="Whisper Transcription",
|
31 |
+
description="Upload an audio file and get the transcription using Whisper model."
|
32 |
+
)
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
iface.launch()
|