masadonline commited on
Commit
aaea35e
Β·
verified Β·
1 Parent(s): 465fc05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -43,11 +43,16 @@ def extract_text_from_docx(docx_path):
43
 
44
  # --- Chunking & Retrieval ---
45
  def chunk_text(text, tokenizer, chunk_size=128, chunk_overlap=32, max_tokens=512):
46
-
47
-
48
  tokens = tokenizer.tokenize(text)
49
  chunks = []
50
  start = 0
 
 
 
 
 
 
 
51
  start += chunk_size - chunk_overlap
52
  return chunks
53
 
@@ -99,12 +104,11 @@ def get_latest_whatsapp_conversation_sid(client):
99
  if (p.identity and p.identity.startswith("whatsapp:")) or (
100
  p.messaging_binding and p.messaging_binding.get("address", "").startswith("whatsapp:")
101
  ):
102
- return convo.sid # Only return the latest one
103
  except:
104
  continue
105
  return None
106
 
107
-
108
  def fetch_latest_incoming_message(client, conversation_sid):
109
  messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
110
  for msg in reversed(messages):
@@ -169,10 +173,11 @@ def start_conversation_monitor(client, index, embed_model, text_chunks, specific
169
  time.sleep(5)
170
 
171
  def monitor_all_conversations():
172
- if specific_sid:
173
- print(f"➑️ Monitoring only the latest conversation: {specific_sid}")
174
- threading.Thread(target=poll_conversation, args=(specific_sid,), daemon=True).start()
175
 
 
176
 
177
  # --- Streamlit UI ---
178
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
@@ -193,14 +198,10 @@ if all([account_sid, auth_token, GROQ_API_KEY]):
193
  client = Client(account_sid, auth_token)
194
  latest_sid = get_latest_whatsapp_conversation_sid(client)
195
 
196
- latest_sid = get_latest_whatsapp_conversation_sid(client)
197
-
198
- if latest_sid:
199
- st.success(f"βœ… Monitoring latest WhatsApp conversation: {latest_sid}")
200
- index, model, chunks = setup_knowledge_base()
201
- # Start monitoring only this conversation
202
- threading.Thread(target=start_conversation_monitor, args=(client, index, model, chunks, latest_sid), daemon=True).start()
203
- st.success("🟒 Chatbot is running in background and will reply to the latest conversation.")
204
- else:
205
- st.error("❌ No WhatsApp conversations found.")
206
-
 
43
 
44
  # --- Chunking & Retrieval ---
45
  def chunk_text(text, tokenizer, chunk_size=128, chunk_overlap=32, max_tokens=512):
 
 
46
  tokens = tokenizer.tokenize(text)
47
  chunks = []
48
  start = 0
49
+ while start < len(tokens):
50
+ end = min(start + chunk_size, len(tokens))
51
+ chunk_tokens = tokens[start:end]
52
+ chunk_text = tokenizer.convert_tokens_to_string(chunk_tokens)
53
+ chunks.append(chunk_text)
54
+ if end == len(tokens):
55
+ break
56
  start += chunk_size - chunk_overlap
57
  return chunks
58
 
 
104
  if (p.identity and p.identity.startswith("whatsapp:")) or (
105
  p.messaging_binding and p.messaging_binding.get("address", "").startswith("whatsapp:")
106
  ):
107
+ return convo.sid
108
  except:
109
  continue
110
  return None
111
 
 
112
  def fetch_latest_incoming_message(client, conversation_sid):
113
  messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
114
  for msg in reversed(messages):
 
173
  time.sleep(5)
174
 
175
  def monitor_all_conversations():
176
+ if specific_sid:
177
+ print(f"➑️ Monitoring only the latest conversation: {specific_sid}")
178
+ threading.Thread(target=poll_conversation, args=(specific_sid,), daemon=True).start()
179
 
180
+ monitor_all_conversations()
181
 
182
  # --- Streamlit UI ---
183
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
 
198
  client = Client(account_sid, auth_token)
199
  latest_sid = get_latest_whatsapp_conversation_sid(client)
200
 
201
+ if latest_sid:
202
+ st.success(f"βœ… Monitoring latest WhatsApp conversation: {latest_sid}")
203
+ index, model, chunks = setup_knowledge_base()
204
+ threading.Thread(target=start_conversation_monitor, args=(client, index, model, chunks, latest_sid), daemon=True).start()
205
+ st.success("🟒 Chatbot is running in background and will reply to the latest conversation.")
206
+ else:
207
+ st.error("❌ No WhatsApp conversations found.")