Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the Whisper model pipeline for speech recognition with optimizations
|
6 |
+
model_name = "Vira21/Whisper-Small-Khmer"
|
7 |
+
whisper_pipeline = pipeline(
|
8 |
+
"automatic-speech-recognition",
|
9 |
+
model=model_name,
|
10 |
+
device=0 if torch.cuda.is_available() else -1 # Use GPU if available, otherwise use CPU
|
11 |
+
)
|
12 |
+
|
13 |
+
def transcribe_audio(audio):
|
14 |
+
try:
|
15 |
+
# Process and transcribe the audio
|
16 |
+
result = whisper_pipeline(audio)["text"]
|
17 |
+
return result
|
18 |
+
except Exception as e:
|
19 |
+
# Handle errors and return an error message
|
20 |
+
return f"An error occurred during transcription: {str(e)}"
|
21 |
+
|
22 |
+
# Gradio Interface with optimizations
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=transcribe_audio,
|
25 |
+
inputs=gr.Audio(type="filepath"),
|
26 |
+
outputs="text",
|
27 |
+
title="Whisper Base Khmer Speech-to-Text",
|
28 |
+
description="Upload an audio file or record your voice to get the transcription in Khmer.",
|
29 |
+
examples=[["Example Audio/126.wav"]],
|
30 |
+
allow_flagging="never" # Disables flagging to save resources
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch the app with queue enabled for better handling on free CPU
|
34 |
+
if __name__ == "__main__":
|
35 |
+
interface.queue() # Enable asynchronous queuing for better performance
|
36 |
+
interface.launch()
|