Spaces:
Sleeping
Sleeping
IZERE HIRWA Roger
commited on
Commit
·
d4d8805
1
Parent(s):
9d7601a
- .env +1 -1
- app.py +1 -1
- chatbot/app.js +25 -4
- run_aimhsa.py +50 -0
.env
CHANGED
|
@@ -30,7 +30,7 @@ HDEV_SMS_API_ID=HDEV-23fb1b59-aec0-4aef-a351-bfc1c3aa3c52-ID
|
|
| 30 |
HDEV_SMS_API_KEY=HDEV-6e36c286-19bb-4b45-838e-8b5cd0240857-KEY
|
| 31 |
|
| 32 |
# Chat Model Configuration
|
| 33 |
-
CHAT_MODEL=meta-llama/llama-
|
| 34 |
EMBED_MODEL=nomic-embed-text
|
| 35 |
SENT_EMBED_MODEL=nomic-embed-text
|
| 36 |
OLLAMA_BASE_URL=https://openrouter.ai/api/v1
|
|
|
|
| 30 |
HDEV_SMS_API_KEY=HDEV-6e36c286-19bb-4b45-838e-8b5cd0240857-KEY
|
| 31 |
|
| 32 |
# Chat Model Configuration
|
| 33 |
+
CHAT_MODEL=meta-llama/llama-4-maverick
|
| 34 |
EMBED_MODEL=nomic-embed-text
|
| 35 |
SENT_EMBED_MODEL=nomic-embed-text
|
| 36 |
OLLAMA_BASE_URL=https://openrouter.ai/api/v1
|
app.py
CHANGED
|
@@ -531,7 +531,7 @@ class RiskDetector:
|
|
| 531 |
def __init__(self):
|
| 532 |
# Risk indicators patterns
|
| 533 |
self.critical_indicators = [
|
| 534 |
-
r'\b(suicide|kill myself|end it all'
|
| 535 |
]
|
| 536 |
self.high_risk_indicators = [
|
| 537 |
r'\b(hopeless|worthless|burden|better off without)\b',
|
|
|
|
| 531 |
def __init__(self):
|
| 532 |
# Risk indicators patterns
|
| 533 |
self.critical_indicators = [
|
| 534 |
+
r'\b(suicide|kill myself|end it all'
|
| 535 |
]
|
| 536 |
self.high_risk_indicators = [
|
| 537 |
r'\b(hopeless|worthless|burden|better off without)\b',
|
chatbot/app.js
CHANGED
|
@@ -193,7 +193,12 @@
|
|
| 193 |
await updateHistoryList();
|
| 194 |
} catch (err) {
|
| 195 |
console.error("session error", err);
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
|
@@ -256,15 +261,21 @@
|
|
| 256 |
if (account) payload.account = account;
|
| 257 |
const model = getSelectedModel();
|
| 258 |
if (model) payload.model = model;
|
|
|
|
| 259 |
const resp = await api("/ask", {
|
| 260 |
method: "POST",
|
| 261 |
headers: { "Content-Type": "application/json" },
|
| 262 |
body: JSON.stringify(payload),
|
| 263 |
});
|
|
|
|
| 264 |
removeTypingIndicator();
|
| 265 |
-
appendMessage("assistant", resp.answer || "(no answer)");
|
| 266 |
|
| 267 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
// Risk assessment is handled in backend only (no display)
|
| 270 |
// But show booking confirmation to user
|
|
@@ -286,7 +297,17 @@
|
|
| 286 |
} catch (err) {
|
| 287 |
console.error("ask error", err);
|
| 288 |
removeTypingIndicator();
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
} finally {
|
| 291 |
disableComposer(false);
|
| 292 |
}
|
|
|
|
| 193 |
await updateHistoryList();
|
| 194 |
} catch (err) {
|
| 195 |
console.error("session error", err);
|
| 196 |
+
// Fallback: create a client-side conversation ID if server session fails
|
| 197 |
+
if (!convId) {
|
| 198 |
+
convId = newConvId();
|
| 199 |
+
localStorage.setItem("aimhsa_conv", convId);
|
| 200 |
+
}
|
| 201 |
+
appendMessage("bot", "Session initialized. How can I help you today?");
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
|
|
|
| 261 |
if (account) payload.account = account;
|
| 262 |
const model = getSelectedModel();
|
| 263 |
if (model) payload.model = model;
|
| 264 |
+
|
| 265 |
const resp = await api("/ask", {
|
| 266 |
method: "POST",
|
| 267 |
headers: { "Content-Type": "application/json" },
|
| 268 |
body: JSON.stringify(payload),
|
| 269 |
});
|
| 270 |
+
|
| 271 |
removeTypingIndicator();
|
|
|
|
| 272 |
|
| 273 |
+
// Ensure we got a valid response
|
| 274 |
+
if (!resp.answer || resp.answer.trim() === '') {
|
| 275 |
+
appendMessage("assistant", "I'm here to help. Could you please rephrase your question?");
|
| 276 |
+
} else {
|
| 277 |
+
appendMessage("assistant", resp.answer);
|
| 278 |
+
}
|
| 279 |
|
| 280 |
// Risk assessment is handled in backend only (no display)
|
| 281 |
// But show booking confirmation to user
|
|
|
|
| 297 |
} catch (err) {
|
| 298 |
console.error("ask error", err);
|
| 299 |
removeTypingIndicator();
|
| 300 |
+
|
| 301 |
+
// Provide helpful error message based on error type
|
| 302 |
+
let errorMessage = "I'm having trouble connecting to the server. Please check your internet connection and try again.";
|
| 303 |
+
|
| 304 |
+
if (err.message && err.message.includes('405')) {
|
| 305 |
+
errorMessage = "There's a server configuration issue. Please try refreshing the page or contact support.";
|
| 306 |
+
} else if (err.message && err.message.includes('500')) {
|
| 307 |
+
errorMessage = "The server encountered an error. Please try again in a moment.";
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
appendMessage("bot", errorMessage);
|
| 311 |
} finally {
|
| 312 |
disableComposer(false);
|
| 313 |
}
|
run_aimhsa.py
CHANGED
|
@@ -404,6 +404,56 @@ def api_reset():
|
|
| 404 |
|
| 405 |
return jsonify({"ok": True})
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
# ============================================================================
|
| 408 |
# MAIN FUNCTION
|
| 409 |
# ============================================================================
|
|
|
|
| 404 |
|
| 405 |
return jsonify({"ok": True})
|
| 406 |
|
| 407 |
+
@app.route('/session', methods=['POST'])
|
| 408 |
+
def api_session():
|
| 409 |
+
"""
|
| 410 |
+
Create or retrieve session by IP or account.
|
| 411 |
+
Request JSON: { "account": "<optional account id>" }
|
| 412 |
+
Returns: { "id": "<conv_id>", "new": true|false }
|
| 413 |
+
"""
|
| 414 |
+
try:
|
| 415 |
+
data = request.get_json(silent=True) or {}
|
| 416 |
+
except Exception:
|
| 417 |
+
data = {}
|
| 418 |
+
|
| 419 |
+
account = (data.get("account") or "").strip()
|
| 420 |
+
if account:
|
| 421 |
+
key = f"acct:{account}"
|
| 422 |
+
else:
|
| 423 |
+
ip = request.remote_addr or "unknown"
|
| 424 |
+
key = f"ip:{ip}"
|
| 425 |
+
|
| 426 |
+
# Simple session creation logic
|
| 427 |
+
conv_id = str(uuid.uuid4())
|
| 428 |
+
|
| 429 |
+
# Save session to database
|
| 430 |
+
conn = sqlite3.connect(DB_FILE)
|
| 431 |
+
try:
|
| 432 |
+
# Check if session exists
|
| 433 |
+
cur = conn.execute("SELECT conv_id FROM sessions WHERE key = ?", (key,))
|
| 434 |
+
row = cur.fetchone()
|
| 435 |
+
if row:
|
| 436 |
+
# Update existing session
|
| 437 |
+
conn.execute("UPDATE sessions SET ts = ? WHERE key = ?", (time.time(), key))
|
| 438 |
+
conn.commit()
|
| 439 |
+
return jsonify({"id": row[0], "new": False})
|
| 440 |
+
else:
|
| 441 |
+
# Create new session
|
| 442 |
+
conn.execute(
|
| 443 |
+
"INSERT INTO sessions (key, conv_id, ts) VALUES (?, ?, ?)",
|
| 444 |
+
(key, conv_id, time.time())
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
# Also create a conversations entry
|
| 448 |
+
conn.execute(
|
| 449 |
+
"INSERT OR IGNORE INTO conversations (conv_id, owner_key, preview, ts) VALUES (?, ?, ?, ?)",
|
| 450 |
+
(conv_id, key, "New chat", time.time())
|
| 451 |
+
)
|
| 452 |
+
conn.commit()
|
| 453 |
+
return jsonify({"id": conv_id, "new": True})
|
| 454 |
+
finally:
|
| 455 |
+
conn.close()
|
| 456 |
+
|
| 457 |
# ============================================================================
|
| 458 |
# MAIN FUNCTION
|
| 459 |
# ============================================================================
|