File size: 2,243 Bytes
0c745fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import StandardScaler
import json
import re
from konlpy.tag import Okt
from tensorflow.keras.preprocessing.text import tokenizer_from_json
import pickle

# λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € 파일 λ‘œλ“œ
model = load_model('deep_learning_model(okt_drop).h5', compile=False)

with open('tokenizer(okt_drop).json', 'r', encoding='utf-8') as f:
    tokenizer_data = f.read()

tokenizer = tokenizer_from_json(tokenizer_data)

with open('scaler.pkl', 'rb') as f:
    scaler = pickle.load(f)

def calculate_sentence_stats(paragraph):
    paragraph = re.sub(r'\.{2,}', '.', paragraph)
    sentences = re.split(r'[.!?]', paragraph)
    sentence_lengths = [len(s.strip()) for s in sentences if s.strip()]
    sentence_count = len(sentence_lengths)
    average_length = sum(sentence_lengths) / len(sentence_lengths) if sentence_lengths else 0
    return sentence_count, average_length

def process_text(text):
    okt = Okt()
    texts = ' '.join(okt.nouns(text))
    sequences = tokenizer.texts_to_sequences([texts])
    max_len = 301
    X = pad_sequences(sequences, maxlen=max_len)
    return X

def predict_text(text, grade):
    X = process_text(text)
    sentence_count, sentence_average = calculate_sentence_stats(text)
    length = len(text)
    emoticon = 0
    numeric_features = np.array([[int(grade), length, emoticon, sentence_count, sentence_average]])
    numeric_features = scaler.transform(numeric_features)
    prediction = model.predict([X, numeric_features])
    predicted_label = '인곡지λŠ₯이 μƒμ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.' if prediction[0][0] > 0.5 else 'μ‚¬λžŒμ΄ μž‘μ„±ν•œ λ…μ„œκ°μƒλ¬Έμž…λ‹ˆλ‹€.'
    return predicted_label

iface = gr.Interface(
    fn=predict_text,
    inputs=[gr.Textbox(lines=10, placeholder="Enter Text Here..."), gr.Textbox(label="Grade")],
    outputs="text",
    title="λ…μ„œκ°μƒλ¬Έ 뢄석기",
    description="이 λ…μ„œκ°μƒλ¬Έμ΄ 학생에 μ˜ν•΄ μž‘μ„±λ˜μ—ˆλŠ”μ§€, 인곡지λŠ₯에 μ˜ν•΄ μƒμ„±λ˜μ—ˆλŠ”μ§€ λΆ„μ„ν•©λ‹ˆλ‹€."
)
iface.launch(debug=True)