Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the fine-tuned Whisper model and processor
|
| 6 |
+
model_name = "hackergeek98/tinyyyy_whisper"
|
| 7 |
+
processor = WhisperProcessor.from_pretrained(model_name)
|
| 8 |
+
model = WhisperForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Move model to GPU if available
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
model.to(device)
|
| 13 |
+
|
| 14 |
+
# Define the ASR function
|
| 15 |
+
def transcribe_audio(audio):
|
| 16 |
+
# Load audio file
|
| 17 |
+
sampling_rate, audio_data = audio
|
| 18 |
+
|
| 19 |
+
# Preprocess the audio
|
| 20 |
+
inputs = processor(audio_data, sampling_rate=sampling_rate, return_tensors="pt").input_features.to(device)
|
| 21 |
+
|
| 22 |
+
# Generate transcription
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
predicted_ids = model.generate(inputs)
|
| 25 |
+
|
| 26 |
+
# Decode the transcription
|
| 27 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 28 |
+
return transcription
|
| 29 |
+
|
| 30 |
+
# Create the Gradio interface
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=transcribe_audio, # Function to call
|
| 33 |
+
inputs=gr.Audio(source="upload", type="numpy"), # Input: Upload audio file
|
| 34 |
+
outputs=gr.Textbox(label="Transcription"), # Output: Display transcription
|
| 35 |
+
title="Whisper ASR: Tinyyyy Model",
|
| 36 |
+
description="Upload an audio file, and the fine-tuned Whisper model will transcribe it.",
|
| 37 |
+
examples=["example1.wav", "example2.wav"], # Example audio files
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Launch the app
|
| 41 |
+
interface.launch()
|