Spaces:
Runtime error
Runtime error
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344import gradio as grfrom gtts import gTTSimport tempfileimport difflibimport pandas as pdfrom Levenshtein import distance as lev_distancefrom transformers import WhisperProcessor, WhisperForConditionalGenerationimport torchaudio
# Load AI4Bharat Whisper modelprocessor = WhisperProcessor.from_pretrained("ai4bharat/indic-whisper-large-v2")model = WhisperForConditionalGeneration.from_pretrained("ai4bharat/indic-whisper-large-v2").to("cpu") # use "cuda" if you have a GPU
# ... [play_text and helper functions as before] ...
def transcribe_audio(audio_path, original_text): try: # Load audio and preprocess speech, rate = torchaudio.load(audio_path) if rate != 16000: resampler = torchaudio.transforms.Resample(orig_freq=rate, new_freq=16000) speech = resampler(speech) input_features = processor(speech.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features
# AI4Bharat Whisper transcription predicted_ids = model.generate(input_features) transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0].strip()
# Error analysis and metrics (as before) errors = compare_hindi_sentences(original_text, transcription) df_errors = pd.DataFrame(errors, columns=["बिगड़ा हुआ शब्द", "संभावित सही शब्द", "गलती का प्रकार"])
duration = speech.shape[-1] / 16000 transcribed_words = transcription.strip().split() speed = round(len(transcribed_words) / duration, 2) if duration > 0 else 0 accuracy = calculate_accuracy(original_text, transcription) result_dict = { "📝 Transcribed Text": transcription, "⏱️ Speaking Speed (words/sec)": speed, "✅ Reading Accuracy (%)": accuracy, } return result_dict, df_errors except Exception as e: return {"error": str(e)}, pd.DataFrame(columns=["बिगड़ा हुआ शब्द", "संभावित सही शब्द", "गलती का प्रकार"])app.pyAdded+44