Spaces:
Sleeping
Sleeping
Added new endpoint for the assistance chat.
Browse files
main.py
CHANGED
|
@@ -127,6 +127,156 @@ def chat():
|
|
| 127 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 128 |
return jsonify({"reply": final_response})
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 127 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 128 |
return jsonify({"reply": final_response})
|
| 129 |
|
| 130 |
+
# --- NEW: lightweight Help/Assistant intent router -------------------------
|
| 131 |
+
@app.route('/assist', methods=['POST'])
|
| 132 |
+
def assist():
|
| 133 |
+
"""
|
| 134 |
+
Classifies a short help question and, if appropriate, returns
|
| 135 |
+
a suggested navigation target + tour key for the frontend to action.
|
| 136 |
+
|
| 137 |
+
Request JSON:
|
| 138 |
+
{
|
| 139 |
+
"role": "incubatee" | ...,
|
| 140 |
+
"message": "Where do I apply?",
|
| 141 |
+
"companyCode": "ACME",
|
| 142 |
+
"userId": "abc123",
|
| 143 |
+
"path": "/current/location" # optional, helps decide replace vs push if you want
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
Response JSON:
|
| 147 |
+
{
|
| 148 |
+
"reply": "bot text to show",
|
| 149 |
+
"navigate": {
|
| 150 |
+
"to": "/incubatee/sme",
|
| 151 |
+
"tour": "apply", # optional
|
| 152 |
+
"ts": 1712345678901, # force re-run effects
|
| 153 |
+
"delayMs": 900 # UI should wait before navigating (to show the bot msg)
|
| 154 |
+
},
|
| 155 |
+
"handoff": {
|
| 156 |
+
"open": false, # whether to auto-open your drawer after nav
|
| 157 |
+
"botText": "You're on Programs. Tap an 'Apply' button to begin.",
|
| 158 |
+
"followups": ["track_application","profile_setup","inquiries"]
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
"""
|
| 162 |
+
data = request.json or {}
|
| 163 |
+
role = data.get('role')
|
| 164 |
+
user_input = (data.get('message') or '').strip().lower()
|
| 165 |
+
company_code = data.get('companyCode')
|
| 166 |
+
user_id = data.get('userId')
|
| 167 |
+
|
| 168 |
+
if not role or not user_input or not company_code or not user_id:
|
| 169 |
+
return jsonify({"error": "Missing role, message, companyCode, or userId"}), 400
|
| 170 |
+
|
| 171 |
+
# Simple keyword intent router (deterministic; fast)
|
| 172 |
+
def nav_payload(to: str, tour: Optional[str], reply: str, handoff: Optional[Dict[str, Any]] = None):
|
| 173 |
+
return jsonify({
|
| 174 |
+
"reply": reply,
|
| 175 |
+
"navigate": {
|
| 176 |
+
"to": to,
|
| 177 |
+
"tour": tour,
|
| 178 |
+
"ts": int(datetime.now().timestamp() * 1000),
|
| 179 |
+
"delayMs": 900
|
| 180 |
+
},
|
| 181 |
+
"handoff": handoff or {}
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
tokens = set(user_input.replace('?', '').split())
|
| 185 |
+
contains = lambda *words: any(w in tokens or w in user_input for w in words)
|
| 186 |
+
|
| 187 |
+
# 1) Where do I apply?
|
| 188 |
+
if contains('apply', 'application', 'register', 'program', 'submit'):
|
| 189 |
+
return nav_payload(
|
| 190 |
+
to="/incubatee/sme",
|
| 191 |
+
tour="apply",
|
| 192 |
+
reply="Taking you to Programs and starting the tour…",
|
| 193 |
+
handoff={
|
| 194 |
+
"open": False,
|
| 195 |
+
"botText": "You're on Programs. Tap an **Apply** button to begin.",
|
| 196 |
+
"followups": ["track_application", "profile_setup", "inquiries"]
|
| 197 |
+
}
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# 2) Track my application
|
| 201 |
+
if contains('track', 'status', 'progress'):
|
| 202 |
+
return nav_payload(
|
| 203 |
+
to="/incubatee/tracker",
|
| 204 |
+
tour="track",
|
| 205 |
+
reply="Opening your application tracker…",
|
| 206 |
+
handoff={
|
| 207 |
+
"open": False,
|
| 208 |
+
"botText": "This is your tracker. You can view statuses and details here.",
|
| 209 |
+
"followups": ["where_apply", "inquiries"]
|
| 210 |
+
}
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# 3) Inquiries / helpdesk
|
| 214 |
+
if contains('inquiry', 'inquiries', 'question', 'helpdesk', 'support', 'ticket'):
|
| 215 |
+
return nav_payload(
|
| 216 |
+
to="/incubatee/sme/inquiries",
|
| 217 |
+
tour="inquiries",
|
| 218 |
+
reply="Taking you to Inquiries…",
|
| 219 |
+
handoff={
|
| 220 |
+
"open": True,
|
| 221 |
+
"botText": "Here you can log questions or check replies.",
|
| 222 |
+
"followups": ["where_apply", "track_application"]
|
| 223 |
+
}
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# 4) Profile/setup questions
|
| 227 |
+
if contains('profile', 'setup', 'account', 'redirect'):
|
| 228 |
+
return jsonify({
|
| 229 |
+
"reply": (
|
| 230 |
+
"You need to complete your profile before applying. "
|
| 231 |
+
"If something’s missing (e.g., name/company), we’ll redirect you to finish it first."
|
| 232 |
+
),
|
| 233 |
+
"handoff": {
|
| 234 |
+
"open": True,
|
| 235 |
+
"botText": "Would you like to go to **My Profile** now?",
|
| 236 |
+
"followups": ["where_apply"]
|
| 237 |
+
}
|
| 238 |
+
})
|
| 239 |
+
|
| 240 |
+
# 5) Contact support
|
| 241 |
+
if contains('contact', 'email', 'help', 'assist'):
|
| 242 |
+
return jsonify({
|
| 243 |
+
"reply": "You can email support at **support@smartincubation.example** or log an inquiry from the Inquiries page.",
|
| 244 |
+
"handoff": {
|
| 245 |
+
"open": True,
|
| 246 |
+
"botText": "Try the **Inquiries** option if you want to log a ticket.",
|
| 247 |
+
"followups": ["inquiries"]
|
| 248 |
+
}
|
| 249 |
+
})
|
| 250 |
+
|
| 251 |
+
# 6) Fallback → optionally leverage GPT to draft a helpful answer (no redirect)
|
| 252 |
+
# Keep this light; we're not doing Firestore fetches here.
|
| 253 |
+
try:
|
| 254 |
+
sys = {
|
| 255 |
+
"role": "system",
|
| 256 |
+
"content": (
|
| 257 |
+
"You are Q-Bot, a concise, friendly assistant. "
|
| 258 |
+
"Clarify the request in one or two sentences and suggest a next step. "
|
| 259 |
+
"Do NOT mention databases or internal logic."
|
| 260 |
+
)
|
| 261 |
+
}
|
| 262 |
+
user = {"role": "user", "content": data.get('message', '')}
|
| 263 |
+
answer = ask_gpt([sys, user]) or "I’m here to help! Could you tell me a bit more about what you need?"
|
| 264 |
+
return jsonify({
|
| 265 |
+
"reply": answer,
|
| 266 |
+
"handoff": {
|
| 267 |
+
"open": True,
|
| 268 |
+
"followups": ["where_apply", "track_application", "inquiries"]
|
| 269 |
+
}
|
| 270 |
+
})
|
| 271 |
+
except Exception:
|
| 272 |
+
return jsonify({
|
| 273 |
+
"reply": "I’m here to help! Could you tell me a bit more about what you need?",
|
| 274 |
+
"handoff": {
|
| 275 |
+
"open": True,
|
| 276 |
+
"followups": ["where_apply", "track_application", "inquiries"]
|
| 277 |
+
}
|
| 278 |
+
})
|
| 279 |
+
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
app.run(host="0.0.0.0", port=7860)
|