Spaces:
Sleeping
Sleeping
updated app.py for the input handling
Browse files
app.py
CHANGED
|
@@ -67,14 +67,61 @@ def ensure_complete_sentences(text):
|
|
| 67 |
return ' '.join(s.strip() for s in sentences)
|
| 68 |
return text
|
| 69 |
|
|
|
|
| 70 |
def is_valid_input(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
if not text or text.strip() == "":
|
| 72 |
-
return False
|
|
|
|
| 73 |
if not re.search('[A-Za-z]', text):
|
| 74 |
-
return False
|
|
|
|
| 75 |
if len(text.strip()) < 5:
|
| 76 |
-
return False
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def initialize_llm(model, temperature, max_tokens):
|
| 80 |
prompt_allocation = int(max_tokens * 0.2)
|
|
@@ -109,10 +156,16 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
| 109 |
|
| 110 |
retriever = vectorstore.as_retriever()
|
| 111 |
|
|
|
|
| 112 |
custom_prompt_template = PromptTemplate(
|
| 113 |
input_variables=["context", "question"],
|
| 114 |
template="""
|
| 115 |
You are an AI assistant specialized in daily wellness. Provide a concise, thorough, and stand-alone answer to the user's question based on the given context. Include relevant examples or schedules where beneficial. **When listing steps or guidelines, format them as a numbered list with appropriate markdown formatting.** The final answer should be coherent, self-contained, and end with a complete sentence.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
Context:
|
| 117 |
{context}
|
| 118 |
Question:
|
|
@@ -136,8 +189,9 @@ max_tokens = 500
|
|
| 136 |
rag_chain, message = create_rag_pipeline(file_paths, model, temperature, max_tokens)
|
| 137 |
|
| 138 |
def answer_question(model, temperature, max_tokens, question):
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
if rag_chain is None:
|
| 142 |
return "The system is currently unavailable. Please try again later."
|
| 143 |
try:
|
|
|
|
| 67 |
return ' '.join(s.strip() for s in sentences)
|
| 68 |
return text
|
| 69 |
|
| 70 |
+
# --- Added: Handling "Not Feasible" Keywords and Gibberish Inputs ---
|
| 71 |
def is_valid_input(text):
|
| 72 |
+
"""
|
| 73 |
+
Validate the user's input question.
|
| 74 |
+
Returns a tuple (is_valid, message).
|
| 75 |
+
"""
|
| 76 |
if not text or text.strip() == "":
|
| 77 |
+
return False, "Input cannot be empty. Please provide a meaningful question."
|
| 78 |
+
|
| 79 |
if not re.search('[A-Za-z]', text):
|
| 80 |
+
return False, "Input must contain alphabetic characters."
|
| 81 |
+
|
| 82 |
if len(text.strip()) < 5:
|
| 83 |
+
return False, "Input is too short. Please provide a more detailed question."
|
| 84 |
+
|
| 85 |
+
# Define not feasible keywords
|
| 86 |
+
not_feasible_keywords = [
|
| 87 |
+
"illegal", "harmful", "dangerous", "unethical", "inappropriate",
|
| 88 |
+
"forbidden", "restricted", "banned", "prohibited", "secret"
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
# Check for not feasible keywords (case-insensitive)
|
| 92 |
+
pattern = re.compile(r'\b(' + '|'.join(not_feasible_keywords) + r')\b', re.IGNORECASE)
|
| 93 |
+
if pattern.search(text):
|
| 94 |
+
return False, "Your question contains restricted or inappropriate content. Please modify your query."
|
| 95 |
+
|
| 96 |
+
# --- Added: Gibberish Detection ---
|
| 97 |
+
# Simple heuristic: Check the ratio of alphabetic characters to total characters
|
| 98 |
+
total_chars = len(text)
|
| 99 |
+
alpha_chars = len(re.findall(r'[A-Za-z]', text))
|
| 100 |
+
ratio = alpha_chars / total_chars if total_chars > 0 else 0
|
| 101 |
+
|
| 102 |
+
if ratio < 0.6:
|
| 103 |
+
return False, "Your input appears to be gibberish or nonsensical. Please enter a clear and meaningful question."
|
| 104 |
+
|
| 105 |
+
# Additionally, check for a minimum number of recognizable words
|
| 106 |
+
words = re.findall(r'\b\w+\b', text)
|
| 107 |
+
recognized_words = [word for word in words if word.lower() in recognized_words_set]
|
| 108 |
+
|
| 109 |
+
if len(recognized_words) < max(3, len(words) * 0.4):
|
| 110 |
+
return False, "Your input contains too many unrecognizable words. Please enter a clear and meaningful question."
|
| 111 |
+
|
| 112 |
+
return True, "Valid input."
|
| 113 |
+
|
| 114 |
+
# Predefined set of common English words for basic gibberish detection
|
| 115 |
+
# In a production environment, consider using a more comprehensive dictionary or language model
|
| 116 |
+
recognized_words_set = set([
|
| 117 |
+
'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'I',
|
| 118 |
+
'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at',
|
| 119 |
+
'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her',
|
| 120 |
+
'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there',
|
| 121 |
+
'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get',
|
| 122 |
+
'which', 'go', 'me'
|
| 123 |
+
# Add more words as needed
|
| 124 |
+
])
|
| 125 |
|
| 126 |
def initialize_llm(model, temperature, max_tokens):
|
| 127 |
prompt_allocation = int(max_tokens * 0.2)
|
|
|
|
| 156 |
|
| 157 |
retriever = vectorstore.as_retriever()
|
| 158 |
|
| 159 |
+
# --- Improved Prompt Template ---
|
| 160 |
custom_prompt_template = PromptTemplate(
|
| 161 |
input_variables=["context", "question"],
|
| 162 |
template="""
|
| 163 |
You are an AI assistant specialized in daily wellness. Provide a concise, thorough, and stand-alone answer to the user's question based on the given context. Include relevant examples or schedules where beneficial. **When listing steps or guidelines, format them as a numbered list with appropriate markdown formatting.** The final answer should be coherent, self-contained, and end with a complete sentence.
|
| 164 |
+
|
| 165 |
+
If the question contains restricted or inappropriate content, respond with a polite message indicating that you cannot assist with that request.
|
| 166 |
+
|
| 167 |
+
If the question appears to be gibberish or nonsensical, respond with a polite message requesting clarification or a more coherent question.
|
| 168 |
+
|
| 169 |
Context:
|
| 170 |
{context}
|
| 171 |
Question:
|
|
|
|
| 189 |
rag_chain, message = create_rag_pipeline(file_paths, model, temperature, max_tokens)
|
| 190 |
|
| 191 |
def answer_question(model, temperature, max_tokens, question):
|
| 192 |
+
is_valid, message = is_valid_input(question)
|
| 193 |
+
if not is_valid:
|
| 194 |
+
return message
|
| 195 |
if rag_chain is None:
|
| 196 |
return "The system is currently unavailable. Please try again later."
|
| 197 |
try:
|