Spaces:
Runtime error
Runtime error
jinggujiwoo7
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Flask ์ ํ๋ฆฌ์ผ์ด์
์ด๊ธฐํ
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# ๋ฌธ๋ฒ ์ฒดํฌ๋ฅผ ์ํ 'fill-mask' ํ์ดํ๋ผ์ธ ๋ก๋
|
8 |
+
grammar_checker = pipeline('fill-mask', model='bert-base-uncased')
|
9 |
+
|
10 |
+
# ๋ฌธ๋ฒ ์ฒดํฌ ํจ์
|
11 |
+
def check_grammar(sentence):
|
12 |
+
words = sentence.split()
|
13 |
+
checked_sentence = []
|
14 |
+
|
15 |
+
for i in range(len(words)):
|
16 |
+
original_word = words[i]
|
17 |
+
# ๋ง์คํฌ๋ ๋ฌธ์ฅ ์์ฑ
|
18 |
+
masked_sentence = ' '.join(words[:i] + ['[MASK]'] + words[i+1:])
|
19 |
+
# ๋ชจ๋ธ์ ์ด์ฉํด ๋ง์คํฌ๋ ๋จ์ด ์์ธก
|
20 |
+
result = grammar_checker(masked_sentence)
|
21 |
+
# ์๋ ๋จ์ด์ ๊ฐ์ฅ ๋น์ทํ ์์ธก ๊ฒฐ๊ณผ ์ ํ
|
22 |
+
best_prediction = min(result, key=lambda x: abs(len(x['token_str']) - len(original_word)))
|
23 |
+
suggested_word = best_prediction['token_str']
|
24 |
+
checked_sentence.append(suggested_word)
|
25 |
+
|
26 |
+
return ' '.join(checked_sentence)
|
27 |
+
|
28 |
+
# ์๋ํฌ์ธํธ ์ ์
|
29 |
+
@app.route('/check_grammar', methods=['POST'])
|
30 |
+
def grammar_check_endpoint():
|
31 |
+
data = request.json
|
32 |
+
sentence = data.get('sentence', '')
|
33 |
+
corrected_sentence = check_grammar(sentence)
|
34 |
+
return jsonify({
|
35 |
+
'original': sentence,
|
36 |
+
'corrected': corrected_sentence
|
37 |
+
})
|
38 |
+
|
39 |
+
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
40 |
+
if __name__ == '__main__':
|
41 |
+
app.run(debug=True)
|