-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (61 loc) · 3.13 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
from flask import Flask, request, jsonify
from utils.config import cache, config
from models.summarize import perform_summarize
from models.sentiment_analysis import perform_sentiment_analysis
from models.language_detection import perform_language_detection
from models.question_answerer import perform_question_answerer
from models.text_generation import perform_text_generation
from models.image_classification import perform_classify_image
from models.text_classification import perform_classify_text
from utils.translate import translate_request, perform_translate
from utils.pipelines_helper import pipeline_file
app = Flask(__name__)
cache.init_app(app, config=config)
@cache.cached()
@app.route('/ping')
def ping():
return {"content": "pong"}
@cache.cached()
@app.route('/')
def root():
return jsonify(content=pipeline_file())
@app.route('/summarize', methods=['POST'])
def summarize():
request_content = translate_request(request.get_json())
response = perform_summarize(request_content['content'])
translated_response = perform_translate(response[0]["summary_text"], source_lang="en",
target_lang=request_content['lang'])
return {"content": translated_response}
@app.route('/sentiment-analysis', methods=['POST'])
def sentiment_analysis():
request_content = translate_request(request.get_json())
return {"content": perform_sentiment_analysis(request_content['content'])}
@app.route('/language-detection', methods=['POST'])
def language_detection():
request_content = request.get_json()
return {"content": perform_language_detection(request_content['content'])}
@app.route('/question-answerer', methods=['POST'])
def answer():
request_content = request.get_json()
question = translate_request({'content': request_content['content']['question']})
knowledge = translate_request({'content': request_content['content']['knowledge']})
lang = question['lang']
response = perform_question_answerer({"question": question['content'], "knowledge": knowledge['content']})
translated_response = perform_translate(response["answer"], source_lang="en", target_lang=lang)
return {"content": translated_response}
@app.route('/generate-text', methods=['POST'])
def generate():
request_content = translate_request(request.get_json())
response = perform_text_generation(request_content['content'], request_content['max_length'])
translated_response = perform_translate(response[0]["generated_text"], source_lang="en",
target_lang=request_content['lang'])
return {"content": translated_response}
@app.route('/classify-image', methods=['POST'])
def classify_image():
request_content = request.get_json()
return {"content": perform_classify_image(request_content['content'])}
@app.route('/classify-text', methods=['POST'])
def classify_text():
request_content = translate_request(request.get_json())
multi_label = (request_content['multi_label'] is True or request_content['multi_label'] == 'true')
return {"content": perform_classify_text(request_content['content'], request_content['labels'], multi_label)}