Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,38 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
import tempfile
|
6 |
+
import soundfile as sf
|
7 |
|
8 |
+
# Load the Whisper model once during startup
|
9 |
+
device = 0 if torch.cuda.is_available() else -1
|
10 |
+
asr_pipeline = pipeline(model="openai/whisper-small", device=device)
|
11 |
+
|
12 |
+
# Function to handle the transcription process
|
13 |
+
def transcribe_audio(audio_file):
|
14 |
+
# Create a temporary file to save the uploaded audio
|
15 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
16 |
+
temp_audio_file.write(audio_file.read())
|
17 |
+
temp_file_path = temp_audio_file.name
|
18 |
+
|
19 |
+
# Perform the transcription
|
20 |
+
transcription = asr_pipeline(temp_file_path)
|
21 |
+
|
22 |
+
# Remove the temporary file
|
23 |
+
os.remove(temp_file_path)
|
24 |
+
|
25 |
+
# Return the transcription result
|
26 |
+
return transcription['text']
|
27 |
+
|
28 |
+
# Create Gradio interface
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=transcribe_audio, # The function to call when audio is uploaded
|
31 |
+
inputs=gr.Audio(source="upload", type="file"), # Input type: audio file
|
32 |
+
outputs="text", # Output type: text (transcription)
|
33 |
+
title="Whisper Audio Transcription", # Title of the Gradio interface
|
34 |
+
description="Upload an audio file to get a transcription using OpenAI's Whisper model"
|
35 |
+
)
|
36 |
+
|
37 |
+
# Launch the Gradio interface
|
38 |
+
interface.launch()
|