AshjanMohammed commited on
Commit
98bbfe6
·
verified ·
1 Parent(s): 4ac587b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -205,39 +205,34 @@ class DefenseQAChatbot:
205
  out = self.model(**enc)
206
  emb = mean_pooling(out.last_hidden_state, enc["attention_mask"])
207
  return emb[0].cpu().numpy()
208
-
209
  def get_response(self, user_prompt, top_k=5, reject=0.55):
210
  if not user_prompt.strip():
211
  return "Please ask a question about defense protocols."
212
-
213
  q = self._embed_one(user_prompt)
214
  sims = self.response_embs @ q
215
  top = np.argpartition(-sims, min(top_k, len(sims)-1))[:top_k]
216
  top = top[np.argsort(-sims[top])]
217
  best = top[0]
218
  score = float(sims[best])
219
-
 
220
  if score < reject:
221
- return f"I couldn't find a reliable answer with sufficient confidence (score: {score:.2f}). Please try rephrasing your question."
222
-
 
223
  response_text = self.responses[best]
224
-
225
- if score >= 0.80:
226
- confidence = "🟢 High confidence"
227
- elif score >= 0.65:
228
- confidence = "🟡 Medium confidence"
229
- else:
230
- confidence = "🟠 Low confidence"
231
-
232
- return f"{response_text}\n\n---\n*{confidence} • Score: {score:.2f}*"
233
 
 
234
  # ========== Password Protection ==========
235
  def check_password():
236
  """Returns True if user entered correct password"""
237
 
238
  def password_entered():
239
  """Checks whether password is correct"""
240
- # غيّر كلمة السر هنا
241
  CORRECT_PASSWORD = "NWTC@2025"
242
 
243
  if st.session_state["password"] == CORRECT_PASSWORD:
@@ -417,6 +412,7 @@ if len(st.session_state.messages) == 0:
417
  <div style="font-size: 100px; font-weight: 700; color: #475569; margin-bottom: 20px;">
418
  NWTC
419
  </div>
 
420
  <p style="font-size: 16px; margin: 0; color: #94a3b8;">
421
  Ask your questions about: C2/STAFF, CA/PA, INTEL/RECON, MEDICAL, OE/ENVIRONMENT, or TACTICS/TECHNIQUES only.
422
  </p>
@@ -430,12 +426,23 @@ with chat_container:
430
  with st.chat_message(message["role"]):
431
  st.markdown(message["content"])
432
 
 
 
433
 
434
- # ========== Chat Input ==========
435
- if prompt := st.chat_input("Write your questions..."):
 
 
 
 
 
 
 
 
 
 
 
436
  st.session_state.messages.append({"role": "user", "content": prompt})
437
- with st.chat_message("user"):
438
- st.markdown(prompt)
439
 
440
  with st.chat_message("assistant"):
441
  with st.spinner("Analyzing..."):
@@ -443,13 +450,7 @@ if prompt := st.chat_input("Write your questions..."):
443
  st.markdown(response)
444
 
445
  st.session_state.messages.append({"role": "assistant", "content": response})
446
-
447
- # ========== Clear Button ==========
448
- col1, col2, col3 = st.columns([3, 1, 3])
449
- with col2:
450
- if st.button("🗑️ Clear Chat"):
451
- st.session_state.messages = []
452
- st.rerun()
453
 
454
  # ========== Footer ==========
455
  st.markdown("""
 
205
  out = self.model(**enc)
206
  emb = mean_pooling(out.last_hidden_state, enc["attention_mask"])
207
  return emb[0].cpu().numpy()
208
+
209
  def get_response(self, user_prompt, top_k=5, reject=0.55):
210
  if not user_prompt.strip():
211
  return "Please ask a question about defense protocols."
212
+
213
  q = self._embed_one(user_prompt)
214
  sims = self.response_embs @ q
215
  top = np.argpartition(-sims, min(top_k, len(sims)-1))[:top_k]
216
  top = top[np.argsort(-sims[top])]
217
  best = top[0]
218
  score = float(sims[best])
219
+
220
+ # إذا الثقة منخفضة جداً
221
  if score < reject:
222
+ return "I couldn't find a reliable answer. Please try rephrasing your question or ask about specific defense protocols and procedures."
223
+
224
+ # إرجاع الإجابة بدون تقييم
225
  response_text = self.responses[best]
226
+ return response_text
 
 
 
 
 
 
 
 
227
 
228
+
229
  # ========== Password Protection ==========
230
  def check_password():
231
  """Returns True if user entered correct password"""
232
 
233
  def password_entered():
234
  """Checks whether password is correct"""
235
+
236
  CORRECT_PASSWORD = "NWTC@2025"
237
 
238
  if st.session_state["password"] == CORRECT_PASSWORD:
 
412
  <div style="font-size: 100px; font-weight: 700; color: #475569; margin-bottom: 20px;">
413
  NWTC
414
  </div>
415
+
416
  <p style="font-size: 16px; margin: 0; color: #94a3b8;">
417
  Ask your questions about: C2/STAFF, CA/PA, INTEL/RECON, MEDICAL, OE/ENVIRONMENT, or TACTICS/TECHNIQUES only.
418
  </p>
 
426
  with st.chat_message(message["role"]):
427
  st.markdown(message["content"])
428
 
429
+
430
+
431
 
432
+ # ========== Chat Input & Clear Button ==========
433
+ col1, col2 = st.columns([5, 1])
434
+
435
+ with col1:
436
+ prompt = st.chat_input("Enter your defense protocol inquiry here...")
437
+
438
+
439
+ with col2:
440
+ if st.button("🗑️ Clear", use_container_width=True):
441
+ st.session_state.messages = []
442
+ st.rerun()
443
+ # Process message
444
+ if prompt:
445
  st.session_state.messages.append({"role": "user", "content": prompt})
 
 
446
 
447
  with st.chat_message("assistant"):
448
  with st.spinner("Analyzing..."):
 
450
  st.markdown(response)
451
 
452
  st.session_state.messages.append({"role": "assistant", "content": response})
453
+ st.rerun()
 
 
 
 
 
 
454
 
455
  # ========== Footer ==========
456
  st.markdown("""