JanetBackEnd/main_simple.py

93 lines
3.8 KiB
Python
Raw Normal View History

2023-04-08 04:04:24 +02:00
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import os
2023-04-19 04:57:54 +02:00
import re
2023-04-19 15:22:22 +02:00
import psycopg2
import requests
2023-04-19 15:44:22 +02:00
import time
2023-04-08 04:04:24 +02:00
app = Flask(__name__)
url = os.getenv("FRONTEND_URL_WITH_PORT")
2023-04-19 14:52:05 +02:00
cors = CORS(app, resources={r"/api/predict": {"origins": url},
r"/api/feedback": {"origins": url},
r"/api/dm": {"origins": url},
r"/health": {"origins": "*"}
})
users = {}
2023-04-19 15:22:22 +02:00
2023-04-19 14:52:05 +02:00
conn = psycopg2.connect(
host="janet-pg",
database=os.getenv("POSTGRES_DB"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"))
2023-04-08 04:04:24 +02:00
2023-04-19 15:22:22 +02:00
cur = conn.cursor()
2023-04-08 04:04:24 +02:00
@app.route("/health", methods=['GET'])
2023-04-08 15:15:18 +02:00
def health():
2023-04-08 04:04:24 +02:00
return "Success", 200
2023-04-19 14:52:05 +02:00
@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
2023-04-08 04:04:24 +02:00
@app.route("/api/predict", methods=['POST'])
def predict():
2023-04-19 15:44:22 +02:00
time.sleep(10)
2023-04-08 04:04:24 +02:00
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")
2023-04-19 14:52:05 +02:00
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()
2023-04-08 04:04:24 +02:00
reply = jsonify({"status": "done"})
return reply
if __name__ == "__main__":
2023-04-19 15:22:22 +02:00
2023-04-19 14:52:05 +02:00
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()
2023-04-19 15:22:22 +02:00
2023-04-09 19:14:35 +02:00
app.run(host='0.0.0.0')