Spaces:
Sleeping
Sleeping
added rule based feedback system
Browse files
app.py
CHANGED
|
@@ -172,22 +172,62 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
| 172 |
logger.debug("Exception details:", exc_info=True)
|
| 173 |
return None, f"Error creating RAG pipeline: {e}"
|
| 174 |
|
| 175 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def handle_feedback(feedback_text):
|
| 177 |
"""
|
| 178 |
-
Handles user feedback by
|
| 179 |
-
|
| 180 |
|
| 181 |
Parameters:
|
| 182 |
- feedback_text (str): The feedback provided by the user.
|
| 183 |
|
| 184 |
Returns:
|
| 185 |
-
- str: Acknowledgment message.
|
| 186 |
"""
|
| 187 |
if feedback_text and feedback_text.strip() != "":
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
return "No feedback provided."
|
| 193 |
|
|
@@ -210,8 +250,10 @@ def answer_question(model, temperature, max_tokens, question, feedback):
|
|
| 210 |
logger.info("Received invalid input from user.")
|
| 211 |
return "Please provide a valid question or input containing meaningful text.", ""
|
| 212 |
|
| 213 |
-
# Check if the RAG pipeline
|
| 214 |
-
|
|
|
|
|
|
|
| 215 |
|
| 216 |
try:
|
| 217 |
answer = rag_chain.run(question)
|
|
|
|
| 172 |
logger.debug("Exception details:", exc_info=True)
|
| 173 |
return None, f"Error creating RAG pipeline: {e}"
|
| 174 |
|
| 175 |
+
# Define positive and negative words for rule-based sentiment analysis
|
| 176 |
+
POSITIVE_WORDS = {
|
| 177 |
+
"good", "great", "excellent", "amazing", "wonderful", "fantastic", "positive",
|
| 178 |
+
"helpful", "satisfied", "happy", "love", "liked", "enjoyed", "beneficial",
|
| 179 |
+
"superb", "awesome", "nice", "brilliant", "favorable", "pleased"
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
NEGATIVE_WORDS = {
|
| 183 |
+
"bad", "terrible", "awful", "poor", "disappointed", "unsatisfied", "hate",
|
| 184 |
+
"hated", "dislike", "dislikes", "worst", "negative", "not helpful", "frustrated",
|
| 185 |
+
"unhappy", "dissatisfied", "unfortunate", "horrible", "annoyed", "problem", "issues"
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
# Function to handle feedback with rule-based sentiment analysis
|
| 189 |
def handle_feedback(feedback_text):
|
| 190 |
"""
|
| 191 |
+
Handles user feedback by analyzing its sentiment and providing a dynamic response.
|
| 192 |
+
Stores the feedback in a temporary file for persistence during the session.
|
| 193 |
|
| 194 |
Parameters:
|
| 195 |
- feedback_text (str): The feedback provided by the user.
|
| 196 |
|
| 197 |
Returns:
|
| 198 |
+
- str: Acknowledgment message based on feedback sentiment.
|
| 199 |
"""
|
| 200 |
if feedback_text and feedback_text.strip() != "":
|
| 201 |
+
# Normalize feedback text to lowercase for comparison
|
| 202 |
+
feedback_lower = feedback_text.lower()
|
| 203 |
+
|
| 204 |
+
# Count positive and negative words
|
| 205 |
+
positive_count = sum(word in feedback_lower for word in POSITIVE_WORDS)
|
| 206 |
+
negative_count = sum(word in feedback_lower for word in NEGATIVE_WORDS)
|
| 207 |
+
|
| 208 |
+
# Determine sentiment based on counts
|
| 209 |
+
if positive_count > negative_count:
|
| 210 |
+
sentiment = "positive"
|
| 211 |
+
acknowledgment = "Thank you for your positive feedback! We're glad to hear that you found our service helpful."
|
| 212 |
+
elif negative_count > positive_count:
|
| 213 |
+
sentiment = "negative"
|
| 214 |
+
acknowledgment = "We're sorry to hear that you're not satisfied. Your feedback is valuable to us, and we'll strive to improve."
|
| 215 |
+
else:
|
| 216 |
+
sentiment = "neutral"
|
| 217 |
+
acknowledgment = "Thank you for your feedback. We appreciate your input."
|
| 218 |
+
|
| 219 |
+
# Log the feedback with sentiment
|
| 220 |
+
logger.info(f"User Feedback: {feedback_text} | Sentiment: {sentiment}")
|
| 221 |
+
|
| 222 |
+
# Optionally, store feedback in a temporary file
|
| 223 |
+
try:
|
| 224 |
+
with open("/tmp/user_feedback.txt", "a") as f:
|
| 225 |
+
f.write(f"{feedback_text} | Sentiment: {sentiment}\n")
|
| 226 |
+
logger.debug("Feedback stored successfully in /tmp/user_feedback.txt.")
|
| 227 |
+
except Exception as e:
|
| 228 |
+
logger.error(f"Error storing feedback: {e}")
|
| 229 |
+
|
| 230 |
+
return acknowledgment
|
| 231 |
else:
|
| 232 |
return "No feedback provided."
|
| 233 |
|
|
|
|
| 250 |
logger.info("Received invalid input from user.")
|
| 251 |
return "Please provide a valid question or input containing meaningful text.", ""
|
| 252 |
|
| 253 |
+
# Check if the RAG pipeline is initialized
|
| 254 |
+
if rag_chain is None:
|
| 255 |
+
logger.error("RAG pipeline is not initialized.")
|
| 256 |
+
return "The system is currently unavailable. Please try again later.", ""
|
| 257 |
|
| 258 |
try:
|
| 259 |
answer = rag_chain.run(question)
|