Ahmad140 commited on
Commit
6abc468
·
verified ·
1 Parent(s): 7bc0028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -162,20 +162,7 @@ def reject_update(project_id):
162
  })
163
  return True
164
 
165
- # ---------- Rule‑Based Fallback (instead of GPT‑2) ----------
166
- def simple_response(user_message):
167
- msg = user_message.lower()
168
- if any(word in msg for word in ["hello", "hi", "salam"]):
169
- return "- Wa-Alaikum-Assalam! 👋\n- I can help with website design, e-commerce, SEO, and chatbots.\n- To start a new project, say **'I want to build a website'**."
170
- if "package" in msg or "price" in msg or "cost" in msg:
171
- return "**Our Packages:**\n- Basic: PKR 50,000 (7-10 days)\n- Standard: PKR 1,20,000 (15-20 days)\n- Premium: PKR 2,50,000 (25-30 days)\n- For exact quote, start a project."
172
- if "contact" in msg:
173
- return "- 📞 Phone: +92 300 1234567\n- ✉️ Email: hello@elitevisuals.com\n- 🌐 Website: www.elitevisuals.com"
174
- if "help" in msg:
175
- return "- Say **'I want to build a website'** to start a step‑by‑step project.\n- Type **'status PROJ-XXXX'** to check your project.\n- Use the quick buttons below."
176
- return "- I'm here to help with websites, e-commerce, SEO, and chatbots.\n- Say **'I want to build a website'** to get started."
177
-
178
- # ---------- OpenRouter (Optional) ----------
179
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
180
  OPENROUTER_MODEL = "google/gemini-2.0-flash-lite-preview-02-05"
181
 
@@ -198,7 +185,20 @@ def call_openrouter(prompt):
198
  print(f"OpenRouter error: {e}")
199
  return None
200
 
201
- # ---------- Step Builder (Structured Project Creation) ----------
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  user_sessions = {}
203
 
204
  def is_start_project_request(text):
@@ -208,20 +208,27 @@ def is_start_project_request(text):
208
 
209
  def process_step_by_step(session_id, message):
210
  msg_lower = message.lower().strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  if session_id not in user_sessions:
212
- if is_start_project_request(message):
213
- user_sessions[session_id] = {
214
- "step": "asking_domain",
215
- "requirements": {},
216
- "deep_answers": {},
217
- "deep_index": 0,
218
- "deep_questions": [
219
- "🤔 **Tell me more about your target audience or business goals.** (This helps us design the perfect website.)",
220
- "⭐ **Are there any specific must‑have features or integrations you need?** (e.g., payment gateway, social login, analytics)",
221
- "💰 **What is your budget range or any other requirements?** (We’ll try to match it as closely as possible.)"
222
- ]
223
- }
224
- return "🌐 **Do you need a domain name?** (e.g., www.yourbusiness.com)\n\nReply: **Yes** or **No**"
225
  return None
226
 
227
  session = user_sessions[session_id]
@@ -238,12 +245,14 @@ def process_step_by_step(session_id, message):
238
  session["step"] = "asking_contact"
239
  return "📞 **Please provide your mobile number** (e.g., 03001234567) – We'll use this to contact you."
240
 
 
241
  if msg_lower in ["add more", "add something", "extra add"]:
242
  session["step"] = "asking_extra"
243
  extra_services = get_extra_services()
244
  extra_list = "\n".join([f"{v['name']}: +PKR {v['price']:,}, +{v['timeline']} days" for v in extra_services.values()])
245
  return f"✨ **Add extra features**\n\nAvailable:\n{extra_list}\n\nType the name or **done**."
246
 
 
247
  if step == "asking_domain":
248
  req["domain"] = "yes" in msg_lower or "han" in msg_lower
249
  session["step"] = "asking_pages"
@@ -389,7 +398,7 @@ We'll contact you within 24 hours. 🚀"""
389
 
390
  # ---------- Main Response Handler ----------
391
  def get_final_response(user_message, session_id):
392
- # 1. Project status query
393
  status_match = re.search(r'status\s+([A-Z0-9\-]+)', user_message.lower())
394
  if status_match:
395
  pid = status_match.group(1).upper()
@@ -430,12 +439,21 @@ def get_final_response(user_message, session_id):
430
  else:
431
  return f"❌ Cannot request update for `{pid}`. Either it doesn't exist or the 3‑day window has passed."
432
 
433
- # 3. Step builder
434
  step_resp = process_step_by_step(session_id, user_message)
435
  if step_resp:
436
  return step_resp
437
 
438
- # 4. AI or fallback
 
 
 
 
 
 
 
 
 
439
  ai_resp = call_openrouter(user_message)
440
  if ai_resp:
441
  return ai_resp
 
162
  })
163
  return True
164
 
165
+ # ---------- OpenRouter (AI) ----------
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
167
  OPENROUTER_MODEL = "google/gemini-2.0-flash-lite-preview-02-05"
168
 
 
185
  print(f"OpenRouter error: {e}")
186
  return None
187
 
188
+ # ---------- Rule‑Based Fallback (when AI fails) ----------
189
+ def simple_response(user_message):
190
+ msg = user_message.lower()
191
+ if any(word in msg for word in ["hello", "hi", "salam"]):
192
+ return "- Wa-Alaikum-Assalam! 👋\n- I can help with website design, e-commerce, SEO, and chatbots.\n- To start a new project, say **'I want to build a website'**."
193
+ if "package" in msg or "price" in msg or "cost" in msg:
194
+ return "**Our Packages:**\n- Basic: PKR 50,000 (7-10 days)\n- Standard: PKR 1,20,000 (15-20 days)\n- Premium: PKR 2,50,000 (25-30 days)\n- For exact quote, start a project."
195
+ if "contact" in msg:
196
+ return "- 📞 Phone: +92 300 1234567\n- ✉️ Email: hello@elitevisuals.com\n- 🌐 Website: www.elitevisuals.com"
197
+ if "help" in msg:
198
+ return "- Say **'I want to build a website'** to start a step‑by‑step project.\n- Type **'status PROJ-XXXX'** to check your project.\n- Use the quick buttons below."
199
+ return "- I'm here to help with websites, e-commerce, SEO, and chatbots.\n- Say **'I want to build a website'** to get started."
200
+
201
+ # ---------- Step Builder (with reset on start command) ----------
202
  user_sessions = {}
203
 
204
  def is_start_project_request(text):
 
208
 
209
  def process_step_by_step(session_id, message):
210
  msg_lower = message.lower().strip()
211
+
212
+ # If user says start command, reset any existing session and start fresh
213
+ if is_start_project_request(message):
214
+ # Clear any existing session
215
+ if session_id in user_sessions:
216
+ del user_sessions[session_id]
217
+ user_sessions[session_id] = {
218
+ "step": "asking_domain",
219
+ "requirements": {},
220
+ "deep_answers": {},
221
+ "deep_index": 0,
222
+ "deep_questions": [
223
+ "🤔 **Tell me more about your target audience or business goals.** (This helps us design the perfect website.)",
224
+ "⭐ **Are there any specific must‑have features or integrations you need?** (e.g., payment gateway, social login, analytics)",
225
+ "💰 **What is your budget range or any other requirements?** (We’ll try to match it as closely as possible.)"
226
+ ]
227
+ }
228
+ return "🌐 **Do you need a domain name?** (e.g., www.yourbusiness.com)\n\nReply: **Yes** or **No**"
229
+
230
+ # If no active session, ignore step builder (let AI handle)
231
  if session_id not in user_sessions:
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  return None
233
 
234
  session = user_sessions[session_id]
 
245
  session["step"] = "asking_contact"
246
  return "📞 **Please provide your mobile number** (e.g., 03001234567) – We'll use this to contact you."
247
 
248
+ # Handle "add more" command
249
  if msg_lower in ["add more", "add something", "extra add"]:
250
  session["step"] = "asking_extra"
251
  extra_services = get_extra_services()
252
  extra_list = "\n".join([f"{v['name']}: +PKR {v['price']:,}, +{v['timeline']} days" for v in extra_services.values()])
253
  return f"✨ **Add extra features**\n\nAvailable:\n{extra_list}\n\nType the name or **done**."
254
 
255
+ # Step handlers
256
  if step == "asking_domain":
257
  req["domain"] = "yes" in msg_lower or "han" in msg_lower
258
  session["step"] = "asking_pages"
 
398
 
399
  # ---------- Main Response Handler ----------
400
  def get_final_response(user_message, session_id):
401
+ # 1. Check if it's a project status query
402
  status_match = re.search(r'status\s+([A-Z0-9\-]+)', user_message.lower())
403
  if status_match:
404
  pid = status_match.group(1).upper()
 
439
  else:
440
  return f"❌ Cannot request update for `{pid}`. Either it doesn't exist or the 3‑day window has passed."
441
 
442
+ # 3. Step builder (only starts when user says start command, else ignored)
443
  step_resp = process_step_by_step(session_id, user_message)
444
  if step_resp:
445
  return step_resp
446
 
447
+ # 4. Special commands (packages, contact, help) – handled by AI or rule‑based
448
+ lower = user_message.lower()
449
+ if "show packages" in lower:
450
+ return "**Our Packages:**\n- Basic: PKR 50,000 (7-10 days)\n- Standard: PKR 1,20,000 (15-20 days)\n- Premium: PKR 2,50,000 (25-30 days)\n- For exact quote, start a project."
451
+ if "contact number" in lower or "contact" in lower:
452
+ return "- 📞 Phone: +92 300 1234567\n- ✉️ Email: hello@elitevisuals.com\n- 🌐 Website: www.elitevisuals.com"
453
+ if "help" in lower:
454
+ return "- Say **'I want to build a website'** to start a step‑by‑step project.\n- Type **'status PROJ-XXXX'** to check your project.\n- Use the quick buttons below."
455
+
456
+ # 5. AI or fallback
457
  ai_resp = call_openrouter(user_message)
458
  if ai_resp:
459
  return ai_resp