yongyeol commited on
Commit
434f993
Β·
verified Β·
1 Parent(s): 717e774

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -57
app.py CHANGED
@@ -1,57 +1,64 @@
1
- import gradio as gr
2
- import numpy as np
3
- from tensorflow.keras.models import load_model
4
- from tensorflow.keras.preprocessing.sequence import pad_sequences
5
- from sklearn.preprocessing import StandardScaler
6
- import json
7
- import re
8
- from konlpy.tag import Okt
9
- from tensorflow.keras.preprocessing.text import tokenizer_from_json
10
- import pickle
11
-
12
- # λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € 파일 λ‘œλ“œ
13
- model = load_model('deep_learning_model(okt_drop).h5', compile=False)
14
-
15
- with open('tokenizer(okt_drop).json', 'r', encoding='utf-8') as f:
16
- tokenizer_data = f.read()
17
-
18
- tokenizer = tokenizer_from_json(tokenizer_data)
19
-
20
- with open('scaler.pkl', 'rb') as f:
21
- scaler = pickle.load(f)
22
-
23
- def calculate_sentence_stats(paragraph):
24
- paragraph = re.sub(r'\.{2,}', '.', paragraph)
25
- sentences = re.split(r'[.!?]', paragraph)
26
- sentence_lengths = [len(s.strip()) for s in sentences if s.strip()]
27
- sentence_count = len(sentence_lengths)
28
- average_length = sum(sentence_lengths) / len(sentence_lengths) if sentence_lengths else 0
29
- return sentence_count, average_length
30
-
31
- def process_text(text):
32
- okt = Okt()
33
- texts = ' '.join(okt.nouns(text))
34
- sequences = tokenizer.texts_to_sequences([texts])
35
- max_len = 301
36
- X = pad_sequences(sequences, maxlen=max_len)
37
- return X
38
-
39
- def predict_text(text, grade):
40
- X = process_text(text)
41
- sentence_count, sentence_average = calculate_sentence_stats(text)
42
- length = len(text)
43
- emoticon = 0
44
- numeric_features = np.array([[int(grade), length, emoticon, sentence_count, sentence_average]])
45
- numeric_features = scaler.transform(numeric_features)
46
- prediction = model.predict([X, numeric_features])
47
- predicted_label = '인곡지λŠ₯이 μƒμ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.' if prediction[0][0] > 0.5 else 'μ‚¬λžŒμ΄ μž‘μ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.'
48
- return predicted_label
49
-
50
- iface = gr.Interface(
51
- fn=predict_text,
52
- inputs=[gr.Textbox(lines=10, placeholder="Enter Text Here..."), gr.Textbox(label="Grade")],
53
- outputs="text",
54
- title="λ…μ„œκ°μƒλ¬Έ 뢄석기",
55
- description="이 λ…μ„œκ°μƒλ¬Έμ΄ 학생에 μ˜ν•΄ μž‘μ„±λ˜μ—ˆλŠ”μ§€, 인곡지λŠ₯에 μ˜ν•΄ μƒμ„±λ˜μ—ˆλŠ”μ§€ λΆ„μ„ν•©λ‹ˆλ‹€."
56
- )
57
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
5
+ from sklearn.preprocessing import StandardScaler
6
+ import json
7
+ import re
8
+ from konlpy.tag import Okt
9
+ from tensorflow.keras.preprocessing.text import tokenizer_from_json
10
+ import pickle
11
+ import jpype
12
+ import os
13
+
14
+ # JVM μ΄ˆκΈ°ν™”
15
+ if not jpype.isJVMStarted():
16
+ jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=/usr/local/lib/python3.10/site-packages/konlpy/java/lib/komoran.jar", "-ea")
17
+
18
+ # λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € 파일 λ‘œλ“œ
19
+ model = load_model('deep_learning_model(okt_drop).h5', compile=False)
20
+
21
+ with open('tokenizer(okt_drop).json', 'r', encoding='utf-8') as f:
22
+ tokenizer_data = f.read()
23
+
24
+ tokenizer = tokenizer_from_json(tokenizer_data)
25
+
26
+ with open('scaler.pkl', 'rb') as f:
27
+ scaler = pickle.load(f)
28
+
29
+ def calculate_sentence_stats(paragraph):
30
+ paragraph = re.sub(r'\.{2,}', '.', paragraph)
31
+ sentences = re.split(r'[.!?]', paragraph)
32
+ sentence_lengths = [len(s.strip()) for s in sentences if s.strip()]
33
+ sentence_count = len(sentence_lengths)
34
+ average_length = sum(sentence_lengths) / len(sentence_lengths) if sentence_lengths else 0
35
+ return sentence_count, average_length
36
+
37
+ def process_text(text):
38
+ okt = Okt()
39
+ texts = ' '.join(okt.nouns(text))
40
+ sequences = tokenizer.texts_to_sequences([texts])
41
+ max_len = 301
42
+ X = pad_sequences(sequences, maxlen=max_len)
43
+ return X
44
+
45
+ def predict_text(text, grade):
46
+ X = process_text(text)
47
+ sentence_count, sentence_average = calculate_sentence_stats(text)
48
+ length = len(text)
49
+ emoticon = 0
50
+ numeric_features = np.array([[int(grade), length, emoticon, sentence_count, sentence_average]])
51
+ numeric_features = scaler.transform(numeric_features)
52
+ prediction = model.predict([X, numeric_features])
53
+ predicted_label = '인곡지λŠ₯이 μƒμ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.' if prediction[0][0] > 0.5 else 'μ‚¬λžŒμ΄ μž‘μ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.'
54
+ return predicted_label
55
+
56
+ iface = gr.Interface(
57
+ fn=predict_text,
58
+ inputs=[gr.Textbox(lines=10, placeholder="Enter Text Here..."), gr.Textbox(label="Grade")],
59
+ outputs="text",
60
+ title="λ…μ„œκ°μƒλ¬Έ 뢄석기",
61
+ description="이 λ…μ„œκ°μƒλ¬Έμ΄ 학생에 μ˜ν•΄ μž‘μ„±λ˜μ—ˆλŠ”μ§€, 인곡지λŠ₯에 μ˜ν•΄ μƒμ„±λ˜μ—ˆλŠ”μ§€ λΆ„μ„ν•©λ‹ˆλ‹€."
62
+ )
63
+ iface.launch(debug=True)
64
+