|
from datetime import datetime |
|
from flask import Flask, request, jsonify, make_response |
|
from flask_cors import CORS |
|
from datetime import datetime |
|
import openai |
|
import os |
|
import random |
|
import therapist |
|
|
|
|
|
|
|
INITIAL_MESSAGE = "Hi! I'm Brainy, a virtual assistant who can guide you through the mental health screening process. Can you start by telling me a bit about the problems you are experiencing?" |
|
HASH_LENGTH = 12 |
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
users = {} |
|
message_history = {} |
|
|
|
|
|
|
|
def clean_users(users=users, message_history=message_history): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
@app.route("/") |
|
def index(): |
|
return app.send_static_file("index.html") |
|
|
|
@app.route('/get_initial', methods=['GET']) |
|
def get_initial(): |
|
response = therapist.initial_message |
|
|
|
connect_time = datetime.now() |
|
user_id = str("%030x" % random.randrange(16 ** HASH_LENGTH))[-HASH_LENGTH:] |
|
|
|
users[user_id] = { |
|
"user_id": user_id, |
|
"connect_time": connect_time, |
|
"state": ["initial"], |
|
"message_history": [therapist.initial_message]} |
|
|
|
clean_users() |
|
|
|
return jsonify({"response": response, "user_id":user_id}) |
|
|
|
@app.route('/get_response', methods=['GET']) |
|
def get_response(): |
|
message = request.args.get("message") |
|
user_id = request.args.get("user_id") |
|
user = users[user_id] |
|
|
|
if not message.strip(): |
|
response = "Please enter a message." |
|
else: |
|
state, response = therapist.generate_response(user, message) |
|
user["state"] = state |
|
user["message_history"].extend([message, response]) |
|
|
|
return jsonify({"response": response}) |
|
|
|
if __name__ == '__main__': |
|
app.run() |
|
|