Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|