Vira21 commited on
Commit
f16c081
·
verified ·
1 Parent(s): 5ede744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -1,7 +1,39 @@
1
- import torch
2
  import gradio as gr
3
- from huggingface_hub import model_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- interface = gr.load("models/Vira21/Whisper-Base-KhmerV2")
 
 
 
 
 
 
 
 
 
6
 
7
- interface.launch()
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ import spaces
5
+
6
+ # Load the Whisper model pipeline for speech recognition with optimizations
7
+ model_name = "Vira21/Whisper-Base-KhmerV2"
8
+ whisper_pipeline = pipeline(
9
+ "automatic-speech-recognition",
10
+ model=model_name,
11
+ device="cuda" if torch.cuda.is_available() else "CPU"
12
+ )
13
+
14
+ def transcribe_audio(audio):
15
+ try:
16
+ if audio is None:
17
+ return "No audio provided. Please upload an audio file or record your voice."
18
+
19
+ # Process and transcribe the audio
20
+ result = whisper_pipeline(audio)["text"]
21
+ return result
22
+ except Exception as e:
23
+ # Handle errors and return an error message
24
+ return f"An error occurred during transcription: {str(e)}"
25
 
26
+ # Gradio Interface with optimizations
27
+ interface = gr.Interface(
28
+ fn=transcribe_audio,
29
+ inputs=gr.Audio(type="filepath"),
30
+ outputs="text",
31
+ title="OpenAI Whisper Small KHMER-ENGLISH Speech-to-Text",
32
+ description="Upload an audio file or record your voice to get the transcription.",
33
+ examples=[["Example Audio/126.wav"], ["Example Audio/tomholland28282.wav"]],
34
+ allow_flagging="never" # Disables flagging to save resources
35
+ )
36
 
37
+ # Launch the app with queue enabled for better handling on free CPU
38
+ if __name__ == "__main__":
39
+ interface.launch()