Spaces:
Sleeping
Sleeping
ew version
Browse files- app.py +23 -103
- nltk_data/.DS_Store +0 -0
app.py
CHANGED
|
@@ -16,7 +16,7 @@ import pandas as pd
|
|
| 16 |
import json
|
| 17 |
|
| 18 |
# Enable logging for debugging
|
| 19 |
-
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
# Function to clean the API key
|
|
@@ -100,7 +100,7 @@ def is_valid_input(text):
|
|
| 100 |
# Initialize the LLM using ChatGroq with GROQ's API
|
| 101 |
def initialize_llm(model, temperature, max_tokens):
|
| 102 |
try:
|
| 103 |
-
# Allocate a portion of tokens for the prompt
|
| 104 |
prompt_allocation = int(max_tokens * 0.2)
|
| 105 |
response_max_tokens = max_tokens - prompt_allocation
|
| 106 |
if response_max_tokens <= 50:
|
|
@@ -109,8 +109,8 @@ def initialize_llm(model, temperature, max_tokens):
|
|
| 109 |
llm = ChatGroq(
|
| 110 |
model=model,
|
| 111 |
temperature=temperature,
|
| 112 |
-
max_tokens=response_max_tokens,
|
| 113 |
-
api_key=api_key
|
| 114 |
)
|
| 115 |
logger.info("LLM initialized successfully.")
|
| 116 |
return llm
|
|
@@ -133,11 +133,11 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
| 133 |
# Initialize the embedding model
|
| 134 |
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 135 |
|
| 136 |
-
# Use a temporary directory for Chroma vectorstore
|
| 137 |
vectorstore = Chroma.from_documents(
|
| 138 |
documents=splits,
|
| 139 |
embedding=embedding_model,
|
| 140 |
-
persist_directory="/tmp/chroma_db"
|
| 141 |
)
|
| 142 |
vectorstore.persist() # Save the database to disk
|
| 143 |
logger.info("Vectorstore initialized and persisted successfully.")
|
|
@@ -148,13 +148,10 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
| 148 |
input_variables=["context", "question"],
|
| 149 |
template="""
|
| 150 |
You are an AI assistant with expertise in daily wellness. Your aim is to provide detailed and comprehensive solutions regarding daily wellness topics without unnecessary verbosity.
|
| 151 |
-
|
| 152 |
Context:
|
| 153 |
{context}
|
| 154 |
-
|
| 155 |
Question:
|
| 156 |
{question}
|
| 157 |
-
|
| 158 |
Provide a thorough and complete answer, including relevant examples and a suggested schedule. Ensure that the response does not end abruptly.
|
| 159 |
"""
|
| 160 |
)
|
|
@@ -172,109 +169,41 @@ 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 |
-
# 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 |
-
|
| 234 |
# Initialize the RAG pipeline once at startup
|
| 235 |
-
# Define the file paths (ensure 'AIChatbot.csv' is in the root directory of your Space)
|
| 236 |
file_paths = ['AIChatbot.csv']
|
| 237 |
-
model = "llama3-8b-8192"
|
| 238 |
-
temperature = 0.7
|
| 239 |
-
max_tokens = 500
|
| 240 |
|
| 241 |
rag_chain, message = create_rag_pipeline(file_paths, model, temperature, max_tokens)
|
| 242 |
if rag_chain is None:
|
| 243 |
logger.error("Failed to initialize RAG pipeline at startup.")
|
| 244 |
-
# Depending on your preference, you might want to exit or continue. Here, we'll continue.
|
| 245 |
|
| 246 |
# Function to answer questions with input validation and post-processing
|
| 247 |
-
def answer_question(model, temperature, max_tokens, question
|
| 248 |
# Validate input
|
| 249 |
if not is_valid_input(question):
|
| 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)
|
| 260 |
logger.info("Question answered successfully.")
|
| 261 |
# Post-process to ensure the answer ends with complete sentences
|
| 262 |
complete_answer = ensure_complete_sentences(answer)
|
| 263 |
-
|
| 264 |
-
# Handle feedback
|
| 265 |
-
feedback_response = handle_feedback(feedback)
|
| 266 |
-
|
| 267 |
-
return complete_answer, feedback_response
|
| 268 |
except Exception as e_inner:
|
| 269 |
logger.error(f"Error during RAG pipeline execution: {e_inner}")
|
| 270 |
logger.debug("Exception details:", exc_info=True)
|
| 271 |
-
return f"Error during RAG pipeline execution: {e_inner}"
|
| 272 |
|
| 273 |
-
# Gradio Interface
|
| 274 |
-
def gradio_interface(model, temperature, max_tokens, question
|
| 275 |
-
|
| 276 |
-
# For now, we'll ignore changes to model parameters after initialization
|
| 277 |
-
return answer_question(model, temperature, max_tokens, question, feedback)
|
| 278 |
|
| 279 |
# Define Gradio UI
|
| 280 |
interface = gr.Interface(
|
|
@@ -291,7 +220,7 @@ interface = gr.Interface(
|
|
| 291 |
maximum=1,
|
| 292 |
step=0.01,
|
| 293 |
value=temperature,
|
| 294 |
-
info="Controls the randomness of the response. Higher values
|
| 295 |
),
|
| 296 |
gr.Slider(
|
| 297 |
label="Max Tokens",
|
|
@@ -299,31 +228,22 @@ interface = gr.Interface(
|
|
| 299 |
maximum=2048,
|
| 300 |
step=1,
|
| 301 |
value=max_tokens,
|
| 302 |
-
info="Determines the maximum number of tokens in the response.
|
| 303 |
),
|
| 304 |
gr.Textbox(
|
| 305 |
label="Question",
|
| 306 |
placeholder="e.g., What is box breathing and how does it help reduce anxiety?"
|
| 307 |
-
),
|
| 308 |
-
gr.Textbox(
|
| 309 |
-
label="Feedback",
|
| 310 |
-
placeholder="Provide your feedback here...",
|
| 311 |
-
lines=2
|
| 312 |
)
|
| 313 |
],
|
| 314 |
-
outputs=
|
| 315 |
-
"text",
|
| 316 |
-
"text"
|
| 317 |
-
],
|
| 318 |
title="Daily Wellness AI",
|
| 319 |
description="Ask questions about daily wellness and get detailed solutions.",
|
| 320 |
examples=[
|
| 321 |
-
["llama3-8b-8192", 0.7, 500, "What is box breathing and how does it help reduce anxiety?"
|
| 322 |
-
["llama3-8b-8192", 0.6, 600, "Provide a daily wellness schedule incorporating box breathing techniques."
|
| 323 |
],
|
| 324 |
-
allow_flagging="never"
|
| 325 |
)
|
| 326 |
|
| 327 |
-
# Launch Gradio app without share=True (not supported on Hugging Face Spaces)
|
| 328 |
if __name__ == "__main__":
|
| 329 |
interface.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
|
| 16 |
import json
|
| 17 |
|
| 18 |
# Enable logging for debugging
|
| 19 |
+
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
# Function to clean the API key
|
|
|
|
| 100 |
# Initialize the LLM using ChatGroq with GROQ's API
|
| 101 |
def initialize_llm(model, temperature, max_tokens):
|
| 102 |
try:
|
| 103 |
+
# Allocate a portion of tokens for the prompt
|
| 104 |
prompt_allocation = int(max_tokens * 0.2)
|
| 105 |
response_max_tokens = max_tokens - prompt_allocation
|
| 106 |
if response_max_tokens <= 50:
|
|
|
|
| 109 |
llm = ChatGroq(
|
| 110 |
model=model,
|
| 111 |
temperature=temperature,
|
| 112 |
+
max_tokens=response_max_tokens,
|
| 113 |
+
api_key=api_key
|
| 114 |
)
|
| 115 |
logger.info("LLM initialized successfully.")
|
| 116 |
return llm
|
|
|
|
| 133 |
# Initialize the embedding model
|
| 134 |
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 135 |
|
| 136 |
+
# Use a temporary directory for Chroma vectorstore
|
| 137 |
vectorstore = Chroma.from_documents(
|
| 138 |
documents=splits,
|
| 139 |
embedding=embedding_model,
|
| 140 |
+
persist_directory="/tmp/chroma_db"
|
| 141 |
)
|
| 142 |
vectorstore.persist() # Save the database to disk
|
| 143 |
logger.info("Vectorstore initialized and persisted successfully.")
|
|
|
|
| 148 |
input_variables=["context", "question"],
|
| 149 |
template="""
|
| 150 |
You are an AI assistant with expertise in daily wellness. Your aim is to provide detailed and comprehensive solutions regarding daily wellness topics without unnecessary verbosity.
|
|
|
|
| 151 |
Context:
|
| 152 |
{context}
|
|
|
|
| 153 |
Question:
|
| 154 |
{question}
|
|
|
|
| 155 |
Provide a thorough and complete answer, including relevant examples and a suggested schedule. Ensure that the response does not end abruptly.
|
| 156 |
"""
|
| 157 |
)
|
|
|
|
| 169 |
logger.debug("Exception details:", exc_info=True)
|
| 170 |
return None, f"Error creating RAG pipeline: {e}"
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
# Initialize the RAG pipeline once at startup
|
|
|
|
| 173 |
file_paths = ['AIChatbot.csv']
|
| 174 |
+
model = "llama3-8b-8192"
|
| 175 |
+
temperature = 0.7
|
| 176 |
+
max_tokens = 500
|
| 177 |
|
| 178 |
rag_chain, message = create_rag_pipeline(file_paths, model, temperature, max_tokens)
|
| 179 |
if rag_chain is None:
|
| 180 |
logger.error("Failed to initialize RAG pipeline at startup.")
|
|
|
|
| 181 |
|
| 182 |
# Function to answer questions with input validation and post-processing
|
| 183 |
+
def answer_question(model, temperature, max_tokens, question):
|
| 184 |
# Validate input
|
| 185 |
if not is_valid_input(question):
|
| 186 |
logger.info("Received invalid input from user.")
|
| 187 |
+
return "Please provide a valid question or input containing meaningful text."
|
| 188 |
|
|
|
|
| 189 |
if rag_chain is None:
|
| 190 |
logger.error("RAG pipeline is not initialized.")
|
| 191 |
+
return "The system is currently unavailable. Please try again later."
|
| 192 |
|
| 193 |
try:
|
| 194 |
answer = rag_chain.run(question)
|
| 195 |
logger.info("Question answered successfully.")
|
| 196 |
# Post-process to ensure the answer ends with complete sentences
|
| 197 |
complete_answer = ensure_complete_sentences(answer)
|
| 198 |
+
return complete_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
except Exception as e_inner:
|
| 200 |
logger.error(f"Error during RAG pipeline execution: {e_inner}")
|
| 201 |
logger.debug("Exception details:", exc_info=True)
|
| 202 |
+
return f"Error during RAG pipeline execution: {e_inner}"
|
| 203 |
|
| 204 |
+
# Gradio Interface (no feedback)
|
| 205 |
+
def gradio_interface(model, temperature, max_tokens, question):
|
| 206 |
+
return answer_question(model, temperature, max_tokens, question)
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Define Gradio UI
|
| 209 |
interface = gr.Interface(
|
|
|
|
| 220 |
maximum=1,
|
| 221 |
step=0.01,
|
| 222 |
value=temperature,
|
| 223 |
+
info="Controls the randomness of the response. Higher values make output more random."
|
| 224 |
),
|
| 225 |
gr.Slider(
|
| 226 |
label="Max Tokens",
|
|
|
|
| 228 |
maximum=2048,
|
| 229 |
step=1,
|
| 230 |
value=max_tokens,
|
| 231 |
+
info="Determines the maximum number of tokens in the response."
|
| 232 |
),
|
| 233 |
gr.Textbox(
|
| 234 |
label="Question",
|
| 235 |
placeholder="e.g., What is box breathing and how does it help reduce anxiety?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
)
|
| 237 |
],
|
| 238 |
+
outputs="text",
|
|
|
|
|
|
|
|
|
|
| 239 |
title="Daily Wellness AI",
|
| 240 |
description="Ask questions about daily wellness and get detailed solutions.",
|
| 241 |
examples=[
|
| 242 |
+
["llama3-8b-8192", 0.7, 500, "What is box breathing and how does it help reduce anxiety?"],
|
| 243 |
+
["llama3-8b-8192", 0.6, 600, "Provide a daily wellness schedule incorporating box breathing techniques."]
|
| 244 |
],
|
| 245 |
+
allow_flagging="never"
|
| 246 |
)
|
| 247 |
|
|
|
|
| 248 |
if __name__ == "__main__":
|
| 249 |
interface.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
nltk_data/.DS_Store
CHANGED
|
Binary files a/nltk_data/.DS_Store and b/nltk_data/.DS_Store differ
|
|
|