pablofr commited on
Commit
a595762
·
verified ·
1 Parent(s): fc207db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -75
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
- 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'):
24
- with open(tokenizer_path, 'r', encoding='utf-8') as f:
25
- tokenizer_json = f.read()
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
40
- padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
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)
53
-
54
- # Assume the model outputs a probability
55
- is_polarizing = bool(prediction[0] > 0.5)
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")
68
- gr.Markdown("Voer een zin in om te beoordelen of deze polariserend is.")
69
-
70
- with gr.Row():
71
- input_text = gr.Textbox(label="Zin", placeholder="Schrijf hier een zin...", lines=2)
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()