Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,6 +54,9 @@ def calculate_price_and_timeline(req):
|
|
| 54 |
chatbot = req.get("chatbot", False)
|
| 55 |
seo = req.get("seo", False)
|
| 56 |
extra_features = req.get("extra_features", [])
|
|
|
|
|
|
|
|
|
|
| 57 |
price = BASE_PRICE
|
| 58 |
timeline = BASE_TIMELINE
|
| 59 |
if pages > BASE_PAGES:
|
|
@@ -76,6 +79,12 @@ def calculate_price_and_timeline(req):
|
|
| 76 |
info = extra_services[feat]
|
| 77 |
price += info.get("price", 0)
|
| 78 |
timeline += info.get("timeline", 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return price, timeline
|
| 80 |
|
| 81 |
def generate_project_id():
|
|
@@ -191,50 +200,15 @@ def process_step_by_step(session_id, message):
|
|
| 191 |
# Move to additional info section
|
| 192 |
session["step"] = "asking_additional"
|
| 193 |
session["additional_index"] = 0
|
| 194 |
-
# Initialize additional answers dict if not exists
|
| 195 |
if "additional_answers" not in session:
|
| 196 |
session["additional_answers"] = {}
|
| 197 |
-
# Get first question
|
| 198 |
qlist = session.get("additional_questions", [])
|
| 199 |
if qlist:
|
| 200 |
return qlist[0]["question"]
|
| 201 |
else:
|
| 202 |
-
# No additional questions, go to
|
| 203 |
-
session["step"] = "
|
| 204 |
-
|
| 205 |
-
# Build proposal with any additional answers
|
| 206 |
-
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 207 |
-
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 208 |
-
add_answers = session.get("additional_answers", {})
|
| 209 |
-
add_text = ""
|
| 210 |
-
if any(add_answers.values()):
|
| 211 |
-
add_text = "\n\n📌 **Additional Info:**\n"
|
| 212 |
-
if add_answers.get("purpose"):
|
| 213 |
-
add_text += f"• Purpose: {add_answers['purpose']}\n"
|
| 214 |
-
if add_answers.get("theme"):
|
| 215 |
-
add_text += f"• Theme: {add_answers['theme']}\n"
|
| 216 |
-
if add_answers.get("content"):
|
| 217 |
-
add_text += f"• Content: {add_answers['content']}\n"
|
| 218 |
-
if add_answers.get("inventory"):
|
| 219 |
-
add_text += f"• Inventory: {add_answers['inventory']}\n"
|
| 220 |
-
if add_answers.get("data"):
|
| 221 |
-
add_text += f"• Data: {add_answers['data']}\n"
|
| 222 |
-
return f"""📋 **Your Project Proposal**
|
| 223 |
-
|
| 224 |
-
✅ **Summary:**
|
| 225 |
-
• Domain: {'Yes' if req.get('domain') else 'No'}
|
| 226 |
-
• Pages: {req.get('pages', 5)}
|
| 227 |
-
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 228 |
-
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 229 |
-
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 230 |
-
• Extras: {extra_display}{add_text}
|
| 231 |
-
|
| 232 |
-
💰 **Total Cost:** PKR {price:,}
|
| 233 |
-
⏱️ **Timeline:** {timeline} working days
|
| 234 |
-
|
| 235 |
-
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 236 |
-
|
| 237 |
-
⚠️ *To add more extras, type 'add more'*"""
|
| 238 |
else:
|
| 239 |
matched_key = None
|
| 240 |
for key, val in extra_services.items():
|
|
@@ -254,65 +228,56 @@ def process_step_by_step(session_id, message):
|
|
| 254 |
return f"❌ Sorry, **'{message}'** is not offered. Available: {', '.join([v['name'] for v in extra_services.values()])}\n\nType a valid name or **done**."
|
| 255 |
|
| 256 |
elif step == "asking_additional":
|
| 257 |
-
# Process additional questions
|
| 258 |
idx = session.get("additional_index", 0)
|
| 259 |
qlist = session.get("additional_questions", [])
|
| 260 |
if idx < len(qlist):
|
| 261 |
-
# Save answer (if not skip/none)
|
| 262 |
current_q = qlist[idx]
|
| 263 |
key = current_q["key"]
|
| 264 |
if msg_lower not in ["skip", "none", "no"]:
|
| 265 |
session["additional_answers"][key] = message
|
| 266 |
else:
|
| 267 |
session["additional_answers"][key] = ""
|
| 268 |
-
# Move to next
|
| 269 |
session["additional_index"] = idx + 1
|
| 270 |
if idx + 1 < len(qlist):
|
| 271 |
return qlist[idx+1]["question"]
|
| 272 |
else:
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 279 |
-
add_answers = session.get("additional_answers", {})
|
| 280 |
-
add_text = ""
|
| 281 |
-
if any(add_answers.values()):
|
| 282 |
-
add_text = "\n\n📌 **Additional Info:**\n"
|
| 283 |
-
if add_answers.get("purpose"):
|
| 284 |
-
add_text += f"• Purpose: {add_answers['purpose']}\n"
|
| 285 |
-
if add_answers.get("theme"):
|
| 286 |
-
add_text += f"• Theme: {add_answers['theme']}\n"
|
| 287 |
-
if add_answers.get("content"):
|
| 288 |
-
add_text += f"• Content: {add_answers['content']}\n"
|
| 289 |
-
if add_answers.get("inventory"):
|
| 290 |
-
add_text += f"• Inventory: {add_answers['inventory']}\n"
|
| 291 |
-
if add_answers.get("data"):
|
| 292 |
-
add_text += f"• Data: {add_answers['data']}\n"
|
| 293 |
-
return f"""📋 **Your Project Proposal**
|
| 294 |
-
|
| 295 |
-
✅ **Summary:**
|
| 296 |
-
• Domain: {'Yes' if req.get('domain') else 'No'}
|
| 297 |
-
• Pages: {req.get('pages', 5)}
|
| 298 |
-
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 299 |
-
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 300 |
-
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 301 |
-
• Extras: {extra_display}{add_text}
|
| 302 |
-
|
| 303 |
-
💰 **Total Cost:** PKR {price:,}
|
| 304 |
-
⏱️ **Timeline:** {timeline} working days
|
| 305 |
-
|
| 306 |
-
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 307 |
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
| 309 |
else:
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
session["step"] = "confirm"
|
| 312 |
price, timeline = calculate_price_and_timeline(req)
|
| 313 |
extra_services = get_extra_services()
|
| 314 |
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 315 |
extra_display = ", ".join(extra_names) if extra_names else "None"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
return f"""📋 **Your Project Proposal**
|
| 317 |
|
| 318 |
✅ **Summary:**
|
|
@@ -321,12 +286,14 @@ def process_step_by_step(session_id, message):
|
|
| 321 |
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 322 |
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 323 |
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 324 |
-
• Extras: {extra_display}
|
| 325 |
|
| 326 |
💰 **Total Cost:** PKR {price:,}
|
| 327 |
⏱️ **Timeline:** {timeline} working days
|
| 328 |
|
| 329 |
-
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
|
|
|
|
|
|
| 330 |
|
| 331 |
elif step == "confirm":
|
| 332 |
if "confirm" in msg_lower or "agree" in msg_lower:
|
|
@@ -339,7 +306,7 @@ def process_step_by_step(session_id, message):
|
|
| 339 |
**Timeline:** {timeline} days
|
| 340 |
|
| 341 |
Use `status {project_id}` anytime.
|
| 342 |
-
We'll contact you
|
| 343 |
elif "cancel" in msg_lower:
|
| 344 |
del user_sessions[session_id]
|
| 345 |
return "❌ Cancelled. Start again with 'I want to build a website'."
|
|
@@ -366,12 +333,10 @@ def package_details(query):
|
|
| 366 |
|
| 367 |
# ---------- Main response handler ----------
|
| 368 |
def get_final_response(user_message, session_id):
|
| 369 |
-
# 1. Step builder
|
| 370 |
step_resp = process_step_by_step(session_id, user_message)
|
| 371 |
if step_resp:
|
| 372 |
return step_resp
|
| 373 |
|
| 374 |
-
# 2. Project status query
|
| 375 |
status_match = re.search(r'status\s+([A-Z0-9\-]+)', user_message.lower())
|
| 376 |
if status_match:
|
| 377 |
pid = status_match.group(1).upper()
|
|
@@ -385,12 +350,10 @@ def get_final_response(user_message, session_id):
|
|
| 385 |
else:
|
| 386 |
return f"❌ Project ID `{pid}` not found."
|
| 387 |
|
| 388 |
-
# 3. Package inquiry
|
| 389 |
pkg = package_details(user_message)
|
| 390 |
if pkg:
|
| 391 |
return pkg
|
| 392 |
|
| 393 |
-
# 4. Simple fallback
|
| 394 |
return "- How can I help? Services: website design, e‑commerce, SEO, chatbots.\n- **To start a project, say 'I want to build a website'**"
|
| 395 |
|
| 396 |
# ---------- Admin API routes (unchanged) ----------
|
|
@@ -470,7 +433,6 @@ def chat():
|
|
| 470 |
if not message:
|
| 471 |
return jsonify({"reply": "Please say something."})
|
| 472 |
reply = get_final_response(message, session_id)
|
| 473 |
-
# Save chat history (optional)
|
| 474 |
ref.child(f"chats/{session_id}/messages").push().set({
|
| 475 |
"role": "user", "content": message, "timestamp": datetime.now().isoformat()
|
| 476 |
})
|
|
|
|
| 54 |
chatbot = req.get("chatbot", False)
|
| 55 |
seo = req.get("seo", False)
|
| 56 |
extra_features = req.get("extra_features", [])
|
| 57 |
+
inventory = req.get("inventory", "").lower() == "yes"
|
| 58 |
+
database = req.get("data", "").lower() == "yes"
|
| 59 |
+
|
| 60 |
price = BASE_PRICE
|
| 61 |
timeline = BASE_TIMELINE
|
| 62 |
if pages > BASE_PAGES:
|
|
|
|
| 79 |
info = extra_services[feat]
|
| 80 |
price += info.get("price", 0)
|
| 81 |
timeline += info.get("timeline", 0)
|
| 82 |
+
if inventory:
|
| 83 |
+
price += 30000
|
| 84 |
+
timeline += 5
|
| 85 |
+
if database:
|
| 86 |
+
price += 40000
|
| 87 |
+
timeline += 8
|
| 88 |
return price, timeline
|
| 89 |
|
| 90 |
def generate_project_id():
|
|
|
|
| 200 |
# Move to additional info section
|
| 201 |
session["step"] = "asking_additional"
|
| 202 |
session["additional_index"] = 0
|
|
|
|
| 203 |
if "additional_answers" not in session:
|
| 204 |
session["additional_answers"] = {}
|
|
|
|
| 205 |
qlist = session.get("additional_questions", [])
|
| 206 |
if qlist:
|
| 207 |
return qlist[0]["question"]
|
| 208 |
else:
|
| 209 |
+
# No additional questions, go to contact details
|
| 210 |
+
session["step"] = "asking_contact"
|
| 211 |
+
return "📞 **Please provide your mobile number** (e.g., 03001234567) – We'll use this to contact you."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
matched_key = None
|
| 214 |
for key, val in extra_services.items():
|
|
|
|
| 228 |
return f"❌ Sorry, **'{message}'** is not offered. Available: {', '.join([v['name'] for v in extra_services.values()])}\n\nType a valid name or **done**."
|
| 229 |
|
| 230 |
elif step == "asking_additional":
|
|
|
|
| 231 |
idx = session.get("additional_index", 0)
|
| 232 |
qlist = session.get("additional_questions", [])
|
| 233 |
if idx < len(qlist):
|
|
|
|
| 234 |
current_q = qlist[idx]
|
| 235 |
key = current_q["key"]
|
| 236 |
if msg_lower not in ["skip", "none", "no"]:
|
| 237 |
session["additional_answers"][key] = message
|
| 238 |
else:
|
| 239 |
session["additional_answers"][key] = ""
|
|
|
|
| 240 |
session["additional_index"] = idx + 1
|
| 241 |
if idx + 1 < len(qlist):
|
| 242 |
return qlist[idx+1]["question"]
|
| 243 |
else:
|
| 244 |
+
session["step"] = "asking_contact"
|
| 245 |
+
return "📞 **Please provide your mobile number** (e.g., 03001234567) – We'll use this to contact you."
|
| 246 |
+
else:
|
| 247 |
+
session["step"] = "asking_contact"
|
| 248 |
+
return "📞 **Please provide your mobile number** (e.g., 03001234567) – We'll use this to contact you."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
elif step == "asking_contact":
|
| 251 |
+
if "mobile" not in session:
|
| 252 |
+
session["mobile"] = message
|
| 253 |
+
return "✉️ **Please provide your email address** (e.g., name@example.com) – We'll send the quote and updates."
|
| 254 |
else:
|
| 255 |
+
session["email"] = message
|
| 256 |
+
# Store contact details in requirements
|
| 257 |
+
req["mobile"] = session.get("mobile", "")
|
| 258 |
+
req["email"] = session.get("email", "")
|
| 259 |
+
session["requirements"] = req
|
| 260 |
+
# Now go to final proposal
|
| 261 |
session["step"] = "confirm"
|
| 262 |
price, timeline = calculate_price_and_timeline(req)
|
| 263 |
extra_services = get_extra_services()
|
| 264 |
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 265 |
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 266 |
+
add_answers = session.get("additional_answers", {})
|
| 267 |
+
add_text = ""
|
| 268 |
+
if any(add_answers.values()):
|
| 269 |
+
add_text = "\n\n📌 **Additional Info:**\n"
|
| 270 |
+
if add_answers.get("purpose"):
|
| 271 |
+
add_text += f"• Purpose: {add_answers['purpose']}\n"
|
| 272 |
+
if add_answers.get("theme"):
|
| 273 |
+
add_text += f"• Theme: {add_answers['theme']}\n"
|
| 274 |
+
if add_answers.get("content"):
|
| 275 |
+
add_text += f"• Content: {add_answers['content']}\n"
|
| 276 |
+
if add_answers.get("inventory"):
|
| 277 |
+
add_text += f"• Inventory Management: {add_answers['inventory']}\n"
|
| 278 |
+
if add_answers.get("data"):
|
| 279 |
+
add_text += f"• Database Integration: {add_answers['data']}\n"
|
| 280 |
+
contact_text = f"\n\n📞 **Contact Details:**\n• Mobile: {req.get('mobile', '')}\n• Email: {req.get('email', '')}"
|
| 281 |
return f"""📋 **Your Project Proposal**
|
| 282 |
|
| 283 |
✅ **Summary:**
|
|
|
|
| 286 |
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 287 |
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 288 |
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 289 |
+
• Extras: {extra_display}{add_text}{contact_text}
|
| 290 |
|
| 291 |
💰 **Total Cost:** PKR {price:,}
|
| 292 |
⏱️ **Timeline:** {timeline} working days
|
| 293 |
|
| 294 |
+
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 295 |
+
|
| 296 |
+
⚠️ *To add more extras, type 'add more'*"""
|
| 297 |
|
| 298 |
elif step == "confirm":
|
| 299 |
if "confirm" in msg_lower or "agree" in msg_lower:
|
|
|
|
| 306 |
**Timeline:** {timeline} days
|
| 307 |
|
| 308 |
Use `status {project_id}` anytime.
|
| 309 |
+
We'll contact you within 24 hours. 🚀"""
|
| 310 |
elif "cancel" in msg_lower:
|
| 311 |
del user_sessions[session_id]
|
| 312 |
return "❌ Cancelled. Start again with 'I want to build a website'."
|
|
|
|
| 333 |
|
| 334 |
# ---------- Main response handler ----------
|
| 335 |
def get_final_response(user_message, session_id):
|
|
|
|
| 336 |
step_resp = process_step_by_step(session_id, user_message)
|
| 337 |
if step_resp:
|
| 338 |
return step_resp
|
| 339 |
|
|
|
|
| 340 |
status_match = re.search(r'status\s+([A-Z0-9\-]+)', user_message.lower())
|
| 341 |
if status_match:
|
| 342 |
pid = status_match.group(1).upper()
|
|
|
|
| 350 |
else:
|
| 351 |
return f"❌ Project ID `{pid}` not found."
|
| 352 |
|
|
|
|
| 353 |
pkg = package_details(user_message)
|
| 354 |
if pkg:
|
| 355 |
return pkg
|
| 356 |
|
|
|
|
| 357 |
return "- How can I help? Services: website design, e‑commerce, SEO, chatbots.\n- **To start a project, say 'I want to build a website'**"
|
| 358 |
|
| 359 |
# ---------- Admin API routes (unchanged) ----------
|
|
|
|
| 433 |
if not message:
|
| 434 |
return jsonify({"reply": "Please say something."})
|
| 435 |
reply = get_final_response(message, session_id)
|
|
|
|
| 436 |
ref.child(f"chats/{session_id}/messages").push().set({
|
| 437 |
"role": "user", "content": message, "timestamp": datetime.now().isoformat()
|
| 438 |
})
|