Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines56789103 unmodified lines1415161718192021222324252627282930313233343536371 unmodified line394041424344454647484 unmodified linesimport tempfileimport osimport speech_recognition as sr# Function to play the text (optional)def play_text(text):3 unmodified linesos.system(f"start {temp_file.name}") # Windowsreturn "✅ Text is being read out. Please listen and read it yourself."def transcribe_audio(audio, original_text):recognizer = sr.Recognizer()try:with sr.AudioFile(audio) as source:audio_data = recognizer.record(source)# Try chunking if neededtranscription = recognizer.recognize_google(audio_data, language="hi-IN")# Clean and split the text better (remove punctuations etc.)import reoriginal_words = re.findall(r'\w+', original_text.strip())transcribed_words = re.findall(r'\w+', transcription.strip())matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)accuracy = round(matcher.ratio() * 100, 2)speed = round(len(transcribed_words) / (len(audio_data.frame_data) / audio_data.sample_rate), 2)result = {"📝 Transcribed Text": transcription,1 unmodified line"⏱️ Speaking Speed (words/sec)": speed}return resultexcept sr.UnknownValueError:return {"error": "Could not understand audio"}except sr.RequestError as e:return {"error": f"Request error: {e}"}except Exception as e:return {"error": str(e)}4 unmodified lines5678910113 unmodified lines15161718192021222324252627282930313233343536373839401 unmodified line4243444546474 unmodified linesimport tempfileimport osimport speech_recognition as srfrom faster_whisper import WhisperModel# Function to play the text (optional)def play_text(text):3 unmodified linesos.system(f"start {temp_file.name}") # Windowsreturn "✅ Text is being read out. Please listen and read it yourself."# Load model once (outside function for efficiency)model = WhisperModel("small", compute_type="float32") # Or "medium" for better accuracydef transcribe_audio(audio, original_text):try:# Run inferencesegments, info = model.transcribe(audio, language='hi')transcription = " ".join([segment.text for segment in segments])# Clean and split the text betterimport reoriginal_words = re.findall(r'\w+', original_text.strip())transcribed_words = re.findall(r'\w+', transcription.strip())matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)accuracy = round(matcher.ratio() * 100, 2)# Speaking speed (approximate)speed = round(len(transcribed_words) / info.duration, 2)result = {"📝 Transcribed Text": transcription,1 unmodified line"⏱️ Speaking Speed (words/sec)": speed}return resultexcept Exception as e:return {"error": str(e)}
app.py+12-13