juancamval commited on
Commit
379062b
verified
1 Parent(s): 770af96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -14
app.py CHANGED
@@ -1,21 +1,91 @@
1
- import os
2
  import streamlit as st
 
3
  from transformers import pipeline
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- st.title("Testing Gemma")
7
- input_text = st.text_input("How can I help you: ")
8
- process = st.button("Ask")
 
 
 
 
9
 
10
- messages = [
11
- {"role": "user", "content": input_text},
12
- {"role": "system", "content": "You are a helpful assistant."}
13
- ]
14
- pipe = pipeline("text-generation", model="google/gemma-3-1b-it", token=os.getenv("HF_TOKEN"))
 
 
 
 
 
 
15
 
16
- if process and input_text:
17
- output = pipe(input_text)
18
- if output and isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
19
- st.write(output[0]["generated_text"])
 
 
 
 
20
  else:
21
- st.write("No response generated or unexpected output format.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import tensorflow as tf
3
  from transformers import pipeline
4
+ import os
5
 
6
+ # Cargar tus modelos de clasificaci贸n (asumiendo que est谩n en la ra铆z del proyecto)
7
+ try:
8
+ model_transformer_encoder = tf.keras.models.load_model('stacked_transformer_encoder.keras')
9
+ model_transformer_positional_encoding = tf.keras.models.load_model('transformer_encoder_pos.keras')
10
+ model_simple_rnn = tf.keras.models.load_model('model_simple_rnn.keras')
11
+ model_lstm = tf.keras.models.load_model('model_lstm.keras')
12
+ st.info("Modelos de clasificaci贸n cargados correctamente.")
13
+ except Exception as e:
14
+ st.error(f"Error al cargar los modelos de clasificaci贸n: {e}")
15
+ model_transformer_encoder = None
16
+ model_transformer_positional_encoding = None
17
+ model_simple_rnn = None
18
+ model_lstm = None
19
 
20
+ # Cargar el pipeline de traducci贸n de Hugging Face (ingl茅s a espa帽ol)
21
+ try:
22
+ translator_en_es = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
23
+ st.info("Modelo de traducci贸n (ingl茅s a espa帽ol) cargado correctamente.")
24
+ except Exception as e:
25
+ st.error(f"Error al cargar el modelo de traducci贸n (ingl茅s a espa帽ol): {e}")
26
+ translator_en_es = None
27
 
28
+ def clasificar_noticia(texto, modelo_seleccionado):
29
+ """Funci贸n para clasificar el texto usando el modelo seleccionado."""
30
+ modelo_a_usar = None
31
+ if modelo_seleccionado == "Transformer Encoder Apilado" and model_transformer_encoder is not None:
32
+ modelo_a_usar = model_transformer_encoder
33
+ elif modelo_seleccionado == "Transformer Positional Encoding" and model_transformer_positional_encoding is not None:
34
+ modelo_a_usar = model_transformer_positional_encoding
35
+ elif modelo_seleccionado == "Simple RNN" and model_simple_rnn is not None:
36
+ modelo_a_usar = model_simple_rnn
37
+ elif modelo_seleccionado == "LSTM" and model_lstm is not None:
38
+ modelo_a_usar = model_lstm
39
 
40
+ if modelo_a_usar:
41
+ # *** IMPORTANTE: Implementa aqu铆 la l贸gica de preprocesamiento del texto ***
42
+ # para que coincida con la entrada esperada por el modelo.
43
+ # Esto incluye tokenizaci贸n, padding, etc.
44
+ prediction = modelo_a_usar.predict([texto])
45
+ # *** IMPORTANTE: Interpreta la salida del modelo para obtener la clase predicha. ***
46
+ # Esto depender谩 de c贸mo entrenaste tu modelo.
47
+ return f"Clase predicha ({modelo_seleccionado}): {prediction}"
48
  else:
49
+ return f"El modelo '{modelo_seleccionado}' no est谩 disponible."
50
+
51
+ def traducir_texto_en_es(texto_en):
52
+ """Funci贸n para traducir texto de ingl茅s a espa帽ol."""
53
+ if translator_en_es:
54
+ result = translator_en_es(texto_en)[0]['translation_text']
55
+ return result
56
+ else:
57
+ return "El modelo de traducci贸n (ingl茅s a espa帽ol) no est谩 disponible."
58
+
59
+ def main():
60
+ st.title("Aplicaci贸n de Clasificaci贸n y Traducci贸n")
61
+
62
+ tab1, tab2 = st.tabs(["Clasificaci贸n de Noticias", "Traducci贸n (Ingl茅s a Espa帽ol)"])
63
+
64
+ with tab1:
65
+ st.header("Clasificaci贸n de Noticias")
66
+ input_texto_clasificacion = st.text_area("Ingresa la noticia aqu铆:")
67
+ modelos = ["Transformer Encoder Apilado", "Transformer Positional Encoding", "Simple RNN", "LSTM"]
68
+ modelo_seleccion = st.selectbox("Selecciona el modelo:", modelos)
69
+ boton_clasificar = st.button("Clasificar")
70
+ if boton_clasificar:
71
+ if input_texto_clasificacion:
72
+ resultado_clasificacion = clasificar_noticia(input_texto_clasificacion, modelo_seleccion)
73
+ st.subheader("Resultado de la clasificaci贸n:")
74
+ st.write(resultado_clasificacion)
75
+ else:
76
+ st.warning("Por favor, ingresa el texto de la noticia.")
77
+
78
+ with tab2:
79
+ st.header("Traducci贸n de Ingl茅s a Espa帽ol")
80
+ input_texto_traduccion = st.text_area("Ingresa el texto en ingl茅s:")
81
+ boton_traducir = st.button("Traducir")
82
+ if boton_traducir:
83
+ if input_texto_traduccion:
84
+ texto_traducido = traducir_texto_en_es(input_texto_traduccion)
85
+ st.subheader("Texto traducido al espa帽ol:")
86
+ st.write(texto_traducido)
87
+ else:
88
+ st.warning("Por favor, ingresa el texto en ingl茅s.")
89
+
90
+ if __name__ == "__main__":
91
+ main()