Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,91 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
@st.cache_resource
|
| 6 |
-
def load_model():
|
| 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 |
-
st.
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
else:
|
| 37 |
-
st.error("Por favor, introduce
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Cargar modelos
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_model(model_type):
|
| 7 |
+
if model_type == 'gpt2':
|
| 8 |
+
return pipeline('text-generation', model='gpt2')
|
| 9 |
+
elif model_type == 'translation_en_to_fr':
|
| 10 |
+
return pipeline('translation_en_to_fr')
|
| 11 |
+
elif model_type == 'translation_en_to_ar':
|
| 12 |
+
return pipeline('translation_en_to_ar')
|
| 13 |
+
elif model_type == 'translation_en_to_en':
|
| 14 |
+
return pipeline('text-generation', model='gpt2')
|
| 15 |
+
|
| 16 |
+
# Modelos de traducción y generación de texto
|
| 17 |
+
generator = load_model('gpt2')
|
| 18 |
+
translator_en_fr = load_model('translation_en_to_fr')
|
| 19 |
+
translator_en_ar = load_model('translation_en_to_ar')
|
| 20 |
+
|
| 21 |
+
# Interfaz de usuario
|
| 22 |
+
st.title("Transformador de Texto Técnico")
|
| 23 |
+
st.write("Reescribe un texto técnico para hacerlo accesible y traduce a varios idiomas.")
|
| 24 |
+
|
| 25 |
+
# Entrada de texto técnico
|
| 26 |
+
technical_text = st.text_area("Texto técnico para reescribir:", "")
|
| 27 |
+
|
| 28 |
+
# Entrada del texto del experimento
|
| 29 |
+
experiment_text = st.text_area("Texto de experimento para inferir conclusiones:", "")
|
| 30 |
+
|
| 31 |
+
# Botón para procesar el texto
|
| 32 |
+
if st.button("Procesar texto"):
|
| 33 |
+
if technical_text and experiment_text:
|
| 34 |
+
# Reescribir el texto técnico en un lenguaje accesible
|
| 35 |
+
with st.spinner("Reescribiendo texto técnico..."):
|
| 36 |
+
rewritten_text = generator(technical_text, do_sample=True, min_length=50, max_length=100)[0]['generated_text']
|
| 37 |
+
|
| 38 |
+
# Traducir a inglés, árabe y francés
|
| 39 |
+
with st.spinner("Traduciendo a inglés, árabe y francés..."):
|
| 40 |
+
translated_en = generator(technical_text, do_sample=True, min_length=50, max_length=100)[0]['generated_text'] # Traducción a inglés
|
| 41 |
+
translated_fr = translator_en_fr(technical_text)[0]['translation_text'] # Traducción a francés
|
| 42 |
+
translated_ar = translator_en_ar(technical_text)[0]['translation_text'] # Traducción a árabe
|
| 43 |
+
|
| 44 |
+
# Identificar el tema principal (simulando con generación de texto)
|
| 45 |
+
with st.spinner("Identificando el tema principal..."):
|
| 46 |
+
main_topic = generator(f"Identifica el tema principal: {technical_text}", do_sample=False, max_length=50)[0]['generated_text']
|
| 47 |
|
| 48 |
+
# Identificar el tono (simulando también)
|
| 49 |
+
with st.spinner("Identificando el tono del texto..."):
|
| 50 |
+
tone = generator(f"¿Cuál es el tono de este texto?: {technical_text}", do_sample=False, max_length=50)[0]['generated_text']
|
| 51 |
+
|
| 52 |
+
# Inferir conclusiones del experimento para un niño de 10 años
|
| 53 |
+
with st.spinner("Generando conclusiones accesibles..."):
|
| 54 |
+
conclusion = generator(f"Explica esto a un niño de 10 años: {experiment_text}", do_sample=True, min_length=50, max_length=100)[0]['generated_text']
|
| 55 |
+
|
| 56 |
+
# Formatear respuestas para enviar al web service
|
| 57 |
+
response = {
|
| 58 |
+
"rewritten_text": rewritten_text,
|
| 59 |
+
"translations": {
|
| 60 |
+
"english": translated_en,
|
| 61 |
+
"french": translated_fr,
|
| 62 |
+
"arabic": translated_ar
|
| 63 |
+
},
|
| 64 |
+
"main_topic": main_topic,
|
| 65 |
+
"tone": tone,
|
| 66 |
+
"conclusions": conclusion
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# Mostrar los resultados
|
| 70 |
+
st.subheader("Texto reescrito:")
|
| 71 |
+
st.write(rewritten_text)
|
| 72 |
+
|
| 73 |
+
st.subheader("Traducciones:")
|
| 74 |
+
st.write("Inglés: ", translated_en)
|
| 75 |
+
st.write("Francés: ", translated_fr)
|
| 76 |
+
st.write("Árabe: ", translated_ar)
|
| 77 |
+
|
| 78 |
+
st.subheader("Tema principal:")
|
| 79 |
+
st.write(main_topic)
|
| 80 |
+
|
| 81 |
+
st.subheader("Tono:")
|
| 82 |
+
st.write(tone)
|
| 83 |
+
|
| 84 |
+
st.subheader("Conclusiones explicadas a un niño de 10 años:")
|
| 85 |
+
st.write(conclusion)
|
| 86 |
+
|
| 87 |
+
st.subheader("Formato para enviar al web service:")
|
| 88 |
+
st.json(response)
|
| 89 |
+
|
| 90 |
else:
|
| 91 |
+
st.error("Por favor, introduce ambos textos antes de generar las respuestas.")
|