Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,283 +13,201 @@ 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 |
|
21 |
APP_START_TIME = datetime.datetime.now(datetime.timezone.utc)
|
22 |
-
|
23 |
os.environ["PYTORCH_JIT"] = "0"
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
28 |
|
|
|
|
|
29 |
tables = page.extract_tables()
|
30 |
-
if not tables:
|
31 |
-
return []
|
32 |
-
|
33 |
formatted_tables = []
|
34 |
for table in tables:
|
35 |
-
|
36 |
-
|
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 |
-
|
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'
|
64 |
-
|
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 |
-
|
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 |
-
|
85 |
-
|
86 |
-
|
87 |
-
return "\n\n".join(
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
def extract_text_from_docx(docx_path):
|
91 |
try:
|
92 |
-
doc = docx.Document(
|
93 |
-
return '\n'.join(
|
94 |
-
except
|
95 |
return ""
|
96 |
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
tokens = tokenizer.tokenize(text)
|
100 |
chunks = []
|
101 |
start = 0
|
102 |
while start < len(tokens):
|
103 |
end = min(start + chunk_size, len(tokens))
|
104 |
-
|
105 |
-
|
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 |
-
|
114 |
-
D, I = index.search(np.array([
|
115 |
return [text_chunks[i] for i in I[0]]
|
116 |
|
117 |
-
#
|
118 |
def generate_answer_with_groq(question, context):
|
119 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
120 |
-
api_key = os.
|
121 |
-
headers = {
|
122 |
-
|
123 |
-
"Content-Type": "application/json",
|
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",
|
132 |
"messages": [
|
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},
|
142 |
],
|
143 |
"temperature": 0.5,
|
144 |
"max_tokens": 300,
|
145 |
}
|
146 |
response = requests.post(url, headers=headers, json=payload)
|
147 |
-
response.raise_for_status()
|
148 |
return response.json()['choices'][0]['message']['content'].strip()
|
149 |
|
150 |
-
#
|
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
|
|
|
|
|
|
|
|
|
156 |
return {
|
157 |
"sid": msg.sid,
|
158 |
"body": msg.body,
|
159 |
"author": msg.author,
|
160 |
"timestamp": msg.date_created,
|
161 |
}
|
162 |
-
except TwilioRestException
|
163 |
-
|
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 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
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 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
time.sleep(5)
|
|
|
268 |
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
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 |
-
|
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 |
-
|
289 |
-
|
290 |
-
client = Client(account_sid, auth_token)
|
291 |
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
st.
|
|
|
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 |
os.environ["PYTORCH_JIT"] = "0"
|
25 |
|
26 |
+
# Twilio Setup
|
27 |
+
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
|
28 |
+
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
|
29 |
+
twilio_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
|
30 |
|
31 |
+
# ---------------- PDF & DOCX & JSON Extraction ----------------
|
32 |
+
def _extract_tables_from_page(page):
|
33 |
tables = page.extract_tables()
|
|
|
|
|
|
|
34 |
formatted_tables = []
|
35 |
for table in tables:
|
36 |
+
formatted_row = [[cell if cell is not None else "" for cell in row] for row in table]
|
37 |
+
formatted_tables.append(formatted_row)
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
return formatted_tables
|
39 |
+
|
40 |
def extract_text_from_pdf(pdf_path):
|
41 |
text_output = StringIO()
|
42 |
all_tables = []
|
43 |
try:
|
44 |
with pdfplumber.open(pdf_path) as pdf:
|
45 |
for page in pdf.pages:
|
46 |
+
all_tables.extend(_extract_tables_from_page(page))
|
|
|
|
|
|
|
|
|
47 |
text = page.extract_text()
|
48 |
if text:
|
49 |
text_output.write(text + "\n\n")
|
50 |
except Exception as e:
|
|
|
|
|
51 |
with open(pdf_path, 'rb') as file:
|
52 |
+
extract_text_to_fp(file, text_output, laparams=LAParams(), output_type='text')
|
53 |
+
return text_output.getvalue(), all_tables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def _format_tables_internal(tables):
|
56 |
+
formatted = []
|
|
|
|
|
57 |
for table in tables:
|
|
|
58 |
with StringIO() as csvfile:
|
59 |
+
writer = csv.writer(csvfile)
|
60 |
+
writer.writerows(table)
|
61 |
+
formatted.append(csvfile.getvalue())
|
62 |
+
return "\n\n".join(formatted)
|
63 |
+
|
64 |
+
def clean_extracted_text(text):
|
65 |
+
return '\n'.join(' '.join(line.strip().split()) for line in text.splitlines() if line.strip())
|
66 |
|
67 |
+
def extract_text_from_docx(path):
|
|
|
68 |
try:
|
69 |
+
doc = docx.Document(path)
|
70 |
+
return '\n'.join(p.text for p in doc.paragraphs)
|
71 |
+
except:
|
72 |
return ""
|
73 |
|
74 |
+
def load_json_data(path):
|
75 |
+
try:
|
76 |
+
with open(path, 'r', encoding='utf-8') as f:
|
77 |
+
data = json.load(f)
|
78 |
+
if isinstance(data, dict):
|
79 |
+
return "\n".join(f"{k}: {v}" for k, v in data.items() if not isinstance(v, (dict, list)))
|
80 |
+
elif isinstance(data, list):
|
81 |
+
return "\n\n".join("\n".join(f"{k}: {v}" for k, v in item.items() if isinstance(item, dict)) for item in data)
|
82 |
+
else:
|
83 |
+
return json.dumps(data, ensure_ascii=False, indent=2)
|
84 |
+
except Exception as e:
|
85 |
+
return ""
|
86 |
+
|
87 |
+
# ---------------- Chunking ----------------
|
88 |
+
def chunk_text(text, tokenizer, chunk_size=128, chunk_overlap=32):
|
89 |
tokens = tokenizer.tokenize(text)
|
90 |
chunks = []
|
91 |
start = 0
|
92 |
while start < len(tokens):
|
93 |
end = min(start + chunk_size, len(tokens))
|
94 |
+
chunk = tokens[start:end]
|
95 |
+
chunks.append(tokenizer.convert_tokens_to_string(chunk))
|
|
|
|
|
|
|
96 |
start += chunk_size - chunk_overlap
|
97 |
return chunks
|
98 |
|
99 |
def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
100 |
+
q_embedding = embed_model.encode(question)
|
101 |
+
D, I = index.search(np.array([q_embedding]), k)
|
102 |
return [text_chunks[i] for i in I[0]]
|
103 |
|
104 |
+
# ---------------- Groq Answer Generator ----------------
|
105 |
def generate_answer_with_groq(question, context):
|
106 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
107 |
+
api_key = os.getenv("GROQ_API_KEY")
|
108 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
109 |
+
prompt = f"Customer asked: '{question}'\n\nHere is the relevant information to help:\n{context}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
payload = {
|
111 |
"model": "llama3-8b-8192",
|
112 |
"messages": [
|
113 |
+
{"role": "system", "content": "You are ToyBot, a friendly WhatsApp assistant..."},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
{"role": "user", "content": prompt},
|
115 |
],
|
116 |
"temperature": 0.5,
|
117 |
"max_tokens": 300,
|
118 |
}
|
119 |
response = requests.post(url, headers=headers, json=payload)
|
|
|
120 |
return response.json()['choices'][0]['message']['content'].strip()
|
121 |
|
122 |
+
# ---------------- Twilio Integration ----------------
|
123 |
def fetch_latest_incoming_message(client, conversation_sid):
|
124 |
try:
|
125 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list()
|
126 |
for msg in reversed(messages):
|
127 |
+
if (
|
128 |
+
msg.author.startswith("whatsapp:") and
|
129 |
+
msg.date_created and
|
130 |
+
msg.date_created > APP_START_TIME
|
131 |
+
):
|
132 |
return {
|
133 |
"sid": msg.sid,
|
134 |
"body": msg.body,
|
135 |
"author": msg.author,
|
136 |
"timestamp": msg.date_created,
|
137 |
}
|
138 |
+
except TwilioRestException:
|
139 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
def send_twilio_message(client, conversation_sid, body):
|
142 |
+
return client.conversations.v1.conversations(conversation_sid).messages.create(author="system", body=body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
def get_latest_whatsapp_conversation_sid(client):
|
145 |
+
try:
|
146 |
+
conversations = client.conversations.v1.conversations.list(limit=20)
|
147 |
+
# Filter conversations created after app start time
|
148 |
+
filtered = [
|
149 |
+
c for c in conversations
|
150 |
+
if c.date_created and c.date_created > APP_START_TIME
|
151 |
+
]
|
152 |
+
for convo in sorted(filtered, key=lambda c: c.date_created, reverse=True):
|
153 |
+
messages = convo.messages.list(limit=1)
|
154 |
+
if messages and any(m.author.startswith("whatsapp:") and m.date_created > APP_START_TIME for m in messages):
|
155 |
+
return convo.sid
|
156 |
+
except Exception as e:
|
157 |
+
print("Error fetching valid conversation SID:", e)
|
158 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
# ---------------- Knowledge Base Setup ----------------
|
161 |
+
def setup_knowledge_base():
|
162 |
+
folder = "docs"
|
163 |
+
text = ""
|
164 |
+
for f in os.listdir(folder):
|
165 |
+
path = os.path.join(folder, f)
|
166 |
+
if f.endswith(".pdf"):
|
167 |
+
t, tables = extract_text_from_pdf(path)
|
168 |
+
text += clean_extracted_text(t) + "\n" + _format_tables_internal(tables) + "\n"
|
169 |
+
elif f.endswith(".docx"):
|
170 |
+
text += clean_extracted_text(extract_text_from_docx(path)) + "\n"
|
171 |
+
elif f.endswith(".json"):
|
172 |
+
text += load_json_data(path) + "\n"
|
173 |
+
elif f.endswith(".csv"):
|
174 |
+
with open(path, newline='', encoding='utf-8') as csvfile:
|
175 |
+
reader = csv.reader(csvfile)
|
176 |
+
text += "\n".join(", ".join(row) for row in reader) + "\n"
|
177 |
+
return text
|
178 |
+
|
179 |
+
# ---------------- App Logic ----------------
|
180 |
+
def process_messages_loop():
|
181 |
+
embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
182 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
183 |
+
knowledge_text = setup_knowledge_base()
|
184 |
+
text_chunks = chunk_text(knowledge_text, tokenizer)
|
185 |
+
embeddings = embed_model.encode(text_chunks)
|
186 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
187 |
+
index.add(embeddings)
|
188 |
+
|
189 |
+
seen_sids = set()
|
190 |
+
|
191 |
+
while True:
|
192 |
+
conversation_sid = get_latest_whatsapp_conversation_sid(twilio_client)
|
193 |
+
if not conversation_sid:
|
194 |
time.sleep(5)
|
195 |
+
continue
|
196 |
|
197 |
+
message = fetch_latest_incoming_message(twilio_client, conversation_sid)
|
198 |
+
if message and message["sid"] not in seen_sids:
|
199 |
+
seen_sids.add(message["sid"])
|
200 |
+
question = message["body"]
|
201 |
+
chunks = retrieve_chunks(question, index, embed_model, text_chunks)
|
202 |
+
answer = generate_answer_with_groq(question, "\n\n".join(chunks))
|
203 |
+
send_twilio_message(twilio_client, conversation_sid, answer)
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
+
time.sleep(5)
|
|
|
|
|
|
|
|
|
206 |
|
207 |
+
# ---------------- Streamlit UI ----------------
|
208 |
+
st.title("ToyShop WhatsApp Assistant (Groq + Twilio)")
|
|
|
209 |
|
210 |
+
if st.button("Start WhatsApp Bot"):
|
211 |
+
thread = threading.Thread(target=process_messages_loop)
|
212 |
+
thread.start()
|
213 |
+
st.success("WhatsApp assistant started and monitoring for new messages.")
|