Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines5678910113 unmodified lines15161718192021182220212223242324252627262829303132333433343536373839401 unmodified line424344424344454546474 unmodified linesimport tempfileimport osimport speech_recognition as srfrom faster_whisper import WhisperModel
# Function to play the text (optional)def play_text(text):3 unmodified lines os.system(f"start {temp_file.name}") # Windows return "✅ 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 accuracy
def transcribe_audio(audio, original_text): recognizer = sr.Recognizer() try: with sr.AudioFile(audio) as source: audio_data = recognizer.record(source) # Try chunking if needed transcription = recognizer.recognize_google(audio_data, language="hi-IN") # Run inference segments, info = model.transcribe(audio, language='hi') transcription = " ".join([segment.text for segment in segments])
# Clean and split the text better (remove punctuations etc.) # Clean and split the text better import re original_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)
# Speaking speed (approximate) speed = round(len(transcribed_words) / info.duration, 2)
result = { "📝 Transcribed Text": transcription,1 unmodified line "⏱️ Speaking Speed (words/sec)": speed } return result except 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)}
app.py+12-13