VectorV17 commited on
Commit
afc6502
·
verified ·
1 Parent(s): 40690b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -15
app.py CHANGED
@@ -52,31 +52,26 @@ FAQ_DATA = [
52
 
53
  def perform_nlp_cosine_matching(user_query):
54
  """
55
- Fulfills NLP Requirements: Preprocesses text with tokenization
56
- and applies Cosine Similarity matching over TF-IDF vectors.
57
  """
58
- # Step 2: Preprocess text using NLP approaches (Lowercase + NLTK Tokenization)
59
  try:
60
  tokens = nltk.word_tokenize(user_query.lower())
61
  cleaned_query = " ".join([w for w in tokens if re.match(r'^\w+$', w)])
62
  except Exception:
63
- # Secure fallback text parsing if NLTK files fail initialization on basic CPU
64
  cleaned_query = " ".join(re.findall(r'\w+', user_query.lower()))
65
 
66
  if not cleaned_query.strip():
67
  return None, 0.0
68
 
69
- # Extract our collected question list text array
70
  questions_corpus = [faq["q"] for faq in FAQ_DATA]
71
 
72
- # Step 3: Compute mathematical similarity matrices
73
  vectorizer = TfidfVectorizer(stop_words='english')
74
  tfidf_matrix = vectorizer.fit_transform(questions_corpus)
75
 
76
  query_vector = vectorizer.transform([cleaned_query])
77
  similarity_scores = cosine_similarity(query_vector, tfidf_matrix)[0]
78
 
79
- # Find highest matching index score
80
  best_match_idx = similarity_scores.argmax()
81
  highest_score = similarity_scores[best_match_idx]
82
 
@@ -89,7 +84,8 @@ def get_thematic_datetime():
89
  if 11 <= day_int <= 13:
90
  suffix = "th"
91
  else:
92
- suffix = {1: "st", 2: "nd", 3: "rd"}._get(day_int % 10, "th")
 
93
 
94
  formatted_date = f"{now.strftime('%d')}{suffix} {now.strftime('%B %Y')} | Time: {now.strftime('%I:%M:%S %p').lower()}"
95
  return f"<div style='text-align: right; font-weight: 600; font-size: 14px; font-family: monospace; letter-spacing: 0.5px;'>{formatted_date}</div>"
@@ -132,14 +128,9 @@ def bot_side(history, system_message, max_tokens, temperature, top_p):
132
  yield history
133
  return
134
 
135
- # Extract the fresh user input message text
136
  user_query = extract_text(history[-1]["content"])
137
-
138
- # Run our NLP Cosine Matcher over our collected database
139
  matched_faq, match_score = perform_nlp_cosine_matching(user_query)
140
 
141
- # Format a visible dynamic tag noting the similarity result inside the UI panel
142
- nlp_metadata_prefix = ""
143
  faq_context_injection = ""
144
 
145
  if matched_faq and match_score > 0.15:
@@ -165,11 +156,9 @@ def bot_side(history, system_message, max_tokens, temperature, top_p):
165
  messages.append({"role": role, "content": content})
166
  clean_history.append({"role": role, "content": content})
167
 
168
- # Append user question to system message array
169
  messages.append({"role": "user", "content": user_query})
170
  clean_history.append({"role": "user", "content": user_query})
171
 
172
- # Initialize assistant output streaming sequence
173
  clean_history.append({"role": "assistant", "content": nlp_metadata_prefix})
174
 
175
  try:
 
52
 
53
  def perform_nlp_cosine_matching(user_query):
54
  """
55
+ Preprocesses text using tokenization and applies Cosine Similarity
56
+ matching over TF-IDF vectors to find the best matching FAQ.
57
  """
 
58
  try:
59
  tokens = nltk.word_tokenize(user_query.lower())
60
  cleaned_query = " ".join([w for w in tokens if re.match(r'^\w+$', w)])
61
  except Exception:
 
62
  cleaned_query = " ".join(re.findall(r'\w+', user_query.lower()))
63
 
64
  if not cleaned_query.strip():
65
  return None, 0.0
66
 
 
67
  questions_corpus = [faq["q"] for faq in FAQ_DATA]
68
 
 
69
  vectorizer = TfidfVectorizer(stop_words='english')
70
  tfidf_matrix = vectorizer.fit_transform(questions_corpus)
71
 
72
  query_vector = vectorizer.transform([cleaned_query])
73
  similarity_scores = cosine_similarity(query_vector, tfidf_matrix)[0]
74
 
 
75
  best_match_idx = similarity_scores.argmax()
76
  highest_score = similarity_scores[best_match_idx]
77
 
 
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()}"
91
  return f"<div style='text-align: right; font-weight: 600; font-size: 14px; font-family: monospace; letter-spacing: 0.5px;'>{formatted_date}</div>"
 
128
  yield history
129
  return
130
 
 
131
  user_query = extract_text(history[-1]["content"])
 
 
132
  matched_faq, match_score = perform_nlp_cosine_matching(user_query)
133
 
 
 
134
  faq_context_injection = ""
135
 
136
  if matched_faq and match_score > 0.15:
 
156
  messages.append({"role": role, "content": content})
157
  clean_history.append({"role": role, "content": content})
158
 
 
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: