VectorV17 commited on
Commit
6ecb25f
·
verified ·
1 Parent(s): afc6502

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -3,7 +3,7 @@ from huggingface_hub import InferenceClient
3
  from datetime import datetime
4
  import re
5
 
6
- # Import localized NLP and Math Matching frameworks
7
  import nltk
8
  from sklearn.feature_extraction.text import TfidfVectorizer
9
  from sklearn.metrics.pairwise import cosine_similarity
@@ -84,7 +84,6 @@ def get_thematic_datetime():
84
  if 11 <= day_int <= 13:
85
  suffix = "th"
86
  else:
87
- # FIXED: Changed ._get to .get
88
  suffix = {1: "st", 2: "nd", 3: "rd"}.get(day_int % 10, "th")
89
 
90
  formatted_date = f"{now.strftime('%d')}{suffix} {now.strftime('%B %Y')} | Time: {now.strftime('%I:%M:%S %p').lower()}"
@@ -133,11 +132,18 @@ def bot_side(history, system_message, max_tokens, temperature, top_p):
133
 
134
  faq_context_injection = ""
135
 
136
- if matched_faq and match_score > 0.15:
137
- nlp_metadata_prefix = f"🔍 *NLP Match Found (Cosine Similarity Confidence: {match_score*100:.1f}%):*\n> **Q: {matched_faq['q']}**\n> A: {matched_faq['a']}\n\n---\n"
138
- faq_context_injection = f"Use this highly relevant matched FAQ data to build your answer: Question: {matched_faq['q']} | Answer: {matched_faq['a']}"
 
 
 
 
 
 
139
  else:
140
- nlp_metadata_prefix = "🔍 *NLP Search Notice: No direct close FAQ match found. Defaulting to general system inference knowledge.*\n\n---\n"
 
141
 
142
  full_system_prompt = (
143
  f"{system_message}\n\n"
@@ -159,6 +165,7 @@ def bot_side(history, system_message, max_tokens, temperature, top_p):
159
  messages.append({"role": "user", "content": user_query})
160
  clean_history.append({"role": "user", "content": user_query})
161
 
 
162
  clean_history.append({"role": "assistant", "content": nlp_metadata_prefix})
163
 
164
  try:
@@ -223,7 +230,7 @@ body.dark #header-container * { color: #f9fafb !important; }
223
  }
224
  """
225
 
226
- with gr.Blocks() as demo:
227
 
228
  # 1. Header Section
229
  with gr.Row(elem_id="header-container"):
@@ -313,7 +320,7 @@ with gr.Blocks() as demo:
313
  bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
314
  )
315
 
316
- # Initialize continuous running digital time clock loop
317
  clock_timer = gr.Timer(1.0)
318
  clock_timer.tick(fn=get_thematic_datetime, outputs=time_display, queue=False)
319
 
 
3
  from datetime import datetime
4
  import re
5
 
6
+ # Import NLP and Math Matching frameworks
7
  import nltk
8
  from sklearn.feature_extraction.text import TfidfVectorizer
9
  from sklearn.metrics.pairwise import cosine_similarity
 
84
  if 11 <= day_int <= 13:
85
  suffix = "th"
86
  else:
 
87
  suffix = {1: "st", 2: "nd", 3: "rd"}.get(day_int % 10, "th")
88
 
89
  formatted_date = f"{now.strftime('%d')}{suffix} {now.strftime('%B %Y')} | Time: {now.strftime('%I:%M:%S %p').lower()}"
 
132
 
133
  faq_context_injection = ""
134
 
135
+ # RECTIFIED LOGIC: The metadata line only outlines telemetry metrics. It doesn't dump the duplicate answer text block.
136
+ if matched_faq and match_score > 0.20:
137
+ nlp_metadata_prefix = f"🔍 *NLP Intent Match: \"{matched_faq['q']}\" (Confidence: {match_score*100:.1f}%)*\n\n"
138
+ faq_context_injection = (
139
+ f"The user's query closely matches this curated FAQ entry:\n"
140
+ f"Question: {matched_faq['q']}\n"
141
+ f"Answer: {matched_faq['a']}\n"
142
+ f"Please output this answer clearly and conversationally. Do not wrap it in quotes."
143
+ )
144
  else:
145
+ nlp_metadata_prefix = "🔍 *NLP Search Notice: No close database FAQ match found. Processing via general knowledge base...*\n\n"
146
+ faq_context_injection = "No matching database entry found. Answer the question directly using standard general system knowledge."
147
 
148
  full_system_prompt = (
149
  f"{system_message}\n\n"
 
165
  messages.append({"role": "user", "content": user_query})
166
  clean_history.append({"role": "user", "content": user_query})
167
 
168
+ # Pre-populate the visualization header block securely
169
  clean_history.append({"role": "assistant", "content": nlp_metadata_prefix})
170
 
171
  try:
 
230
  }
231
  """
232
 
233
+ with gr.Blocks(css=custom_css) as demo:
234
 
235
  # 1. Header Section
236
  with gr.Row(elem_id="header-container"):
 
320
  bot_side, inputs=[chatbot, system_msg, max_tokens, temperature, top_p], outputs=[chatbot]
321
  )
322
 
323
+ # Running digital time clock update loop
324
  clock_timer = gr.Timer(1.0)
325
  clock_timer.tick(fn=get_thematic_datetime, outputs=time_display, queue=False)
326