| from transformers import ElectraTokenizer, ElectraForQuestionAnswering, pipeline |
| from pprint import pprint |
| from konlpy.tag import Mecab |
| from flask import Flask, request, jsonify |
| import re |
|
|
| tokenizer = ElectraTokenizer.from_pretrained("monologg/koelectra-base-v3-finetuned-korquad") |
| model = ElectraForQuestionAnswering.from_pretrained("monologg/koelectra-base-v3-finetuned-korquad") |
| model = pipeline("question-answering", tokenizer=tokenizer, model=model, device=0) |
| mecab = Mecab() |
| pattern = r'\([^])]*\)' |
|
|
| app = Flask(__name__) |
|
|
| @app.route("/") |
| def main(): |
| return { "QA_CIRCULUS" : "V1.1_TEST_2211"} |
|
|
| @app.route("/qa", methods=['POST']) |
| def qa(): |
| question = request.json['q'] |
| context = request.json['c'] |
| result = model({ "question" : question, "context" : context }) |
| |
| answer = result["answer"] |
|
|
| if answer.find('(') > -1 and answer.find(')') < 0: |
| if(answer.startswith('(')): |
| answer.replace("(","") |
| else: |
| answer = answer + ")" |
| if answer.endswith('์'): |
| answer = answer.replace("์","") |
| answer = re.sub(pattern=pattern, repl='', string=answer ) |
|
|
| list = mecab.pos(result["answer"]) |
| print(list) |
| for word in list: |
| print(word[1]) |
| |
| |
| |
| |
| |
| |
| if word[1].startswith('JKO') or word[1].startswith('JKS') or word[1].startswith('JKB') or word[1].startswith('JX') or word[1].startswith('JC'): |
| answer = answer.replace(word[0],"") |
| if word[1].startswith('VCP'): |
| answer = answer.replace(word[0],"") |
| if word[1].startswith('SS'): |
| answer = answer.replace(word[0],"") |
| if word[1].endswith('F'): |
| answer = answer.replace(word[0],"") |
| |
| |
| |
| print(result) |
| |
| result["answer"] = answer |
| return result |
|
|
| |
| |
| if __name__ == '__main__': |
| app.run(host="0.0.0.0",port=5000) |