Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435import gradio as grfrom gtts import gTTSimport osimport timedef read_hindi_text(text):if not text.strip():return "❌ Please enter some text!"# Generate TTStts = gTTS(text=text, lang='hi', slow=False)filename = "hindi_output.mp3"tts.save(filename)# Play audiotry:os.system("start " + filename) # Windows# os.system("afplay " + filename) # Mac# os.system("xdg-open " + filename) # Linuxoutput_message = f"✅ Hindi Text saved as '{filename}' and playing now!"except Exception as e:output_message = f"⚠️ Error playing the file: {str(e)}"return output_message# Gradio interfaceiface = gr.Interface(fn=read_hindi_text,inputs=gr.Textbox(lines=4, placeholder="यहाँ हिंदी टेक्स्ट लिखें..."),outputs="text",title="Hindi Text-to-Speech",description="हिंदी टेक्स्ट डालें और 'Read' बटन दबाएँ। टेक्स्ट को आवाज़ में बदला जाएगा।")iface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import gradio as grfrom gtts import gTTSimport timeimport difflibimport tempfileimport osimport speech_recognition as sr# Function to play the text (optional)def play_text(text):tts = gTTS(text=text, lang='hi', slow=False)temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')tts.save(temp_file.name)os.system(f"start {temp_file.name}") # Windowsreturn "✅ Text is being read out. Please listen and read it yourself."# Function to transcribe user's audiodef transcribe_audio(audio, original_text):recognizer = sr.Recognizer()with sr.AudioFile(audio) as source:audio_data = recognizer.record(source)try:start_time = time.time()# Using Google Speech Recognition (supports Hindi)transcription = recognizer.recognize_google(audio_data, language="hi-IN")end_time = time.time()# Calculate Accuracyoriginal_words = original_text.strip().split()transcribed_words = transcription.strip().split()matcher = difflib.SequenceMatcher(None, original_words, transcribed_words)accuracy = round(matcher.ratio() * 100, 2)# Calculate speedduration = end_time - start_time # time to process (not speaking time)# Better: estimate speaking time from audio length if needed (advanced)speed = round(len(transcribed_words) / duration, 2) # words per secondresult = {"📝 Transcribed Text": transcription,"🎯 Accuracy (%)": accuracy,"⏱️ Speaking Speed (words/sec)": speed}return resultexcept Exception as e:return {"error": str(e)}# Gradio Appwith gr.Blocks() as app:gr.Markdown("## 🗣️ Hindi Reading & Pronunciation Practice App")with gr.Row():input_text = gr.Textbox(label="Paste Hindi Text Here", placeholder="यहाँ हिंदी टेक्स्ट लिखें...")play_button = gr.Button("🔊 Listen to Text")play_button.click(play_text, inputs=[input_text], outputs=[])gr.Markdown("### 🎤 Now record yourself reading the text aloud below:")audio_input = gr.Audio(source="microphone", type="filepath", label="Record Your Voice")submit_button = gr.Button("✅ Submit Recording for Checking")output = gr.JSON(label="Results")submit_button.click(transcribe_audio, inputs=[audio_input, input_text], outputs=[output])# Launch the appapp.launch()
app.py+61-26