Spaces:
Sleeping
Sleeping
Commit ·
31e64f5
0
Parent(s):
Working voice pipeline: Bangla STT and TTS in Streamlit app
Browse files- .gitignore +3 -0
- app.py +21 -0
- output.mp3 +0 -0
- response.mp3 +0 -0
- roundtrip_output.mp3 +0 -0
- test_roundtrip.py +7 -0
- test_tts.py +10 -0
- test_whisper.py +15 -0
- voice_functions.py +18 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from voice_functions import listen, speak
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
st.title("KrishiKotha - কৃষি সহায়ক")
|
| 6 |
+
|
| 7 |
+
audio_file = st.file_uploader("আপনার প্রশ্ন রেকর্ড করে আপলোড করুন", type=["wav", "mp3", "m4a"])
|
| 8 |
+
|
| 9 |
+
if audio_file is not None:
|
| 10 |
+
with open("temp_input.wav", "wb") as f:
|
| 11 |
+
f.write(audio_file.getbuffer())
|
| 12 |
+
|
| 13 |
+
# Convert to proper format using ffmpeg
|
| 14 |
+
subprocess.run(["ffmpeg", "-y", "-i", "temp_input.wav", "-ar", "16000", "-ac", "1", "temp_fixed.wav"])
|
| 15 |
+
|
| 16 |
+
text = listen("temp_fixed.wav")
|
| 17 |
+
st.write("**আপনি বলেছেন:**", text)
|
| 18 |
+
|
| 19 |
+
# For now, just echo back — later this becomes Pair 2's grounded answer
|
| 20 |
+
speak(text, "response.mp3")
|
| 21 |
+
st.audio("response.mp3")
|
output.mp3
ADDED
|
Binary file (61.4 kB). View file
|
|
|
response.mp3
ADDED
|
Binary file (31.5 kB). View file
|
|
|
roundtrip_output.mp3
ADDED
|
Binary file (31.5 kB). View file
|
|
|
test_roundtrip.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from voice_functions import listen, speak
|
| 2 |
+
|
| 3 |
+
text = listen("test_audio_fixed.wav")
|
| 4 |
+
print("Heard:", text)
|
| 5 |
+
|
| 6 |
+
speak(text, "roundtrip_output.mp3")
|
| 7 |
+
print("Spoken back — check roundtrip_output.mp3")
|
test_tts.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gtts import gTTS
|
| 2 |
+
|
| 3 |
+
def speak(text: str, output_path: str = "output.mp3") -> str:
|
| 4 |
+
tts = gTTS(text=text, lang="bn")
|
| 5 |
+
tts.save(output_path)
|
| 6 |
+
return output_path
|
| 7 |
+
|
| 8 |
+
if __name__ == "__main__":
|
| 9 |
+
speak("আপনার ধান গাছের পাতা হলুদ হয়ে যাচ্ছে, একজন কৃষি বিশেষজ্ঞের পরামর্শ নিন।")
|
| 10 |
+
print("Saved to output.mp3")
|
test_whisper.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Bangla fine-tuned Whisper model
|
| 4 |
+
asr = pipeline(
|
| 5 |
+
"automatic-speech-recognition",
|
| 6 |
+
model="bangla-speech-processing/BanglaASR"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
def listen(audio_file_path: str) -> str:
|
| 10 |
+
result = asr(audio_file_path)
|
| 11 |
+
return result["text"]
|
| 12 |
+
|
| 13 |
+
if __name__ == "__main__":
|
| 14 |
+
result = listen("test_audio_fixed.wav")
|
| 15 |
+
print("Transcribed text:", result)
|
voice_functions.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
|
| 4 |
+
# Speech-to-text (Bangla-specific model)
|
| 5 |
+
asr = pipeline(
|
| 6 |
+
"automatic-speech-recognition",
|
| 7 |
+
model="bangla-speech-processing/BanglaASR"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
def listen(audio_file_path: str) -> str:
|
| 11 |
+
result = asr(audio_file_path)
|
| 12 |
+
return result["text"]
|
| 13 |
+
|
| 14 |
+
# Text-to-speech
|
| 15 |
+
def speak(text: str, output_path: str = "output.mp3") -> str:
|
| 16 |
+
tts = gTTS(text=text, lang="bn")
|
| 17 |
+
tts.save(output_path)
|
| 18 |
+
return output_path
|