Spaces:
Sleeping
Sleeping
updated with voice the app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,9 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassifica
|
|
| 9 |
from wordcloud import WordCloud
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
import io
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Download NLTK resources
|
| 14 |
nltk.download('punkt')
|
|
@@ -30,6 +33,22 @@ def load_classification_model():
|
|
| 30 |
def load_qa_model():
|
| 31 |
return pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Preprocessing function (same as in Section 01)
|
| 34 |
def preprocess_text(text):
|
| 35 |
# Lowercase
|
|
@@ -163,6 +182,10 @@ with tab2:
|
|
| 163 |
|
| 164 |
|
| 165 |
question = st.text_input("Enter your question:")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
if st.button("Get Answer") and context and question:
|
| 168 |
with st.spinner("Searching for answers..."):
|
|
@@ -174,6 +197,18 @@ with tab2:
|
|
| 174 |
|
| 175 |
st.subheader("Details")
|
| 176 |
st.write(f"Confidence: {result['score']:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
|
| 179 |
with tab3:
|
|
|
|
| 9 |
from wordcloud import WordCloud
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
import io
|
| 12 |
+
import speech_recognition as sr
|
| 13 |
+
from gtts import gTTS
|
| 14 |
+
import os
|
| 15 |
|
| 16 |
# Download NLTK resources
|
| 17 |
nltk.download('punkt')
|
|
|
|
| 33 |
def load_qa_model():
|
| 34 |
return pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 35 |
|
| 36 |
+
def recognize_speech():
|
| 37 |
+
recognizer = sr.Recognizer()
|
| 38 |
+
with sr.Microphone() as source:
|
| 39 |
+
st.info("Listening... Speak now.")
|
| 40 |
+
try:
|
| 41 |
+
audio = recognizer.listen(source, timeout=5) # Listen for 5 seconds
|
| 42 |
+
question_text = recognizer.recognize_google(audio) # Convert speech to text
|
| 43 |
+
st.success(f"You said: {question_text}") # Show recognized text
|
| 44 |
+
return question_text
|
| 45 |
+
except sr.UnknownValueError:
|
| 46 |
+
st.error("Sorry, could not understand the audio.")
|
| 47 |
+
except sr.RequestError:
|
| 48 |
+
st.error("Could not request results, check your internet connection.")
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
|
| 52 |
# Preprocessing function (same as in Section 01)
|
| 53 |
def preprocess_text(text):
|
| 54 |
# Lowercase
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
question = st.text_input("Enter your question:")
|
| 185 |
+
|
| 186 |
+
if st.button("🎤 Speak"):
|
| 187 |
+
question = recognize_speech()
|
| 188 |
+
use_voice = True
|
| 189 |
|
| 190 |
if st.button("Get Answer") and context and question:
|
| 191 |
with st.spinner("Searching for answers..."):
|
|
|
|
| 197 |
|
| 198 |
st.subheader("Details")
|
| 199 |
st.write(f"Confidence: {result['score']:.2f}")
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
if use_voice:
|
| 203 |
+
tts = gTTS(result['answer']) # Convert text answer to speech
|
| 204 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") # Create temp file
|
| 205 |
+
tts.save(temp_audio.name)
|
| 206 |
+
|
| 207 |
+
# Play the Answer
|
| 208 |
+
st.audio(temp_audio.name, format="audio/mp3")
|
| 209 |
+
|
| 210 |
+
# Cleanup temp file
|
| 211 |
+
os.remove(temp_audio.name)
|
| 212 |
|
| 213 |
|
| 214 |
with tab3:
|