jinggujiwoo7 commited on
Commit
f68db6a
ยท
verified ยท
1 Parent(s): 5b97d7b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
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)