pablofr commited on
Commit
fa9b96e
verified
1 Parent(s): b560b40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -6
app.py CHANGED
@@ -4,26 +4,75 @@ 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)
 
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
5
  import json
6
  import numpy as np
7
+ import os
8
+
9
+ # Funci贸n para cargar archivos con manejo de errores
10
+ def load_file(filename):
11
+ try:
12
+ filepath = os.path.join(os.path.dirname(__file__), filename)
13
+ print(f"Attempting to load {filepath}")
14
+ if not os.path.exists(filepath):
15
+ print(f"File not found: {filepath}")
16
+ return None
17
+ return filepath
18
+ except Exception as e:
19
+ print(f"Error loading {filename}: {str(e)}")
20
+ return None
21
 
22
  # Cargar el modelo
23
+ model_path = load_file('polarisatie_model.h5')
24
+ if model_path:
25
+ try:
26
+ model = tf.keras.models.load_model(model_path)
27
+ print("Model loaded successfully")
28
+ except Exception as e:
29
+ print(f"Error loading model: {str(e)}")
30
+ model = None
31
+ else:
32
+ model = None
33
 
34
  # Cargar el tokenizador
35
+ tokenizer_path = load_file('tokenizer.json')
36
+ if tokenizer_path:
37
+ try:
38
+ with open(tokenizer_path, 'r') as f:
39
+ tokenizer_json = json.load(f)
40
+ tokenizer = tokenizer_from_json(json.dumps(tokenizer_json))
41
+ print("Tokenizer loaded successfully")
42
+ except Exception as e:
43
+ print(f"Error loading tokenizer: {str(e)}")
44
+ tokenizer = None
45
+ else:
46
+ tokenizer = None
47
 
48
  # Cargar max_length
49
+ max_length_path = load_file('max_length.txt')
50
+ if max_length_path:
51
+ try:
52
+ with open(max_length_path, 'r') as f:
53
+ max_length = int(f.read().strip())
54
+ print(f"Max length loaded: {max_length}")
55
+ except Exception as e:
56
+ print(f"Error loading max_length: {str(e)}")
57
+ max_length = 100 # valor por defecto
58
+ else:
59
+ max_length = 100 # valor por defecto
60
 
61
  def preprocess_text(text):
62
+ if tokenizer is None:
63
+ return None
64
  sequence = tokenizer.texts_to_sequences([text])
65
  padded = pad_sequences(sequence, maxlen=max_length)
66
  return padded
67
 
68
  def predict_polarization(text):
69
+ if model is None or tokenizer is None:
70
+ return {"Error": "Model or tokenizer not loaded correctly"}
71
+
72
  preprocessed_text = preprocess_text(text)
73
+ if preprocessed_text is None:
74
+ return {"Error": "Failed to preprocess text"}
75
+
76
  prediction = model.predict(preprocessed_text)
77
  probability = float(prediction[0][1])
78
  is_polarizing = bool(probability > 0.5)