Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,103 +13,108 @@ import requests
|
|
| 13 |
from io import StringIO
|
| 14 |
from pdfminer.high_level import extract_text_to_fp
|
| 15 |
from pdfminer.layout import LAParams
|
| 16 |
-
from twilio.base.exceptions import TwilioRestException
|
| 17 |
import pdfplumber
|
| 18 |
import datetime
|
| 19 |
import csv
|
| 20 |
-
import json
|
| 21 |
-
import re
|
| 22 |
|
| 23 |
APP_START_TIME = datetime.datetime.now(datetime.timezone.utc)
|
| 24 |
-
|
| 25 |
os.environ["PYTORCH_JIT"] = "0"
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def _extract_tables_from_page(page):
|
|
|
|
|
|
|
| 29 |
tables = page.extract_tables()
|
|
|
|
|
|
|
|
|
|
| 30 |
formatted_tables = []
|
| 31 |
for table in tables:
|
| 32 |
formatted_table = []
|
| 33 |
for row in table:
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
formatted_tables.append(formatted_table)
|
| 37 |
return formatted_tables
|
| 38 |
-
|
| 39 |
def extract_text_from_pdf(pdf_path):
|
| 40 |
text_output = StringIO()
|
| 41 |
all_tables = []
|
| 42 |
try:
|
| 43 |
with pdfplumber.open(pdf_path) as pdf:
|
| 44 |
for page in pdf.pages:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
text = page.extract_text()
|
| 47 |
if text:
|
| 48 |
text_output.write(text + "\n\n")
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"pdfplumber
|
|
|
|
| 51 |
with open(pdf_path, 'rb') as file:
|
| 52 |
-
extract_text_to_fp(file, text_output, laparams=LAParams(), output_type='text')
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def _format_tables_internal(tables):
|
|
|
|
|
|
|
| 56 |
formatted_tables_str = []
|
| 57 |
for table in tables:
|
|
|
|
| 58 |
with StringIO() as csvfile:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
formatted_tables_str.append(csvfile.getvalue())
|
| 62 |
return "\n\n".join(formatted_tables_str)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
return '\n'.join(' '.join(line.strip().split()) for line in text.splitlines() if line.strip())
|
| 66 |
-
|
| 67 |
def extract_text_from_docx(docx_path):
|
| 68 |
try:
|
| 69 |
doc = docx.Document(docx_path)
|
| 70 |
return '\n'.join(para.text for para in doc.paragraphs)
|
| 71 |
-
except:
|
| 72 |
return ""
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
with open(json_path, 'r', encoding='utf-8') as f:
|
| 77 |
-
data = json.load(f)
|
| 78 |
-
if isinstance(data, dict):
|
| 79 |
-
# Flatten dictionary values (avoiding nested structures as strings)
|
| 80 |
-
return "\n".join(f"{key}: {value}" for key, value in data.items() if not isinstance(value, (dict, list)))
|
| 81 |
-
elif isinstance(data, list):
|
| 82 |
-
# Flatten list of dictionaries
|
| 83 |
-
all_items = []
|
| 84 |
-
for item in data:
|
| 85 |
-
if isinstance(item, dict):
|
| 86 |
-
all_items.append("\n".join(f"{key}: {value}" for key, value in item.items() if not isinstance(value, (dict, list))))
|
| 87 |
-
return "\n\n".join(all_items)
|
| 88 |
-
else:
|
| 89 |
-
return json.dumps(data, ensure_ascii=False, indent=2)
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"JSON read error: {e}")
|
| 92 |
-
return ""
|
| 93 |
-
|
| 94 |
-
# ---------------- Chunking ----------------
|
| 95 |
-
def chunk_text(text, tokenizer, chunk_size=128, chunk_overlap=32):
|
| 96 |
tokens = tokenizer.tokenize(text)
|
| 97 |
chunks = []
|
| 98 |
start = 0
|
| 99 |
while start < len(tokens):
|
| 100 |
end = min(start + chunk_size, len(tokens))
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
start += chunk_size - chunk_overlap
|
| 105 |
return chunks
|
| 106 |
|
| 107 |
def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
| 108 |
-
|
| 109 |
-
D, I = index.search(np.array([
|
| 110 |
return [text_chunks[i] for i in I[0]]
|
| 111 |
|
| 112 |
-
#
|
| 113 |
def generate_answer_with_groq(question, context):
|
| 114 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 115 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
@@ -119,9 +124,8 @@ def generate_answer_with_groq(question, context):
|
|
| 119 |
}
|
| 120 |
prompt = (
|
| 121 |
f"Customer asked: '{question}'\n\n"
|
| 122 |
-
f"Here is the relevant
|
| 123 |
-
f"Respond in a friendly and helpful tone as a toy shop support agent
|
| 124 |
-
f"addressing the customer by their name if it's available in the context."
|
| 125 |
)
|
| 126 |
payload = {
|
| 127 |
"model": "llama3-8b-8192",
|
|
@@ -129,11 +133,9 @@ def generate_answer_with_groq(question, context):
|
|
| 129 |
{
|
| 130 |
"role": "system",
|
| 131 |
"content": (
|
| 132 |
-
"You are ToyBot, a friendly WhatsApp assistant for an online toy shop. "
|
| 133 |
-
"
|
| 134 |
-
"
|
| 135 |
-
"and address them directly. If the context contains order details and status, "
|
| 136 |
-
"include that information in your response."
|
| 137 |
)
|
| 138 |
},
|
| 139 |
{"role": "user", "content": prompt},
|
|
@@ -145,10 +147,9 @@ def generate_answer_with_groq(question, context):
|
|
| 145 |
response.raise_for_status()
|
| 146 |
return response.json()['choices'][0]['message']['content'].strip()
|
| 147 |
|
| 148 |
-
#
|
| 149 |
def fetch_latest_incoming_message(client, conversation_sid):
|
| 150 |
try:
|
| 151 |
-
print(f"fetch_latest_incoming_message Twilio SID : {conversation_sid}")
|
| 152 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
| 153 |
for msg in reversed(messages):
|
| 154 |
if msg.author.startswith("whatsapp:"):
|
|
@@ -159,114 +160,136 @@ def fetch_latest_incoming_message(client, conversation_sid):
|
|
| 159 |
"timestamp": msg.date_created,
|
| 160 |
}
|
| 161 |
except TwilioRestException as e:
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
|
|
|
| 165 |
|
| 166 |
def send_twilio_message(client, conversation_sid, body):
|
| 167 |
return client.conversations.v1.conversations(conversation_sid).messages.create(
|
| 168 |
author="system", body=body
|
| 169 |
)
|
| 170 |
|
| 171 |
-
#
|
| 172 |
def setup_knowledge_base():
|
| 173 |
folder_path = "docs"
|
| 174 |
all_text = ""
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
|
|
|
| 198 |
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 199 |
chunks = chunk_text(all_text, tokenizer)
|
| 200 |
model = SentenceTransformer('all-mpnet-base-v2')
|
| 201 |
-
embeddings = model.encode(chunks, show_progress_bar=False)
|
| 202 |
dim = embeddings[0].shape[0]
|
| 203 |
index = faiss.IndexFlatL2(dim)
|
| 204 |
index.add(np.array(embeddings).astype('float32'))
|
| 205 |
return index, model, chunks
|
| 206 |
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
def start_conversation_monitor(client, index, embed_model, text_chunks):
|
| 209 |
processed_convos = set()
|
| 210 |
last_processed_timestamp = {}
|
| 211 |
|
| 212 |
-
def
|
| 213 |
-
print(f"π§΅ Started polling for SID: {convo_sid}")
|
| 214 |
while True:
|
| 215 |
try:
|
| 216 |
latest_msg = fetch_latest_incoming_message(client, convo_sid)
|
| 217 |
if latest_msg:
|
| 218 |
msg_time = latest_msg["timestamp"]
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
if prev_time is None or msg_time > prev_time:
|
| 222 |
last_processed_timestamp[convo_sid] = msg_time
|
| 223 |
question = latest_msg["body"]
|
| 224 |
sender = latest_msg["author"]
|
| 225 |
-
print(f"
|
| 226 |
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
| 227 |
answer = generate_answer_with_groq(question, context)
|
| 228 |
send_twilio_message(client, convo_sid, answer)
|
|
|
|
|
|
|
| 229 |
except Exception as e:
|
| 230 |
-
print(f"
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
# Get all conversations and find the most recent one after APP_START_TIME
|
| 234 |
-
conversations = client.conversations.v1.conversations.list(limit=20)
|
| 235 |
-
print("Conversations (date_created) sorted in descending order:")
|
| 236 |
-
for c in sorted_convos:
|
| 237 |
-
print(f"Date: {c.date_created}, ID: {c.sid}")
|
| 238 |
-
sorted_convos = sorted(
|
| 239 |
-
[c for c in conversations if c.date_created > APP_START_TIME],
|
| 240 |
-
key=lambda c: c.date_created,
|
| 241 |
-
reverse=True
|
| 242 |
-
)
|
| 243 |
|
| 244 |
-
|
| 245 |
-
print("
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
processed_convos.add(latest_convo.sid)
|
| 251 |
-
print(f"β
Monitoring latest conversation SID: {latest_convo.sid}, Created: {latest_convo.date_created}")
|
| 252 |
-
threading.Thread(target=poll_convo, args=(latest_convo.sid,), daemon=True).start()
|
| 253 |
|
| 254 |
|
| 255 |
-
# ---------------- Main Entry ----------------
|
| 256 |
-
if __name__ == "__main__":
|
| 257 |
-
st.title("π€ ToyBot WhatsApp Assistant")
|
| 258 |
-
st.write("Initializing knowledge base...")
|
| 259 |
|
| 260 |
-
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from io import StringIO
|
| 14 |
from pdfminer.high_level import extract_text_to_fp
|
| 15 |
from pdfminer.layout import LAParams
|
| 16 |
+
from twilio.base.exceptions import TwilioRestException # Add this at the top
|
| 17 |
import pdfplumber
|
| 18 |
import datetime
|
| 19 |
import csv
|
|
|
|
|
|
|
| 20 |
|
| 21 |
APP_START_TIME = datetime.datetime.now(datetime.timezone.utc)
|
| 22 |
+
|
| 23 |
os.environ["PYTORCH_JIT"] = "0"
|
| 24 |
|
| 25 |
+
# --- PDF Extraction ---
|
| 26 |
def _extract_tables_from_page(page):
|
| 27 |
+
"""Extracts tables from a single page of a PDF."""
|
| 28 |
+
|
| 29 |
tables = page.extract_tables()
|
| 30 |
+
if not tables:
|
| 31 |
+
return []
|
| 32 |
+
|
| 33 |
formatted_tables = []
|
| 34 |
for table in tables:
|
| 35 |
formatted_table = []
|
| 36 |
for row in table:
|
| 37 |
+
if row: # Filter out empty rows
|
| 38 |
+
formatted_row = [cell if cell is not None else "" for cell in row] # Replace None with ""
|
| 39 |
+
formatted_table.append(formatted_row)
|
| 40 |
+
else:
|
| 41 |
+
formatted_table.append([""]) # Append an empty row if the row is None
|
| 42 |
formatted_tables.append(formatted_table)
|
| 43 |
return formatted_tables
|
| 44 |
+
|
| 45 |
def extract_text_from_pdf(pdf_path):
|
| 46 |
text_output = StringIO()
|
| 47 |
all_tables = []
|
| 48 |
try:
|
| 49 |
with pdfplumber.open(pdf_path) as pdf:
|
| 50 |
for page in pdf.pages:
|
| 51 |
+
# Extract tables
|
| 52 |
+
page_tables = _extract_tables_from_page(page)
|
| 53 |
+
if page_tables:
|
| 54 |
+
all_tables.extend(page_tables)
|
| 55 |
+
# Extract text
|
| 56 |
text = page.extract_text()
|
| 57 |
if text:
|
| 58 |
text_output.write(text + "\n\n")
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"Error extracting with pdfplumber: {e}")
|
| 61 |
+
# Fallback to pdfminer if pdfplumber fails
|
| 62 |
with open(pdf_path, 'rb') as file:
|
| 63 |
+
extract_text_to_fp(file, text_output, laparams=LAParams(), output_type='text', codec=None)
|
| 64 |
+
extracted_text = text_output.getvalue()
|
| 65 |
+
return extracted_text, all_tables # Return text and list of tables
|
| 66 |
+
|
| 67 |
+
def clean_extracted_text(text):
|
| 68 |
+
lines = text.splitlines()
|
| 69 |
+
cleaned = []
|
| 70 |
+
for line in lines:
|
| 71 |
+
line = line.strip()
|
| 72 |
+
if line:
|
| 73 |
+
line = ' '.join(line.split())
|
| 74 |
+
cleaned.append(line)
|
| 75 |
+
return '\n'.join(cleaned)
|
| 76 |
|
| 77 |
def _format_tables_internal(tables):
|
| 78 |
+
"""Formats extracted tables into a string representation."""
|
| 79 |
+
|
| 80 |
formatted_tables_str = []
|
| 81 |
for table in tables:
|
| 82 |
+
# Use csv writer to handle commas and quotes correctly
|
| 83 |
with StringIO() as csvfile:
|
| 84 |
+
csvwriter = csv.writer(csvfile)
|
| 85 |
+
csvwriter.writerows(table)
|
| 86 |
formatted_tables_str.append(csvfile.getvalue())
|
| 87 |
return "\n\n".join(formatted_tables_str)
|
| 88 |
|
| 89 |
+
# --- DOCX Extraction ---
|
|
|
|
|
|
|
| 90 |
def extract_text_from_docx(docx_path):
|
| 91 |
try:
|
| 92 |
doc = docx.Document(docx_path)
|
| 93 |
return '\n'.join(para.text for para in doc.paragraphs)
|
| 94 |
+
except Exception:
|
| 95 |
return ""
|
| 96 |
|
| 97 |
+
# --- Chunking ---
|
| 98 |
+
def chunk_text(text, tokenizer, chunk_size=128, chunk_overlap=32, max_tokens=512):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
tokens = tokenizer.tokenize(text)
|
| 100 |
chunks = []
|
| 101 |
start = 0
|
| 102 |
while start < len(tokens):
|
| 103 |
end = min(start + chunk_size, len(tokens))
|
| 104 |
+
chunk_tokens = tokens[start:end]
|
| 105 |
+
chunk_text = tokenizer.convert_tokens_to_string(chunk_tokens)
|
| 106 |
+
chunks.append(chunk_text)
|
| 107 |
+
if end == len(tokens):
|
| 108 |
+
break
|
| 109 |
start += chunk_size - chunk_overlap
|
| 110 |
return chunks
|
| 111 |
|
| 112 |
def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
| 113 |
+
question_embedding = embed_model.encode(question)
|
| 114 |
+
D, I = index.search(np.array([question_embedding]), k)
|
| 115 |
return [text_chunks[i] for i in I[0]]
|
| 116 |
|
| 117 |
+
# --- Groq Answer Generator ---
|
| 118 |
def generate_answer_with_groq(question, context):
|
| 119 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 120 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 124 |
}
|
| 125 |
prompt = (
|
| 126 |
f"Customer asked: '{question}'\n\n"
|
| 127 |
+
f"Here is the relevant product or policy info to help:\n{context}\n\n"
|
| 128 |
+
f"Respond in a friendly and helpful tone as a toy shop support agent."
|
|
|
|
| 129 |
)
|
| 130 |
payload = {
|
| 131 |
"model": "llama3-8b-8192",
|
|
|
|
| 133 |
{
|
| 134 |
"role": "system",
|
| 135 |
"content": (
|
| 136 |
+
"You are ToyBot, a friendly and helpful WhatsApp assistant for an online toy shop. "
|
| 137 |
+
"Your goal is to politely answer customer questions, help them choose the right toys, "
|
| 138 |
+
"provide order or delivery information, explain return policies, and guide them through purchases."
|
|
|
|
|
|
|
| 139 |
)
|
| 140 |
},
|
| 141 |
{"role": "user", "content": prompt},
|
|
|
|
| 147 |
response.raise_for_status()
|
| 148 |
return response.json()['choices'][0]['message']['content'].strip()
|
| 149 |
|
| 150 |
+
# --- Twilio Functions ---
|
| 151 |
def fetch_latest_incoming_message(client, conversation_sid):
|
| 152 |
try:
|
|
|
|
| 153 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
| 154 |
for msg in reversed(messages):
|
| 155 |
if msg.author.startswith("whatsapp:"):
|
|
|
|
| 160 |
"timestamp": msg.date_created,
|
| 161 |
}
|
| 162 |
except TwilioRestException as e:
|
| 163 |
+
if e.status == 404:
|
| 164 |
+
print(f"Conversation {conversation_sid} not found, skipping...")
|
| 165 |
+
else:
|
| 166 |
+
print(f"Twilio error fetching messages for {conversation_sid}:", e)
|
| 167 |
+
except Exception as e:
|
| 168 |
+
#print(f"Unexpected error in fetch_latest_incoming_message for {conversation_sid}:", e)
|
| 169 |
+
pass
|
| 170 |
|
| 171 |
+
return None
|
| 172 |
|
| 173 |
def send_twilio_message(client, conversation_sid, body):
|
| 174 |
return client.conversations.v1.conversations(conversation_sid).messages.create(
|
| 175 |
author="system", body=body
|
| 176 |
)
|
| 177 |
|
| 178 |
+
# --- Load Knowledge Base ---
|
| 179 |
def setup_knowledge_base():
|
| 180 |
folder_path = "docs"
|
| 181 |
all_text = ""
|
| 182 |
|
| 183 |
+
# Process PDFs
|
| 184 |
+
for filename in ["FAQ.pdf", "ProductReturnPolicy.pdf"]:
|
| 185 |
+
pdf_path = os.path.join(folder_path, filename)
|
| 186 |
+
text, tables = extract_text_from_pdf(pdf_path)
|
| 187 |
+
all_text += clean_extracted_text(text) + "\n"
|
| 188 |
+
all_text += _format_tables_internal(tables) + "\n"
|
| 189 |
+
|
| 190 |
+
# Process CSVs
|
| 191 |
+
for filename in ["CustomerOrders.csv"]:
|
| 192 |
+
csv_path = os.path.join(folder_path, filename)
|
| 193 |
+
try:
|
| 194 |
+
with open(csv_path, newline='', encoding='utf-8') as csvfile:
|
| 195 |
+
reader = csv.DictReader(csvfile)
|
| 196 |
+
for row in reader:
|
| 197 |
+
line = f"Order ID: {row.get('OrderID')} | Customer Name: {row.get('CustomerName')} | Order Date: {row.get('OrderDate')} | ProductID: {row.get('ProductID')} | Date: {row.get('OrderDate')} | Quantity: {row.get('Quantity')} | UnitPrice(USD): {row.get('UnitPrice(USD)')} | TotalPrice(USD): {row.get('TotalPrice(USD)')} | ShippingAddress: {row.get('ShippingAddress')} | OrderStatus: {row.get('OrderStatus')}"
|
| 198 |
+
all_text += line + "\n"
|
| 199 |
+
except Exception as e:
|
| 200 |
+
print(f"β Error reading {filename}: {e}")
|
| 201 |
+
|
| 202 |
+
for filename in ["Products.csv"]:
|
| 203 |
+
csv_path = os.path.join(folder_path, filename)
|
| 204 |
+
try:
|
| 205 |
+
with open(csv_path, newline='', encoding='utf-8') as csvfile:
|
| 206 |
+
reader = csv.DictReader(csvfile)
|
| 207 |
+
for row in reader:
|
| 208 |
+
line = f"Product ID: {row.get('ProductID')} | Toy Name: {row.get('ToyName')} | Category: {row.get('Category')} | Price(USD): {row.get('Price(USD)')} | Stock Quantity: {row.get('StockQuantity')} | Description: {row.get('Description')}"
|
| 209 |
+
all_text += line + "\n"
|
| 210 |
+
except Exception as e:
|
| 211 |
+
print(f"β Error reading {filename}: {e}")
|
| 212 |
|
| 213 |
+
# Tokenization & chunking
|
| 214 |
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 215 |
chunks = chunk_text(all_text, tokenizer)
|
| 216 |
model = SentenceTransformer('all-mpnet-base-v2')
|
| 217 |
+
embeddings = model.encode(chunks, show_progress_bar=False, truncation=True, max_length=512)
|
| 218 |
dim = embeddings[0].shape[0]
|
| 219 |
index = faiss.IndexFlatL2(dim)
|
| 220 |
index.add(np.array(embeddings).astype('float32'))
|
| 221 |
return index, model, chunks
|
| 222 |
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
# --- Monitor Conversations ---
|
| 226 |
def start_conversation_monitor(client, index, embed_model, text_chunks):
|
| 227 |
processed_convos = set()
|
| 228 |
last_processed_timestamp = {}
|
| 229 |
|
| 230 |
+
def poll_conversation(convo_sid):
|
|
|
|
| 231 |
while True:
|
| 232 |
try:
|
| 233 |
latest_msg = fetch_latest_incoming_message(client, convo_sid)
|
| 234 |
if latest_msg:
|
| 235 |
msg_time = latest_msg["timestamp"]
|
| 236 |
+
if convo_sid not in last_processed_timestamp or msg_time > last_processed_timestamp[convo_sid]:
|
|
|
|
|
|
|
| 237 |
last_processed_timestamp[convo_sid] = msg_time
|
| 238 |
question = latest_msg["body"]
|
| 239 |
sender = latest_msg["author"]
|
| 240 |
+
print(f"\nπ₯ New message from {sender} in {convo_sid}: {question}")
|
| 241 |
context = "\n\n".join(retrieve_chunks(question, index, embed_model, text_chunks))
|
| 242 |
answer = generate_answer_with_groq(question, context)
|
| 243 |
send_twilio_message(client, convo_sid, answer)
|
| 244 |
+
print(f"π€ Replied to {sender}: {answer}")
|
| 245 |
+
time.sleep(3)
|
| 246 |
except Exception as e:
|
| 247 |
+
print(f"β Error in convo {convo_sid} polling:", e)
|
| 248 |
+
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
def poll_new_conversations():
|
| 251 |
+
print("β‘οΈ Monitoring for new WhatsApp conversations...")
|
| 252 |
+
while True:
|
| 253 |
+
try:
|
| 254 |
+
conversations = client.conversations.v1.conversations.list(limit=20)
|
| 255 |
+
for convo in conversations:
|
| 256 |
+
convo_full = client.conversations.v1.conversations(convo.sid).fetch()
|
| 257 |
+
if convo.sid not in processed_convos and convo_full.date_created > APP_START_TIME:
|
| 258 |
+
participants = client.conversations.v1.conversations(convo.sid).participants.list()
|
| 259 |
+
for p in participants:
|
| 260 |
+
address = p.messaging_binding.get("address", "") if p.messaging_binding else ""
|
| 261 |
+
if address.startswith("whatsapp:"):
|
| 262 |
+
print(f"π New WhatsApp convo found: {convo.sid}")
|
| 263 |
+
processed_convos.add(convo.sid)
|
| 264 |
+
threading.Thread(target=poll_conversation, args=(convo.sid,), daemon=True).start()
|
| 265 |
+
except Exception as e:
|
| 266 |
+
print("β Error polling conversations:", e)
|
| 267 |
+
time.sleep(5)
|
| 268 |
|
| 269 |
+
# β
Launch conversation polling monitor
|
| 270 |
+
threading.Thread(target=poll_new_conversations, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
# --- Streamlit UI ---
|
| 275 |
+
st.set_page_config(page_title="Quasa β A Smart WhatsApp Chatbot", layout="wide")
|
| 276 |
+
st.title("π± Quasa β A Smart WhatsApp Chatbot")
|
| 277 |
|
| 278 |
+
account_sid = st.secrets.get("TWILIO_SID")
|
| 279 |
+
auth_token = st.secrets.get("TWILIO_TOKEN")
|
| 280 |
+
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
| 281 |
+
|
| 282 |
+
if not all([account_sid, auth_token, GROQ_API_KEY]):
|
| 283 |
+
st.warning("β οΈ Provide all credentials below:")
|
| 284 |
+
account_sid = st.text_input("Twilio SID", value=account_sid or "")
|
| 285 |
+
auth_token = st.text_input("Twilio Token", type="password", value=auth_token or "")
|
| 286 |
+
GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
| 287 |
+
|
| 288 |
+
if all([account_sid, auth_token, GROQ_API_KEY]):
|
| 289 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 290 |
+
client = Client(account_sid, auth_token)
|
| 291 |
+
|
| 292 |
+
st.success("π’ Monitoring new WhatsApp conversations...")
|
| 293 |
+
index, model, chunks = setup_knowledge_base()
|
| 294 |
+
threading.Thread(target=start_conversation_monitor, args=(client, index, model, chunks), daemon=True).start()
|
| 295 |
+
st.info("β³ Waiting for new messages...")
|