Vira21 commited on
Commit
dc3248b
1 Parent(s): c389be6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,22 +1,36 @@
1
- import torch
2
  import gradio as gr
3
- from huggingface_hub import model_info
 
4
 
5
- # Load the Gradio model interface from Hugging Face
6
- interface = gr.load("models/Vira21/Whisper-Base-KhmerV2")
 
 
 
 
 
7
 
8
- # Path to the example WAV file
9
- example_audio_path = "Example Audio/126.wav" # Replace with the correct path
 
 
 
 
 
 
10
 
11
- # Re-create the Gradio Interface with the examples parameter
12
- interface_with_examples = gr.Interface(
13
- fn=interface.fn,
14
- inputs=interface.inputs,
15
- outputs=interface.outputs,
16
- examples=[[example_audio_path]], # Add example here
17
- title=interface.title,
18
- description=interface.description,
 
19
  )
20
 
21
- # Launch the modified interface
22
- interface_with_examples.launch()
 
 
 
 
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-Base-KhmerV2"
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 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()