|
import os |
|
import torch |
|
from transformers import pipeline |
|
import librosa |
|
import gradio as gr |
|
import warnings |
|
|
|
|
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
|
|
|
|
def load_model(): |
|
try: |
|
print("β³ Loading Telugu ASR model...") |
|
|
|
asr_pipeline = pipeline( |
|
"automatic-speech-recognition", |
|
model="anuragshas/wav2vec2-large-xlsr-53-telugu", |
|
device="cuda:0" if torch.cuda.is_available() else "cpu" |
|
) |
|
print("β
Model loaded successfully") |
|
return asr_pipeline |
|
except Exception as e: |
|
print(f"β Model loading failed: {str(e)}") |
|
raise |
|
|
|
|
|
asr_pipeline = load_model() |
|
|
|
|
|
|
|
|
|
def transcribe_telugu(audio_path): |
|
try: |
|
|
|
audio, sr = librosa.load(audio_path, sr=16000) |
|
|
|
|
|
result = asr_pipeline(audio) |
|
telugu_transcription = result['text'] |
|
|
|
return telugu_transcription |
|
|
|
except Exception as e: |
|
print(f"β Processing error: {str(e)}") |
|
return "Transcription failed (try clearer audio)" |
|
|
|
|
|
|
|
|
|
def create_interface(): |
|
with gr.Blocks() as interface: |
|
gr.Markdown(""" |
|
# π€ Telugu Speech Recognition |
|
Telugu Audio Transcription System: |
|
1. Speak in Telugu or upload an audio file |
|
2. Get accurate Telugu transcription |
|
|
|
Tips for best results: |
|
- Speak clearly in a quiet environment |
|
- Keep recordings between 5-30 seconds |
|
""") |
|
|
|
with gr.Row(): |
|
audio_input = gr.Audio( |
|
type="filepath", |
|
label="Record/Upload Telugu Audio", |
|
sources=["microphone", "upload"] |
|
) |
|
|
|
with gr.Row(): |
|
output_text = gr.Textbox(label="Telugu Transcription") |
|
|
|
submit_btn = gr.Button("Transcribe") |
|
submit_btn.click( |
|
fn=transcribe_telugu, |
|
inputs=audio_input, |
|
outputs=output_text |
|
) |
|
|
|
gr.Examples( |
|
examples=[ |
|
["sample_telugu1.wav"], |
|
["sample_telugu2.wav"] |
|
], |
|
inputs=audio_input |
|
) |
|
|
|
return interface |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
print("π Starting Telugu transcription service...") |
|
interface = create_interface() |
|
interface.launch( |
|
server_name="0.0.0.0", |
|
share=False, |
|
debug=True |
|
) |