Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,12 +7,17 @@ import numpy as np
|
|
7 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
9 |
|
10 |
-
# Load the Keras model
|
11 |
-
def
|
12 |
-
|
|
|
|
|
13 |
return model
|
14 |
|
15 |
-
model
|
|
|
|
|
|
|
16 |
|
17 |
# Load the tokenizer from tokenizer.json
|
18 |
def load_tokenizer(tokenizer_path='tokenizer.json'):
|
@@ -21,18 +26,14 @@ def load_tokenizer(tokenizer_path='tokenizer.json'):
|
|
21 |
tokenizer = tokenizer_from_json(tokenizer_json)
|
22 |
return tokenizer
|
23 |
|
24 |
-
tokenizer = load_tokenizer()
|
25 |
-
|
26 |
# Load max_length from max_length.txt
|
27 |
def load_max_length(max_length_path='max_length.txt'):
|
28 |
with open(max_length_path, 'r') as f:
|
29 |
max_length = int(f.read().strip())
|
30 |
return max_length
|
31 |
|
32 |
-
max_length = load_max_length()
|
33 |
-
|
34 |
# Preprocessing function
|
35 |
-
def preprocess(text):
|
36 |
# Tokenize the text
|
37 |
sequences = tokenizer.texts_to_sequences([text])
|
38 |
# Pad the sequences
|
@@ -40,12 +41,12 @@ def preprocess(text):
|
|
40 |
return padded
|
41 |
|
42 |
# Prediction function
|
43 |
-
def predict_polarization(text):
|
44 |
if not text.strip():
|
45 |
return "Voer alstublieft een geldige zin in."
|
46 |
|
47 |
# Preprocess the text
|
48 |
-
processed_text = preprocess(text)
|
49 |
|
50 |
# Make prediction
|
51 |
prediction = model.predict(processed_text)
|
@@ -55,6 +56,12 @@ def predict_polarization(text):
|
|
55 |
|
56 |
return "Ja" if is_polarizing else "Nee"
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Define the Gradio interface using Blocks
|
59 |
with gr.Blocks() as demo:
|
60 |
gr.Markdown("# Polarisatie Thermometer")
|
@@ -67,7 +74,11 @@ with gr.Blocks() as demo:
|
|
67 |
|
68 |
result = gr.Textbox(label="Is polariserend?", interactive=False)
|
69 |
|
70 |
-
evaluate_button.click(
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Launch the Gradio app
|
73 |
if __name__ == "__main__":
|
|
|
7 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
9 |
|
10 |
+
# Load the Keras model architecture from model.json
|
11 |
+
def load_model_architecture(model_json_path='model.json'):
|
12 |
+
with open(model_json_path, 'r', encoding='utf-8') as json_file:
|
13 |
+
model_json = json_file.read()
|
14 |
+
model = tf.keras.models.model_from_json(model_json)
|
15 |
return model
|
16 |
|
17 |
+
# Load the model weights from polarisatie_model.h5
|
18 |
+
def load_model_weights(model, weights_path='polarisatie_model.h5'):
|
19 |
+
model.load_weights(weights_path)
|
20 |
+
return model
|
21 |
|
22 |
# Load the tokenizer from tokenizer.json
|
23 |
def load_tokenizer(tokenizer_path='tokenizer.json'):
|
|
|
26 |
tokenizer = tokenizer_from_json(tokenizer_json)
|
27 |
return tokenizer
|
28 |
|
|
|
|
|
29 |
# Load max_length from max_length.txt
|
30 |
def load_max_length(max_length_path='max_length.txt'):
|
31 |
with open(max_length_path, 'r') as f:
|
32 |
max_length = int(f.read().strip())
|
33 |
return max_length
|
34 |
|
|
|
|
|
35 |
# Preprocessing function
|
36 |
+
def preprocess(text, tokenizer, max_length):
|
37 |
# Tokenize the text
|
38 |
sequences = tokenizer.texts_to_sequences([text])
|
39 |
# Pad the sequences
|
|
|
41 |
return padded
|
42 |
|
43 |
# Prediction function
|
44 |
+
def predict_polarization(text, model, tokenizer, max_length):
|
45 |
if not text.strip():
|
46 |
return "Voer alstublieft een geldige zin in."
|
47 |
|
48 |
# Preprocess the text
|
49 |
+
processed_text = preprocess(text, tokenizer, max_length)
|
50 |
|
51 |
# Make prediction
|
52 |
prediction = model.predict(processed_text)
|
|
|
56 |
|
57 |
return "Ja" if is_polarizing else "Nee"
|
58 |
|
59 |
+
# Load all components
|
60 |
+
model = load_model_architecture()
|
61 |
+
model = load_model_weights(model)
|
62 |
+
tokenizer = load_tokenizer()
|
63 |
+
max_length = load_max_length()
|
64 |
+
|
65 |
# Define the Gradio interface using Blocks
|
66 |
with gr.Blocks() as demo:
|
67 |
gr.Markdown("# Polarisatie Thermometer")
|
|
|
74 |
|
75 |
result = gr.Textbox(label="Is polariserend?", interactive=False)
|
76 |
|
77 |
+
evaluate_button.click(
|
78 |
+
fn=lambda text: predict_polarization(text, model, tokenizer, max_length),
|
79 |
+
inputs=input_text,
|
80 |
+
outputs=result
|
81 |
+
)
|
82 |
|
83 |
# Launch the Gradio app
|
84 |
if __name__ == "__main__":
|