Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,56 @@
|
|
1 |
import gradio as gr
|
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 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
import pickle
|
6 |
+
|
7 |
+
# Cargar el modelo y el tokenizer
|
8 |
+
model_path = "mi_modelo.h5"
|
9 |
+
tokenizer_path = "tokenizer.pkl"
|
10 |
+
modelo = load_model(model_path)
|
11 |
+
with open(tokenizer_path, 'rb') as file:
|
12 |
+
tokenizer = pickle.load(file)
|
13 |
+
|
14 |
+
# Diccionario de consejer铆as y n煤meros
|
15 |
+
consejeria_a_numero = {
|
16 |
+
'Consejer铆a de Agricultura, Ganader铆a y Medio Ambiente': 0,
|
17 |
+
'Consejer铆a de Salud': 1,
|
18 |
+
'Consejer铆a de Pol铆ticas Sociales, Familia, Igualdad y Justicia': 2,
|
19 |
+
'Consejer铆a de Fomento y Pol铆tica Territorial': 3,
|
20 |
+
'Consejer铆a de Desarrollo Econ贸mico e Innovaci贸n': 4,
|
21 |
+
'Consejer铆a de Desarrollo Econ贸mico e Innovaci贸nII': 5,
|
22 |
+
'Consejer铆a de Educaci贸n, Formaci贸n y Empleo': 6,
|
23 |
+
'Consejer铆a de Administraci贸n P煤blica y Hacienda': 7,
|
24 |
+
'Consejer铆a de Presidencia, Relaciones Institucionales y Acci贸n Exterior': 8
|
25 |
+
}
|
26 |
+
|
27 |
+
numero_a_consejeria = {v: k for k, v in consejeria_a_numero.items()}
|
28 |
+
|
29 |
+
def predict_consejeria(description):
|
30 |
+
# Preprocesar la descripci贸n
|
31 |
+
description_sequence = tokenizer.texts_to_sequences([description])
|
32 |
+
maxlen = 450
|
33 |
+
description_padded = pad_sequences(description_sequence, maxlen=maxlen)
|
34 |
+
|
35 |
+
# Realizar la predicci贸n
|
36 |
+
prediction = modelo.predict(description_padded)
|
37 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
38 |
+
predicted_consejeria = numero_a_consejeria[predicted_class]
|
39 |
+
return predicted_consejeria
|
40 |
+
|
41 |
+
# Definir la funci贸n respond adaptada a nuestro modelo
|
42 |
+
def respond(description):
|
43 |
+
predicted_consejeria = predict_consejeria(description)
|
44 |
+
return predicted_consejeria
|
45 |
+
|
46 |
+
# Crear la interfaz de Gradio
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=respond,
|
49 |
+
inputs=gr.Textbox(lines=2, label="Descripci贸n"),
|
50 |
+
outputs="text",
|
51 |
+
title="Clasificaci贸n de Consejer铆as",
|
52 |
+
description="Introduce una descripci贸n para predecir a qu茅 consejer铆a pertenece."
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
|
|
55 |
if __name__ == "__main__":
|
56 |
+
demo.launch()
|