Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -58,10 +58,8 @@ def validate_prompt(prompt: str) -> bool:
|
|
58 |
Returns:
|
59 |
bool: True if the prompt is valid, False otherwise.
|
60 |
"""
|
61 |
-
#
|
62 |
-
|
63 |
-
return True
|
64 |
-
return False
|
65 |
|
66 |
# Function to convert text to speech
|
67 |
def text_to_speech(text: str) -> None:
|
@@ -73,15 +71,10 @@ def text_to_speech(text: str) -> None:
|
|
73 |
try:
|
74 |
tts = gTTS(text, lang='en')
|
75 |
tts.save("response.mp3")
|
76 |
-
os.system("mpg321 response.mp3")
|
77 |
except Exception as e:
|
78 |
st.error(f"Error generating speech: {e}")
|
79 |
|
80 |
-
# Display chat history
|
81 |
-
for message in st.session_state.messages:
|
82 |
-
with st.chat_message(message["role"]):
|
83 |
-
st.write(message["content"])
|
84 |
-
|
85 |
# Function to generate chatbot response
|
86 |
def generate_response(prompt: str) -> str:
|
87 |
"""
|
@@ -112,29 +105,35 @@ def generate_response(prompt: str) -> str:
|
|
112 |
|
113 |
return response
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# User input handling
|
116 |
-
|
|
|
117 |
# Validate user input
|
118 |
if validate_prompt(prompt):
|
119 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
120 |
-
|
121 |
-
st.write(prompt)
|
122 |
|
123 |
-
# Generate
|
124 |
-
with st.
|
125 |
-
|
126 |
-
|
127 |
-
st.write(response)
|
128 |
-
|
129 |
-
# Text-to-Speech integration for the assistant's response
|
130 |
-
text_to_speech(response)
|
131 |
|
132 |
# Store the assistant's response
|
133 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
134 |
else:
|
135 |
st.warning("Invalid input. Please ensure your input contains only valid characters.")
|
136 |
|
137 |
# User Feedback Section
|
138 |
feedback = st.selectbox("How was your experience?", ["π Excellent", "π Okay", "π Poor"])
|
139 |
if feedback:
|
140 |
-
st.success(f"Thank you for your feedback: {feedback}", icon="β
")
|
|
|
58 |
Returns:
|
59 |
bool: True if the prompt is valid, False otherwise.
|
60 |
"""
|
61 |
+
# Allow alphanumeric characters, spaces, and punctuation
|
62 |
+
return bool(re.match(r'^[A-Za-z0-9\s\.,;!?(){}[\]]+$', prompt))
|
|
|
|
|
63 |
|
64 |
# Function to convert text to speech
|
65 |
def text_to_speech(text: str) -> None:
|
|
|
71 |
try:
|
72 |
tts = gTTS(text, lang='en')
|
73 |
tts.save("response.mp3")
|
74 |
+
os.system("mpg321 response.mp3") # Use mpg321 to play the audio
|
75 |
except Exception as e:
|
76 |
st.error(f"Error generating speech: {e}")
|
77 |
|
|
|
|
|
|
|
|
|
|
|
78 |
# Function to generate chatbot response
|
79 |
def generate_response(prompt: str) -> str:
|
80 |
"""
|
|
|
105 |
|
106 |
return response
|
107 |
|
108 |
+
# Display chat history using native Streamlit components
|
109 |
+
for message in st.session_state.messages:
|
110 |
+
if message["role"] == "user":
|
111 |
+
st.markdown(f"**You:** {message['content']}")
|
112 |
+
elif message["role"] == "assistant":
|
113 |
+
st.markdown(f"**Assistant:** {message['content']}")
|
114 |
+
|
115 |
# User input handling
|
116 |
+
prompt = st.text_input("Your message:", key="chat_input")
|
117 |
+
if prompt:
|
118 |
# Validate user input
|
119 |
if validate_prompt(prompt):
|
120 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
121 |
+
st.markdown(f"**You:** {prompt}")
|
|
|
122 |
|
123 |
+
# Generate assistant response
|
124 |
+
with st.spinner("Assistant is typing..."):
|
125 |
+
response = generate_response(prompt)
|
126 |
+
st.markdown(f"**Assistant:** {response}")
|
|
|
|
|
|
|
|
|
127 |
|
128 |
# Store the assistant's response
|
129 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
130 |
+
|
131 |
+
# Text-to-Speech integration for the assistant's response
|
132 |
+
text_to_speech(response)
|
133 |
else:
|
134 |
st.warning("Invalid input. Please ensure your input contains only valid characters.")
|
135 |
|
136 |
# User Feedback Section
|
137 |
feedback = st.selectbox("How was your experience?", ["π Excellent", "π Okay", "π Poor"])
|
138 |
if feedback:
|
139 |
+
st.success(f"Thank you for your feedback: {feedback}", icon="β
")
|