JanetBackEnd/main_simple.py

93 lines
3.8 KiB
Python

from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import os
import re
import psycopg2
import requests
import time
app = Flask(__name__)
url = os.getenv("FRONTEND_URL_WITH_PORT")
cors = CORS(app, resources={r"/api/predict": {"origins": url},
r"/api/feedback": {"origins": url},
r"/api/dm": {"origins": url},
r"/health": {"origins": "*"}
})
users = {}
conn = psycopg2.connect(
host="janet-pg",
database=os.getenv("POSTGRES_DB"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"))
cur = conn.cursor()
@app.route("/health", methods=['GET'])
def health():
return "Success", 200
@app.route("/api/dm", methods=['POST'])
def init_dm():
token = request.get_json().get("token")
status = request.get_json().get("stat")
if status == "start":
message = {"stat": "waiting"}
elif status == "set":
headers = {"gcube-token": token, "Accept": "application/json"}
if token not in users:
url = 'https://api.d4science.org/rest/2/people/profile'
response = requests.get(url, headers=headers)
if response.status_code == 200:
username = response.json()['result']['username']
name = response.json()['result']['fullname']
message = {"stat": "done"}
else:
message = {"stat": "rejected"}
else:
message = {"stat": "done"}
return message
@app.route("/api/predict", methods=['POST'])
def predict():
time.sleep(10)
text = request.get_json().get("message")
message = {"answer": "answer", "query": "text", "cand": "candidate", "history": "history", "modQuery": "modQuery"}
reply = jsonify(message)
return reply
@app.route('/api/feedback', methods = ['POST'])
def feedback():
data = request.get_json().get("feedback")
print(data)
cur.execute('INSERT INTO feedback_experimental (query, history, janet_modified_query, is_modified_query_correct, user_modified_query, evidence_useful, response, preferred_response, response_length_feedback, response_fluency_feedback, response_truth_feedback, response_useful_feedback, response_time_feedback, response_intent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
(data['query'], data['history'], data['modQuery'],
data['queryModCorrect'], data['correctQuery'], data['evidence'], data['janetResponse'], data['preferredResponse'], data['length'],
data['fluency'], data['truthfulness'], data['usefulness'],
data['speed'], data['intent'])
)
conn.commit()
reply = jsonify({"status": "done"})
return reply
if __name__ == "__main__":
cur.execute('CREATE TABLE IF NOT EXISTS feedback_experimental (id serial PRIMARY KEY,'
'query text NOT NULL,'
'history text NOT NULL,'
'janet_modified_query text NOT NULL,'
'is_modified_query_correct text NOT NULL,'
'user_modified_query text, evidence_useful text NOT NULL,'
'response text NOT NULL,'
'preferred_response text,'
'response_length_feedback text NOT NULL,'
'response_fluency_feedback text NOT NULL,'
'response_truth_feedback text NOT NULL,'
'response_useful_feedback text NOT NULL,'
'response_time_feedback text NOT NULL,'
'response_intent text NOT NULL);'
)
conn.commit()
app.run(host='0.0.0.0')