Ahmad140 commited on
Commit
9fb4113
Β·
verified Β·
1 Parent(s): 23d3910

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -67
app.py CHANGED
@@ -24,18 +24,17 @@ firebase_admin.initialize_app(cred, {'databaseURL': database_url})
24
  ref = db.reference('/')
25
  print("βœ… Firebase connected")
26
 
27
- # ---------- Sentence-Transformer Model for Intent ----------
28
  print("πŸ”„ Loading sentence-transformers...")
29
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
30
  print("βœ… Embedding model loaded")
31
 
32
- # ---------- Local GPT-2 Model (Fallback) ----------
33
- print("πŸ”„ Loading local GPT-2 model (fallback)...")
34
  try:
35
  gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
36
  gpt2_model = AutoModelForCausalLM.from_pretrained("gpt2")
37
  gpt2_tokenizer.pad_token = gpt2_tokenizer.eos_token
38
- # Move to GPU if available
39
  device = "cuda" if torch.cuda.is_available() else "cpu"
40
  gpt2_model = gpt2_model.to(device)
41
  print(f"βœ… GPT-2 loaded on {device}")
@@ -44,80 +43,107 @@ except Exception as e:
44
  gpt2_model = None
45
  gpt2_tokenizer = None
46
 
47
- # ---------- Service List (used only for context) ----------
48
- SERVICES_DESC = {
49
- "product photography": "High-end product photography for jewelry, cars, luxury goods.",
50
- "architecture renders": "3D architectural visualization and rendering.",
51
- "corporate videos": "4K/8K corporate video production.",
52
- "drone cinematography": "Drone videography for events, real estate, commercials.",
53
- "3d rendering": "3D rendering for products and architecture.",
54
- "wedding photography": "Wedding photography & cinematography packages.",
55
- "real estate visuals": "Real estate photo, virtual tours, drone shoots.",
56
- "ai editing": "AI-enhanced photo editing and retouching.",
57
- "headshots": "Professional corporate headshots and portraits."
58
- }
59
-
60
- SERVICES_LIST = list(SERVICES_DESC.keys())
61
-
62
- # ---------- Intent Classification ----------
63
- IRRELEVANT_KEYWORDS = ["cricket", "weather", "politics", "imran", "nawaz", "election", "stock", "bitcoin", "covid", "corona"]
64
-
65
- def classify_intent(user_text):
66
- text_lower = user_text.lower()
67
- if any(kw in text_lower for kw in IRRELEVANT_KEYWORDS):
68
- return "irrelevant"
69
- if any(x in text_lower for x in ["contact", "phone", "number", "whatsapp", "email", "call", "raabta"]):
70
- return "contact"
71
- if any(x in text_lower for x in ["price", "cost", "rate", "kitne", "quote", "charges", "fee"]):
72
- return "pricing"
73
- if any(x in text_lower for x in ["portfolio", "sample", "gallery", "kaam dikhao"]):
74
- return "portfolio"
75
- if any(x in text_lower for x in ["salam", "hello", "hi", "hey"]):
76
- return "greeting"
77
- return "service_inquiry"
78
-
79
- def handle_negation(text):
80
- neg_patterns = [r"can(not|'t) you", r"do you not", r"don't you", r"nhi karte", r"nahi karte"]
81
- return any(re.search(pat, text.lower()) for pat in neg_patterns)
82
-
83
- # ---------- Local GPT-2 Generation (Personalized, No Copy-Paste) ----------
84
- def generate_with_gpt2(prompt):
85
- """Generate a response using local GPT-2 model."""
86
  if gpt2_model is None or gpt2_tokenizer is None:
87
- return "Local AI model unavailable. Please try again later."
88
 
89
- # Build a structured prompt for GPT-2 to produce relevant answers
90
- full_prompt = f"""You are VisionCraft AI, assistant for Elite Visuals, a premium imagery business. Services include: {', '.join(SERVICES_LIST)}.
91
- Answer the user's question in short bullet points. Be helpful and accurate.
92
 
93
- User: {prompt}
 
 
94
  Assistant:"""
95
 
96
- inputs = gpt2_tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
97
  device = next(gpt2_model.parameters()).device
98
  inputs = {k: v.to(device) for k, v in inputs.items()}
99
 
100
  with torch.no_grad():
101
  outputs = gpt2_model.generate(
102
  **inputs,
103
- max_new_tokens=150,
104
- temperature=0.8,
105
  do_sample=True,
106
  top_p=0.9,
 
 
107
  pad_token_id=gpt2_tokenizer.eos_token_id
108
  )
109
  response = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
 
110
  # Extract only the assistant part
111
  if "Assistant:" in response:
112
  response = response.split("Assistant:")[-1].strip()
113
- # Remove the original prompt if it appears
114
- if response.startswith(full_prompt):
115
- response = response[len(full_prompt):].strip()
116
- # If response is too short or empty, provide a default
117
- if len(response) < 10:
118
- response = "- I understand you're asking about Elite Visuals services. Could you please be more specific? We offer product photography, drone videos, 3D rendering, wedding shoots, and more."
 
 
 
 
 
 
 
 
119
  return response
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  # ---------- OpenRouter Setup ----------
122
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
123
  MODEL = "google/gemini-2.0-flash-lite-preview-02-05"
@@ -127,8 +153,8 @@ SYSTEM_PROMPT = """You are "VisionCraft AI" – assistant for "Elite Visuals", a
127
  Services: product photography, architecture renders, corporate videos, drone cinematography, 3D rendering, wedding photography, real estate visuals, AI editing.
128
 
129
  Always answer in bullet points. Be concise, helpful, and personalized to the user's exact question.
130
- If user asks "can you not provide X", clarify that you DO provide it unless X is not in the list.
131
- Never copy-paste the same generic list for every question. Adapt to the query."""
132
 
133
  def call_openrouter(prompt):
134
  if not OPENROUTER_API_KEY:
@@ -142,7 +168,7 @@ def call_openrouter(prompt):
142
  {"role": "user", "content": prompt}
143
  ],
144
  "temperature": 0.7,
145
- "max_tokens": 500
146
  }
147
  try:
148
  resp = requests.post(url, json=payload, headers=headers, timeout=15)
@@ -153,15 +179,21 @@ def call_openrouter(prompt):
153
  return None
154
 
155
  def get_ai_response(user_message, intent):
156
- # Priority 1: OpenRouter (smart, personalized)
157
- ai_response = call_openrouter(user_message)
158
- if ai_response and ai_response.strip() and "error" not in ai_response.lower():
159
  print("βœ… OpenRouter succeeded")
160
- return ai_response
 
 
 
 
 
 
161
 
162
- # Priority 2: Local GPT-2 (still personalized, not copy-paste)
163
- print("⚠️ OpenRouter failed – using local GPT-2")
164
- return generate_with_gpt2(user_message)
165
 
166
  # ---------- Firebase Helpers ----------
167
  def save_message(session_id, role, content, intent=None):
 
24
  ref = db.reference('/')
25
  print("βœ… Firebase connected")
26
 
27
+ # ---------- Sentence-Transformer Model ----------
28
  print("πŸ”„ Loading sentence-transformers...")
29
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
30
  print("βœ… Embedding model loaded")
31
 
32
+ # ---------- Local GPT-2 Model (Fallback) with Fixes ----------
33
+ print("πŸ”„ Loading local GPT-2 model...")
34
  try:
35
  gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
36
  gpt2_model = AutoModelForCausalLM.from_pretrained("gpt2")
37
  gpt2_tokenizer.pad_token = gpt2_tokenizer.eos_token
 
38
  device = "cuda" if torch.cuda.is_available() else "cpu"
39
  gpt2_model = gpt2_model.to(device)
40
  print(f"βœ… GPT-2 loaded on {device}")
 
43
  gpt2_model = None
44
  gpt2_tokenizer = None
45
 
46
+ def generate_with_gpt2(user_query):
47
+ """Generate response using GPT-2 with repetition penalties and structured prompt."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  if gpt2_model is None or gpt2_tokenizer is None:
49
+ return None
50
 
51
+ # Clean prompt – tell GPT-2 it's an assistant for Elite Visuals
52
+ prompt = f"""You are a helpful assistant for Elite Visuals, a premium imagery business.
53
+ Services: product photography, architecture renders, corporate videos, drone cinematography, wedding photography, real estate visuals, AI editing.
54
 
55
+ Answer the user's question in 2-3 short bullet points. Be direct and helpful. Do not repeat yourself.
56
+
57
+ User: {user_query}
58
  Assistant:"""
59
 
60
+ inputs = gpt2_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
61
  device = next(gpt2_model.parameters()).device
62
  inputs = {k: v.to(device) for k, v in inputs.items()}
63
 
64
  with torch.no_grad():
65
  outputs = gpt2_model.generate(
66
  **inputs,
67
+ max_new_tokens=100,
68
+ temperature=0.7,
69
  do_sample=True,
70
  top_p=0.9,
71
+ repetition_penalty=1.5, # Penalty for repetition
72
+ no_repeat_ngram_size=3, # No repeating 3-grams
73
  pad_token_id=gpt2_tokenizer.eos_token_id
74
  )
75
  response = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
76
+
77
  # Extract only the assistant part
78
  if "Assistant:" in response:
79
  response = response.split("Assistant:")[-1].strip()
80
+ else:
81
+ response = response[len(prompt):].strip()
82
+
83
+ # Remove any trailing repetition or incomplete sentences
84
+ lines = response.split('\n')
85
+ unique_lines = []
86
+ for line in lines:
87
+ if line.strip() and line not in unique_lines:
88
+ unique_lines.append(line.strip())
89
+ response = '\n'.join(unique_lines[:4]) # max 4 lines
90
+
91
+ # If response is empty or too short, use fallback
92
+ if len(response) < 10 or response.count('-') == 0:
93
+ return None
94
  return response
95
 
96
+ # ---------- Intent-Based Smart Fallback (No GPT-2 nonsense) ----------
97
+ SERVICES_LIST = ["product photography", "architecture renders", "corporate videos", "drone cinematography", "wedding photography", "real estate visuals", "AI editing", "headshots"]
98
+
99
+ def intent_based_reply(user_message, intent):
100
+ """Safe fallback when GPT-2 fails."""
101
+ msg_lower = user_message.lower()
102
+
103
+ # Contact
104
+ if "contact" in msg_lower or "phone" in msg_lower or "number" in msg_lower or "email" in msg_lower:
105
+ return "- πŸ“ž Phone: +92 300 1234567\n- βœ‰οΈ Email: hello@elitevisuals.com\n- 🌐 Website: www.elitevisuals.com\n- ⏰ Timings: Mon-Sat, 10am-7pm"
106
+
107
+ # Drone specific
108
+ if "drone" in msg_lower or "videography" in msg_lower:
109
+ return "- βœ… Yes, we offer drone cinematography for events, real estate, and commercials.\n- πŸ’° Pricing starts at PKR 50,000 per hour.\n- πŸŽ₯ Sample video available on request."
110
+
111
+ # Wedding
112
+ if "wedding" in msg_lower:
113
+ return "- βœ… Yes, we provide wedding photography and cinematography.\n- πŸ’° Packages from PKR 50,000 to PKR 1,50,000.\n- πŸ“Έ Portfolio available on Instagram @elitevisuals."
114
+
115
+ # Pricing
116
+ if any(x in msg_lower for x in ["price", "cost", "rate", "kitne"]):
117
+ return "- πŸ“Έ Product photography: PKR 15,000–25,000/session\n- πŸ›οΈ Architecture renders: PKR 30,000–50,000/project\n- πŸŽ₯ Corporate video: PKR 50,000–2,00,000\n- 🎬 Wedding photography: PKR 50,000–1,50,000\n- ✈️ Drone services: PKR 25,000–60,000/hour"
118
+
119
+ # Greeting
120
+ if any(x in msg_lower for x in ["salam", "hello", "hi"]):
121
+ return "- Wa-Alaikum-Assalam! πŸ‘‹\n- Main Elite Visuals ka assistant hoon.\n- Main in services mein madad kar sakta hoon: " + ", ".join(SERVICES_LIST[:5]) + "."
122
+
123
+ # Out of scope (weather, politics, etc.)
124
+ if any(x in msg_lower for x in ["weather", "cricket", "politics", "imran", "nawaz", "stock", "bitcoin"]):
125
+ return "- ❌ Maazrat, yeh sawaal mere scope mein nahi hai.\n- Main sirf Elite Visuals ki imagery services ke baare mein jaankaari de sakta hoon.\n- Kya aap in mein se kisi ke baare mein poochna chahenge?"
126
+
127
+ # General service inquiry
128
+ return f"- Main Elite Visuals ki in services mein madad kar sakta hoon:\n β€’ " + "\n β€’ ".join(SERVICES_LIST[:6]) + "\n- Kya aap kisi specific ke baare mein jaanna chahenge?"
129
+
130
+ # ---------- Intent Classification ----------
131
+ IRRELEVANT_KEYWORDS = ["weather", "cricket", "politics", "imran", "nawaz", "election", "stock", "bitcoin", "covid", "corona"]
132
+
133
+ def classify_intent(user_text):
134
+ text_lower = user_text.lower()
135
+ if any(kw in text_lower for kw in IRRELEVANT_KEYWORDS):
136
+ return "irrelevant"
137
+ if any(x in text_lower for x in ["contact", "phone", "number", "whatsapp", "email", "call", "raabta"]):
138
+ return "contact"
139
+ if any(x in text_lower for x in ["price", "cost", "rate", "kitne", "quote", "charges", "fee"]):
140
+ return "pricing"
141
+ if any(x in text_lower for x in ["portfolio", "sample", "gallery", "kaam dikhao"]):
142
+ return "portfolio"
143
+ if any(x in text_lower for x in ["salam", "hello", "hi", "hey"]):
144
+ return "greeting"
145
+ return "service_inquiry"
146
+
147
  # ---------- OpenRouter Setup ----------
148
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
149
  MODEL = "google/gemini-2.0-flash-lite-preview-02-05"
 
153
  Services: product photography, architecture renders, corporate videos, drone cinematography, 3D rendering, wedding photography, real estate visuals, AI editing.
154
 
155
  Always answer in bullet points. Be concise, helpful, and personalized to the user's exact question.
156
+ If user asks "can you not provide X", clarify that you DO provide it.
157
+ Never repeat the same sentence. Keep answers to 2-4 bullet points."""
158
 
159
  def call_openrouter(prompt):
160
  if not OPENROUTER_API_KEY:
 
168
  {"role": "user", "content": prompt}
169
  ],
170
  "temperature": 0.7,
171
+ "max_tokens": 300
172
  }
173
  try:
174
  resp = requests.post(url, json=payload, headers=headers, timeout=15)
 
179
  return None
180
 
181
  def get_ai_response(user_message, intent):
182
+ # Priority 1: OpenRouter
183
+ resp = call_openrouter(user_message)
184
+ if resp and resp.strip() and len(resp) > 10:
185
  print("βœ… OpenRouter succeeded")
186
+ return resp
187
+
188
+ # Priority 2: Local GPT-2 (now fixed)
189
+ gpt2_resp = generate_with_gpt2(user_message)
190
+ if gpt2_resp and len(gpt2_resp) > 10 and "view your clients" not in gpt2_resp.lower():
191
+ print("βœ… GPT-2 succeeded")
192
+ return gpt2_resp
193
 
194
+ # Priority 3: Intent-based fallback (safe, no nonsense)
195
+ print("⚠️ Using intent-based fallback")
196
+ return intent_based_reply(user_message, intent)
197
 
198
  # ---------- Firebase Helpers ----------
199
  def save_message(session_id, role, content, intent=None):