Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ from reportlab.lib.pagesizes import letter
|
|
| 16 |
from reportlab.pdfgen import canvas
|
| 17 |
from PyPDF2 import PdfReader
|
| 18 |
import docx
|
|
|
|
| 19 |
# Load dataset
|
| 20 |
@st.cache_data
|
| 21 |
def load_dataset():
|
|
@@ -45,7 +46,7 @@ def generate_question_embeddings():
|
|
| 45 |
question_embeddings = generate_question_embeddings()
|
| 46 |
|
| 47 |
# Initialize translator
|
| 48 |
-
translator =
|
| 49 |
|
| 50 |
# Suggested Questions
|
| 51 |
suggested_questions = ["What is AI?", "Tell me a joke!", "How does machine learning work?"]
|
|
@@ -111,19 +112,6 @@ def text_to_speech(text):
|
|
| 111 |
audio_file.seek(0)
|
| 112 |
return audio_file
|
| 113 |
|
| 114 |
-
def transcribe_audio():
|
| 115 |
-
"""Allows user to speak input instead of typing."""
|
| 116 |
-
recognizer = sr.Recognizer()
|
| 117 |
-
with sr.Microphone() as source:
|
| 118 |
-
st.write("π€ Listening... Speak now!")
|
| 119 |
-
audio = recognizer.listen(source)
|
| 120 |
-
try:
|
| 121 |
-
return recognizer.recognize_google(audio)
|
| 122 |
-
except sr.UnknownValueError:
|
| 123 |
-
return "Sorry, I couldn't understand that."
|
| 124 |
-
except sr.RequestError:
|
| 125 |
-
return "Speech recognition service error."
|
| 126 |
-
|
| 127 |
def generate_chat_pdf():
|
| 128 |
"""Creates a PDF of the chat history and returns it as a downloadable file."""
|
| 129 |
buffer = BytesIO()
|
|
@@ -180,32 +168,21 @@ for i, q in enumerate(suggested_questions):
|
|
| 180 |
# Voice Input
|
| 181 |
st.subheader("π€ Speak instead of typing!")
|
| 182 |
if st.button("ποΈ Use Voice Input"):
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# Video Call Feature
|
| 186 |
st.subheader("πΉ Video Call")
|
| 187 |
-
|
| 188 |
-
# Initialize session state for video call
|
| 189 |
-
if "video_call_active" not in st.session_state:
|
| 190 |
-
st.session_state.video_call_active = False
|
| 191 |
-
|
| 192 |
-
# Button to start/stop video call
|
| 193 |
if st.button("π Start Video Call"):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
if st.button("β End Video Call"):
|
| 197 |
-
st.session_state.video_call_active = False
|
| 198 |
-
|
| 199 |
-
# Run video call if active
|
| 200 |
-
if st.session_state.video_call_active:
|
| 201 |
-
webrtc_streamer(
|
| 202 |
-
key="video-call",
|
| 203 |
-
mode=WebRtcMode.SENDRECV,
|
| 204 |
-
rtc_configuration=RTCConfiguration(
|
| 205 |
-
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
|
| 206 |
-
),
|
| 207 |
-
media_stream_constraints={"video": True, "audio": True},
|
| 208 |
-
)
|
| 209 |
|
| 210 |
if user_input is None:
|
| 211 |
user_input = st.chat_input("Type your message here...")
|
|
@@ -214,21 +191,10 @@ if user_input is None:
|
|
| 214 |
if "messages" not in st.session_state:
|
| 215 |
st.session_state.messages = []
|
| 216 |
|
| 217 |
-
# Clear Chat Button
|
| 218 |
-
if st.button("ποΈ Clear Chat"):
|
| 219 |
-
st.session_state.messages.clear()
|
| 220 |
-
st.rerun()
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
# Display chat history
|
| 224 |
-
for message in st.session_state.messages:
|
| 225 |
-
with st.chat_message(message["role"]):
|
| 226 |
-
st.write(message["content"])
|
| 227 |
-
|
| 228 |
if user_input:
|
| 229 |
-
|
| 230 |
-
if
|
| 231 |
-
user_input =
|
| 232 |
|
| 233 |
response = get_best_response(user_input)
|
| 234 |
log_chat(user_input, response)
|
|
@@ -244,6 +210,5 @@ if user_input:
|
|
| 244 |
st.write(f"{response}\n\n**Sentiment Analysis:** {sentiment_result}")
|
| 245 |
st.audio(audio_file, format="audio/mp3")
|
| 246 |
|
| 247 |
-
# Download Chat as PDF
|
| 248 |
pdf_file = generate_chat_pdf()
|
| 249 |
st.download_button("π₯ Download Chat as PDF", pdf_file, "chat_history.pdf", "application/pdf")
|
|
|
|
| 16 |
from reportlab.pdfgen import canvas
|
| 17 |
from PyPDF2 import PdfReader
|
| 18 |
import docx
|
| 19 |
+
|
| 20 |
# Load dataset
|
| 21 |
@st.cache_data
|
| 22 |
def load_dataset():
|
|
|
|
| 46 |
question_embeddings = generate_question_embeddings()
|
| 47 |
|
| 48 |
# Initialize translator
|
| 49 |
+
translator = GoogleTranslator(source="auto", target="en")
|
| 50 |
|
| 51 |
# Suggested Questions
|
| 52 |
suggested_questions = ["What is AI?", "Tell me a joke!", "How does machine learning work?"]
|
|
|
|
| 112 |
audio_file.seek(0)
|
| 113 |
return audio_file
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def generate_chat_pdf():
|
| 116 |
"""Creates a PDF of the chat history and returns it as a downloadable file."""
|
| 117 |
buffer = BytesIO()
|
|
|
|
| 168 |
# Voice Input
|
| 169 |
st.subheader("π€ Speak instead of typing!")
|
| 170 |
if st.button("ποΈ Use Voice Input"):
|
| 171 |
+
recognizer = sr.Recognizer()
|
| 172 |
+
with sr.Microphone() as source:
|
| 173 |
+
st.write("π€ Listening... Speak now!")
|
| 174 |
+
audio = recognizer.listen(source)
|
| 175 |
+
try:
|
| 176 |
+
user_input = recognizer.recognize_google(audio)
|
| 177 |
+
except sr.UnknownValueError:
|
| 178 |
+
user_input = "Sorry, I couldn't understand that."
|
| 179 |
+
except sr.RequestError:
|
| 180 |
+
user_input = "Speech recognition service error."
|
| 181 |
|
| 182 |
# Video Call Feature
|
| 183 |
st.subheader("πΉ Video Call")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
if st.button("π Start Video Call"):
|
| 185 |
+
webrtc_streamer(key="video-call", mode=WebRtcMode.SENDRECV)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
if user_input is None:
|
| 188 |
user_input = st.chat_input("Type your message here...")
|
|
|
|
| 191 |
if "messages" not in st.session_state:
|
| 192 |
st.session_state.messages = []
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
if user_input:
|
| 195 |
+
translated_text = translator.translate(user_input)
|
| 196 |
+
if translated_text != user_input:
|
| 197 |
+
user_input = translated_text
|
| 198 |
|
| 199 |
response = get_best_response(user_input)
|
| 200 |
log_chat(user_input, response)
|
|
|
|
| 210 |
st.write(f"{response}\n\n**Sentiment Analysis:** {sentiment_result}")
|
| 211 |
st.audio(audio_file, format="audio/mp3")
|
| 212 |
|
|
|
|
| 213 |
pdf_file = generate_chat_pdf()
|
| 214 |
st.download_button("π₯ Download Chat as PDF", pdf_file, "chat_history.pdf", "application/pdf")
|