File size: 3,224 Bytes
8c581c2
 
a595762
 
8c581c2
 
fa9b96e
 
 
 
 
 
 
 
 
 
 
 
 
 
8c581c2
a595762
fa9b96e
 
 
 
 
 
 
 
 
 
8c581c2
a595762
fa9b96e
 
 
 
 
 
 
 
 
 
 
 
8c581c2
a595762
fa9b96e
 
 
 
 
 
 
 
 
 
 
8c581c2
a595762
fa9b96e
 
a595762
 
8c581c2
 
a595762
fa9b96e
 
 
a595762
fa9b96e
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
import os

# Función para cargar archivos con manejo de errores
def load_file(filename):
    try:
        filepath = os.path.join(os.path.dirname(__file__), filename)
        print(f"Attempting to load {filepath}")
        if not os.path.exists(filepath):
            print(f"File not found: {filepath}")
            return None
        return filepath
    except Exception as e:
        print(f"Error loading {filename}: {str(e)}")
        return None

# Cargar el modelo
model_path = load_file('polarisatie_model.h5')
if model_path:
    try:
        model = tf.keras.models.load_model(model_path)
        print("Model loaded successfully")
    except Exception as e:
        print(f"Error loading model: {str(e)}")
        model = None
else:
    model = None

# Cargar el tokenizador
tokenizer_path = load_file('tokenizer.json')
if tokenizer_path:
    try:
        with open(tokenizer_path, 'r') as f:
            tokenizer_json = json.load(f)
        tokenizer = tokenizer_from_json(json.dumps(tokenizer_json))
        print("Tokenizer loaded successfully")
    except Exception as e:
        print(f"Error loading tokenizer: {str(e)}")
        tokenizer = None
else:
    tokenizer = None

# Cargar max_length
max_length_path = load_file('max_length.txt')
if max_length_path:
    try:
        with open(max_length_path, 'r') as f:
            max_length = int(f.read().strip())
        print(f"Max length loaded: {max_length}")
    except Exception as e:
        print(f"Error loading max_length: {str(e)}")
        max_length = 100  # valor por defecto
else:
    max_length = 100  # valor por defecto

def preprocess_text(text):
    if tokenizer is None:
        return None
    sequence = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(sequence, maxlen=max_length)
    return padded

def predict_polarization(text):
    if model is None or tokenizer is None:
        return {"Error": "Model or tokenizer not loaded correctly"}
    
    preprocessed_text = preprocess_text(text)
    if preprocessed_text is None:
        return {"Error": "Failed to preprocess 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()