File size: 2,380 Bytes
c15be89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7a9b1c
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
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 }) 
    #print(result) 
    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] in ["JX","JKB","JKO"]: #Josa #Adjective 
        #if word[1].startswith('J'):
        #if answer.endswith('์˜'):
        #    answer = answer.replace('์˜','')
        #answer = answer.replace('์ด๋‹ค','')
        #answer = answer.replace('๋ผ๋Š”','')
        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],"")                        
        #if answer.find('(') > -1 and answer.find(')') < 0:
        #    answer = answer + ")"
        #if answer.find(''  
    print(result)
    
    result["answer"] = answer 
    return result 

#if __name__ == "__main__": 
#  uvicorn.run("qa_server:app", host="0.0.0.0", port=8000) 
if __name__ == '__main__':
    app.run(host="0.0.0.0",port=5000)