-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.py
177 lines (141 loc) · 4.92 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Backend server for the report.Ai extension
Make sure that you've added your cluster access in the environment
mongodb+srv://<username>:<password>@cluster0.dsdb23w.mongodb.net/
"""
# pylint: disable=E0401
# pylint: disable=W0718
import os
import json
from flask_cors import CORS
from flask import Flask, request, jsonify
from pymongo import MongoClient
from pymongo.server_api import ServerApi
from bson.json_util import dumps
import requests
app = Flask(__name__)
cors = CORS(app)
BART = os.environ.get("BART", None)
ROBERTA = os.environ.get("BART", None)
headersBart = {"Authorization": BART}
headersRoberta = {"Authorization": ROBERTA}
MONGO_URI = os.environ.get(
"mongo_uri", "mongodb+srv://admin:[email protected]/"
)
client = MongoClient(MONGO_URI, server_api=ServerApi("1"))
try:
client.admin.command("ping")
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
ratings_collection = client["report"]["rating"]
def summarizer(text):
"""summarizer function"""
api_url_summarizer = (
"https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
)
def query(payload):
data = json.dumps(payload)
response = requests.request(
"POST", api_url_summarizer, headers=headersBart, data=data, timeout=10
)
return json.loads(response.content.decode("utf-8"))
output = query(
{
"inputs": text,
"parameters": {"min_length": 100},
}
)
print(output)
return output[0]["summary_text"]
def ask_qna(text):
"""Question answer handler"""
api_url_qna = (
"https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
)
print(text)
def query(payload):
data = json.dumps(payload)
response = requests.request(
"POST", api_url_qna, headers=headersRoberta, data=data, timeout=10
)
return json.loads(response.content.decode("utf-8"))
data = query(
{
"inputs": {
"question": text["question"],
"context": text["context"],
}
}
)
return data
@app.route("/summarize", methods=["POST"])
def summarize():
"""Summarizer"""
text_data = request.get_json()
print(text_data)
# Adding CORS headers to the response
response = jsonify({"status": "success"})
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
summary = summarizer(text_data[:499])
response = jsonify({"summary": summary})
return response
@app.route("/save_entry", methods=["POST"])
def save_entry():
"""Rating saver"""
req_data = request.get_json()
print(req_data)
url = req_data["url"]
curr_rating = req_data["rating"]
res = ratings_collection.find_one({"url": url})
if res:
new_rating = (res["rating"] * res["freq"] + curr_rating) / (res["freq"] + 1)
update = {"$set": {"rating": new_rating, "freq": res["freq"] + 1}}
ratings_collection.update_many({"url": url}, update)
else:
new_doc = {"url": url, "rating": curr_rating, "freq": 1}
ratings_collection.insert_one(new_doc)
return jsonify({"status": "success"})
@app.route("/get_rating", methods=["POST"])
def get_rating_():
"""Rating fetcher"""
req_data = request.get_json()
url = req_data["url"]
res = ratings_collection.find_one({"url": url})
if res:
response = jsonify({"rating": res["rating"], "freq": res["freq"]})
else:
response = jsonify({"rating": 0, "freq": 0})
return response
@app.route("/ask-question", methods=["POST"])
def ask():
"""Question handler"""
text_data = request.get_json()
# Add CORS headers to the response
response = jsonify({"status": "success"})
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
response = jsonify({"answer": ask_qna(text_data)})
return response
@app.route("/top_ratings")
def top_ratings():
"""Fetching the top ratings"""
tops = ratings_collection.find().sort("rating", -1)
tops_list = []
for top in tops:
tops_list.append(dumps(top))
response = {"top": tops_list}
return jsonify(response)
@app.route("/")
def home():
"""Home page"""
return "Server is working, //[REPORT.AI]"
@app.route("/healthcheck")
def health_check():
"""Health check route"""
return "OK", 200
if __name__ == "__main__":
app.run(debug=True)