Upload qa_server2.py
Browse files- qa_server2.py +65 -0
qa_server2.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ElectraTokenizer, ElectraForQuestionAnswering, pipeline
|
| 2 |
+
from pprint import pprint
|
| 3 |
+
from konlpy.tag import Mecab
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
tokenizer = ElectraTokenizer.from_pretrained("monologg/koelectra-base-v3-finetuned-korquad")
|
| 8 |
+
model = ElectraForQuestionAnswering.from_pretrained("monologg/koelectra-base-v3-finetuned-korquad")
|
| 9 |
+
model = pipeline("question-answering", tokenizer=tokenizer, model=model, device=0)
|
| 10 |
+
mecab = Mecab()
|
| 11 |
+
pattern = r'\([^])]*\)'
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
|
| 15 |
+
@app.route("/")
|
| 16 |
+
def main():
|
| 17 |
+
return { "QA_CIRCULUS" : "V1.1_TEST_2211"}
|
| 18 |
+
|
| 19 |
+
@app.route("/qa", methods=['POST'])
|
| 20 |
+
def qa():
|
| 21 |
+
question = request.json['q']
|
| 22 |
+
context = request.json['c']
|
| 23 |
+
result = model({ "question" : question, "context" : context })
|
| 24 |
+
#print(result)
|
| 25 |
+
answer = result["answer"]
|
| 26 |
+
|
| 27 |
+
if answer.find('(') > -1 and answer.find(')') < 0:
|
| 28 |
+
if(answer.startswith('(')):
|
| 29 |
+
answer.replace("(","")
|
| 30 |
+
else:
|
| 31 |
+
answer = answer + ")"
|
| 32 |
+
if answer.endswith('์'):
|
| 33 |
+
answer = answer.replace("์","")
|
| 34 |
+
answer = re.sub(pattern=pattern, repl='', string=answer )
|
| 35 |
+
|
| 36 |
+
list = mecab.pos(result["answer"])
|
| 37 |
+
print(list)
|
| 38 |
+
for word in list:
|
| 39 |
+
print(word[1])
|
| 40 |
+
#if word[1] in ["JX","JKB","JKO"]: #Josa #Adjective
|
| 41 |
+
#if word[1].startswith('J'):
|
| 42 |
+
#if answer.endswith('์'):
|
| 43 |
+
# answer = answer.replace('์','')
|
| 44 |
+
#answer = answer.replace('์ด๋ค','')
|
| 45 |
+
#answer = answer.replace('๋ผ๋','')
|
| 46 |
+
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'):
|
| 47 |
+
answer = answer.replace(word[0],"")
|
| 48 |
+
if word[1].startswith('VCP'):
|
| 49 |
+
answer = answer.replace(word[0],"")
|
| 50 |
+
if word[1].startswith('SS'):
|
| 51 |
+
answer = answer.replace(word[0],"")
|
| 52 |
+
if word[1].endswith('F'):
|
| 53 |
+
answer = answer.replace(word[0],"")
|
| 54 |
+
#if answer.find('(') > -1 and answer.find(')') < 0:
|
| 55 |
+
# answer = answer + ")"
|
| 56 |
+
#if answer.find(''
|
| 57 |
+
print(result)
|
| 58 |
+
|
| 59 |
+
result["answer"] = answer
|
| 60 |
+
return result
|
| 61 |
+
|
| 62 |
+
#if __name__ == "__main__":
|
| 63 |
+
# uvicorn.run("qa_server:app", host="0.0.0.0", port=8000)
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
app.run()
|