Spaces:
Sleeping
Sleeping
File size: 1,699 Bytes
8c581c2 a595762 8c581c2 a595762 8c581c2 a595762 8c581c2 a595762 8c581c2 a595762 8c581c2 a595762 |
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 |
import gradio as gr
import tensorflow as tf
from tensorflow.keras.preprocessing.text import tokenizer_from_json
from tensorflow.keras.preprocessing.sequence import pad_sequences
import json
import numpy as np
# Cargar el modelo
model = tf.keras.models.load_model('polarisatie_model.h5')
# Cargar el tokenizador
with open('tokenizer.json', 'r') as f:
tokenizer_json = json.load(f)
tokenizer = tokenizer_from_json(json.dumps(tokenizer_json))
# Cargar max_length
with open('max_length.txt', 'r') as f:
max_length = int(f.read().strip())
def preprocess_text(text):
sequence = tokenizer.texts_to_sequences([text])
padded = pad_sequences(sequence, maxlen=max_length)
return padded
def predict_polarization(text):
preprocessed_text = preprocess_text(text)
prediction = model.predict(preprocessed_text)
probability = float(prediction[0][1])
is_polarizing = bool(probability > 0.5)
response = "Polariserend" if is_polarizing else "Niet polariserend"
return {
"Is Polarizing": is_polarizing,
"Probability": f"{probability:.2%}",
"Response": response
}
# Crear la interfaz Gradio
iface = gr.Interface(
fn=predict_polarization,
inputs=gr.Textbox(lines=2, placeholder="Voer hier je Nederlandse tekst in..."),
outputs=gr.JSON(),
title="Dutch Text Polarization Detector",
description="Voer een Nederlandse tekst in om te bepalen of deze polariserend is.",
examples=[
["Dit is een neutrale zin."],
["Alle politici zijn leugenaars en dieven!"],
["Het weer is vandaag erg mooi."],
["Die groep mensen is de oorzaak van al onze problemen."]
]
)
# Lanzar la app
iface.launch() |