cesaenv commited on
Commit
14f18b1
verified
1 Parent(s): 2473437

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -59
app.py CHANGED
@@ -1,63 +1,56 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
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()