Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,54 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
import tensorflow as tf
|
|
|
|
|
4 |
import json
|
5 |
import numpy as np
|
6 |
|
7 |
-
|
8 |
-
|
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 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
tokenizer_json = f.read()
|
26 |
-
tokenizer = tokenizer_from_json(tokenizer_json)
|
27 |
-
return tokenizer
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
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
|
40 |
-
padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
|
41 |
return padded
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
evaluate_button = gr.Button("Evalueren")
|
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__":
|
85 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
import json
|
6 |
import numpy as np
|
7 |
|
8 |
+
# Cargar el modelo
|
9 |
+
model = tf.keras.models.load_model('polarisatie_model.h5')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Cargar el tokenizador
|
12 |
+
with open('tokenizer.json', 'r') as f:
|
13 |
+
tokenizer_json = json.load(f)
|
14 |
+
tokenizer = tokenizer_from_json(json.dumps(tokenizer_json))
|
15 |
|
16 |
+
# Cargar max_length
|
17 |
+
with open('max_length.txt', 'r') as f:
|
18 |
+
max_length = int(f.read().strip())
|
|
|
|
|
|
|
19 |
|
20 |
+
def preprocess_text(text):
|
21 |
+
sequence = tokenizer.texts_to_sequences([text])
|
22 |
+
padded = pad_sequences(sequence, maxlen=max_length)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return padded
|
24 |
|
25 |
+
def predict_polarization(text):
|
26 |
+
preprocessed_text = preprocess_text(text)
|
27 |
+
prediction = model.predict(preprocessed_text)
|
28 |
+
probability = float(prediction[0][1])
|
29 |
+
is_polarizing = bool(probability > 0.5)
|
30 |
+
response = "Polariserend" if is_polarizing else "Niet polariserend"
|
31 |
+
|
32 |
+
return {
|
33 |
+
"Is Polarizing": is_polarizing,
|
34 |
+
"Probability": f"{probability:.2%}",
|
35 |
+
"Response": response
|
36 |
+
}
|
37 |
+
|
38 |
+
# Crear la interfaz Gradio
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=predict_polarization,
|
41 |
+
inputs=gr.Textbox(lines=2, placeholder="Voer hier je Nederlandse tekst in..."),
|
42 |
+
outputs=gr.JSON(),
|
43 |
+
title="Dutch Text Polarization Detector",
|
44 |
+
description="Voer een Nederlandse tekst in om te bepalen of deze polariserend is.",
|
45 |
+
examples=[
|
46 |
+
["Dit is een neutrale zin."],
|
47 |
+
["Alle politici zijn leugenaars en dieven!"],
|
48 |
+
["Het weer is vandaag erg mooi."],
|
49 |
+
["Die groep mensen is de oorzaak van al onze problemen."]
|
50 |
+
]
|
51 |
+
)
|
52 |
+
|
53 |
+
# Lanzar la app
|
54 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|