Download app.py from QuickLearnerAI/voice-to-text: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/spaces/QuickLearnerAI/voice-to-text/resolve/main/app.py
- Command line
-
hf download hf://spaces/QuickLearnerAI/voice-to-text/app.py
-
curl -L -o app.py https://huggingface.co/spaces/QuickLearnerAI/voice-to-text/resolve/main/app.py
1.66 kB
| import speech_recognition as sr | |
| import gradio as gr | |
| # 🎯 Speech to Text function with chunking for large audio | |
| def speech_to_text(audio_file): | |
| r = sr.Recognizer() | |
| text_result = [] | |
| with sr.AudioFile(audio_file) as source: | |
| while True: | |
| # প্রতি 60 সেকেন্ডে ভাগ করা হচ্ছে | |
| audio = r.record(source, duration=60) | |
| if not audio.get_raw_data(): # ডেটা না থাকলে লুপ শেষ | |
| break | |
| try: | |
| chunk_text = r.recognize_google(audio, language="en-US") | |
| text_result.append(chunk_text) | |
| except sr.UnknownValueError: | |
| text_result.append("❌ [Unrecognized audio]") | |
| except sr.RequestError as e: | |
| text_result.append(f"⚠️ API Error: {e}") | |
| break | |
| return " ".join(text_result).strip() | |
| # Gradio wrapper | |
| def stt_gradio(audio): | |
| if audio is None: | |
| return "No audio provided" | |
| return speech_to_text(audio) | |
| # Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🎤 English Speech to Text (Short & Long Audio Supported)") | |
| audio_input = gr.Audio( | |
| label="🎙️ Record or Upload Audio", | |
| type="filepath", | |
| sources=["microphone", "upload"], # ✅ মাইক্রোফোন + আপলোড | |
| format="wav", | |
| streaming=False | |
| ) | |
| text_output = gr.Textbox(label="📝 Transcribed Text", lines=10) | |
| submit_btn = gr.Button("Convert") | |
| submit_btn.click(fn=stt_gradio, inputs=audio_input, outputs=text_output) | |
| if __name__ == "__main__": | |
| demo.launch() |