Spaces:
Sleeping
Sleeping
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 | |
model = load_model('deep_learning_model(okt_drop).h5') | |
with open('tokenizer(okt_drop).json', 'r', encoding='utf-8') as f: | |
tokenizer_data = f.read() | |
tokenizer = tokenizer_from_json(tokenizer_data) | |
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]]) | |
scaler = StandardScaler() | |
numeric_features = scaler.fit_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() | |