NLPV commited on
Commit
92d559f
·
verified ·
1 Parent(s): 715deb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gtts import gTTS
3
+ import tempfile
4
+ import difflib
5
+ import pandas as pd
6
+ from Levenshtein import distance as lev_distance
7
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
8
+ import torchaudio
9
+
10
+ # Load AI4Bharat Whisper model
11
+ processor = WhisperProcessor.from_pretrained("ai4bharat/indic-whisper-large-v2")
12
+ model = WhisperForConditionalGeneration.from_pretrained("ai4bharat/indic-whisper-large-v2").to("cpu") # use "cuda" if you have a GPU
13
+
14
+ # ... [play_text and helper functions as before] ...
15
+
16
+ def transcribe_audio(audio_path, original_text):
17
+ try:
18
+ # Load audio and preprocess
19
+ speech, rate = torchaudio.load(audio_path)
20
+ if rate != 16000:
21
+ resampler = torchaudio.transforms.Resample(orig_freq=rate, new_freq=16000)
22
+ speech = resampler(speech)
23
+ input_features = processor(speech.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features
24
+
25
+ # AI4Bharat Whisper transcription
26
+ predicted_ids = model.generate(input_features)
27
+ transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0].strip()
28
+
29
+ # Error analysis and metrics (as before)
30
+ errors = compare_hindi_sentences(original_text, transcription)
31
+ df_errors = pd.DataFrame(errors, columns=["बिगड़ा हुआ शब्द", "संभावित सही शब्द", "गलती का प्रकार"])
32
+
33
+ duration = speech.shape[-1] / 16000
34
+ transcribed_words = transcription.strip().split()
35
+ speed = round(len(transcribed_words) / duration, 2) if duration > 0 else 0
36
+ accuracy = calculate_accuracy(original_text, transcription)
37
+ result_dict = {
38
+ "📝 Transcribed Text": transcription,
39
+ "⏱️ Speaking Speed (words/sec)": speed,
40
+ "✅ Reading Accuracy (%)": accuracy,
41
+ }
42
+ return result_dict, df_errors
43
+ except Exception as e:
44
+ return {"error": str(e)}, pd.DataFrame(columns=["बिगड़ा हुआ शब्द", "संभावित सही शब्द", "गलती का प्रकार"])