-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (68 loc) · 2.47 KB
/
app.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
from flask import Flask, request
from flask_restx import Api, Resource, fields
from flask_httpauth import HTTPTokenAuth
import json
import chains.openaiChain as openaiChain
import chains.T5Chain as T5Chain
import chains.T5ChainChatGPT as T5ChainChatGPT
import chains.stancedriven as stancedriven
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
load_dotenv()
authorizations = {
'apikey': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
}
}
app = Flask(__name__)
api = Api(app, authorizations=authorizations, security='apikey')
auth = HTTPTokenAuth()
with open("secrets.json") as secrets:
tokens = json.load(secrets)
@auth.verify_token
def verify_token(token):
if token in tokens:
print(tokens[token])
return tokens[token]
else:
print("Invalid token - " + token)
rephrase = api.model("Rephrase", {
"post": fields.String(description="The post to be rephrased", required=True)
})
rephraseT5WithParent = api.model("RephraseT5WithParent", {
"post": fields.String(description="The post to be rephrased", required=True),
"parent": fields.String(description="The parent post", required=False),
"parent_toxicity": fields.String(description="The toxicity of the parent post (high or low)", required=False),
"model": fields.String(description="The model to use", required=True)
})
stanceDrivenDemo = api.model("StanceDrivenDemo", {
"stances": fields.String("The stances to use", required=True)
})
@api.route('/chatgpt', endpoint='chatgpt')
@api.doc(body=rephrase, security='apikey')
class ChatGPT(Resource):
@auth.login_required
def post(self):
text = request.json["post"]
return openaiChain.runOpenAIChain(text)
@api.route('/t5', endpoint='t5')
@api.doc(body=rephraseT5WithParent, security='apikey')
class T5Endpoint(Resource):
@auth.login_required
def post(self):
model = request.json["model"]
rq = {k: v for k, v in request.json.items() if k != "model"}
if model == "chatgpt":
return T5ChainChatGPT.pcts_chatgpt(rq)
return T5Chain.pcts_gpt(rq)
@api.route('/stanceDriven', endpoint='stancedriven')
@api.doc(body=stanceDrivenDemo, security='apikey')
class StanceDrivenEndpoint(Resource):
@auth.login_required
def post(self):
rq = {k: v for k, v in request.json.items() if k != "model"}
return stancedriven.stance_detection(rq)
if __name__ == '__main__':
app.run(port=8000, host="0.0.0.0")