-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.py
90 lines (74 loc) · 2.52 KB
/
apis.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
from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS # Import CORS
import requests
import socket
headers = {"Authorization": "Bearer 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"}
# creating the flask app
app = Flask(__name__)
# creating an API object
api = Api(app)
CORS(app, resources={r"/": {"origins": ""}})
# making a class for a particular resource
# the get, post methods correspond to get and post requests
# they are automatically mapped by flask_restful.
# other methods include put, delete, etc.
'''
"inputs": "content"
'''
class T5_small(Resource):
@staticmethod
def post():
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
payload = request.get_json()
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
'''
"inputs": {
"question": "What is my name?",
"context": "My name is Clara and I live in Berkeley."
}
'''
class qna(Resource):
@staticmethod
def post():
API_URL = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
payload = request.get_json()
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
'''
{
"inputs": "My name is Sarah Jessica Parker but you can call me Jessica", "parameters": {"src_lang": "en_XX", "tgt_lang": "fr_XX"}
}
'''
class translate(Resource):
@staticmethod
def post():
API_URL = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-many-mmt"
payload = request.get_json()
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
'''
{"inputs": "The answer to the universe is"}
'''
class text_generation(Resource):
@staticmethod
def post():
API_URL = "https://api-inference.huggingface.co/models/gpt2"
payload = request.get_json()
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
class say_hello(Resource):
@staticmethod
def get():
return f"container ID: {socket.gethostname()}"
# adding the defined resources along with their corresponding urls
api.add_resource(T5_small, '/summarize')
api.add_resource(qna, '/qna')
api.add_resource(translate, '/translate')
api.add_resource(text_generation, '/text_generation')
api.add_resource(say_hello, '/')
# driver function
if __name__ == '__main__':
app.run(debug=False)
#docker compose up -d --build --scale server=3