Update app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,24 @@ import spacy
|
|
| 8 |
from spacy import displacy
|
| 9 |
import re
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from modules.morpho_analysis import get_repeated_words_colors, highlight_repeated_words, POS_COLORS, POS_TRANSLATIONS
|
| 13 |
from modules.syntax_analysis import visualize_syntax
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def login_page():
|
| 18 |
st.title("Iniciar Sesión")
|
|
@@ -23,6 +36,7 @@ def login_page():
|
|
| 23 |
st.success(f"Bienvenido, {username}!")
|
| 24 |
st.session_state.logged_in = True
|
| 25 |
st.session_state.username = username
|
|
|
|
| 26 |
st.experimental_rerun()
|
| 27 |
else:
|
| 28 |
st.error("Usuario o contraseña incorrectos")
|
|
@@ -39,8 +53,91 @@ def register_page():
|
|
| 39 |
st.error("El usuario ya existe")
|
| 40 |
|
| 41 |
def main_app():
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def main():
|
| 46 |
if 'logged_in' not in st.session_state:
|
|
@@ -60,4 +157,4 @@ def main():
|
|
| 60 |
main_app()
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
main()
|
|
|
|
| 8 |
from spacy import displacy
|
| 9 |
import re
|
| 10 |
|
| 11 |
+
# Configure the page to use the full width
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="AIdeaText",
|
| 14 |
+
layout="wide",
|
| 15 |
+
page_icon="random"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
from modules.auth import register_user, authenticate_user, get_user_role
|
| 19 |
from modules.morpho_analysis import get_repeated_words_colors, highlight_repeated_words, POS_COLORS, POS_TRANSLATIONS
|
| 20 |
from modules.syntax_analysis import visualize_syntax
|
| 21 |
|
| 22 |
+
@st.cache_resource
|
| 23 |
+
def load_spacy_models():
|
| 24 |
+
return {
|
| 25 |
+
'es': spacy.load("es_core_news_lg"),
|
| 26 |
+
'en': spacy.load("en_core_web_lg"),
|
| 27 |
+
'fr': spacy.load("fr_core_news_lg")
|
| 28 |
+
}
|
| 29 |
|
| 30 |
def login_page():
|
| 31 |
st.title("Iniciar Sesión")
|
|
|
|
| 36 |
st.success(f"Bienvenido, {username}!")
|
| 37 |
st.session_state.logged_in = True
|
| 38 |
st.session_state.username = username
|
| 39 |
+
st.session_state.role = get_user_role(username)
|
| 40 |
st.experimental_rerun()
|
| 41 |
else:
|
| 42 |
st.error("Usuario o contraseña incorrectos")
|
|
|
|
| 53 |
st.error("El usuario ya existe")
|
| 54 |
|
| 55 |
def main_app():
|
| 56 |
+
# Load spaCy models
|
| 57 |
+
nlp_models = load_spacy_models()
|
| 58 |
+
|
| 59 |
+
# Language selection
|
| 60 |
+
languages = {
|
| 61 |
+
'Español': 'es',
|
| 62 |
+
'English': 'en',
|
| 63 |
+
'Français': 'fr'
|
| 64 |
+
}
|
| 65 |
+
selected_lang = st.sidebar.selectbox("Select Language / Seleccione el idioma / Choisissez la langue", list(languages.keys()))
|
| 66 |
+
lang_code = languages[selected_lang]
|
| 67 |
+
|
| 68 |
+
# Translations
|
| 69 |
+
translations = {
|
| 70 |
+
'es': {
|
| 71 |
+
'title': "AIdeaText - Análisis morfológico y sintáctico",
|
| 72 |
+
'input_label': "Ingrese un texto para analizar (máx. 5,000 palabras):",
|
| 73 |
+
'input_placeholder': "El objetivo de esta aplicación es que mejore sus habilidades de redacción...",
|
| 74 |
+
'analyze_button': "Analizar texto",
|
| 75 |
+
'repeated_words': "Palabras repetidas",
|
| 76 |
+
'legend': "Leyenda: Categorías gramaticales",
|
| 77 |
+
'arc_diagram': "Análisis sintáctico: Diagrama de arco",
|
| 78 |
+
'network_diagram': "Análisis sintáctico: Diagrama de red",
|
| 79 |
+
'sentence': "Oración"
|
| 80 |
+
},
|
| 81 |
+
'en': {
|
| 82 |
+
# ... (mantén las traducciones en inglés)
|
| 83 |
+
},
|
| 84 |
+
'fr': {
|
| 85 |
+
# ... (mantén las traducciones en francés)
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
# Use translations
|
| 90 |
+
t = translations[lang_code]
|
| 91 |
+
|
| 92 |
+
st.markdown(f"### {t['title']}")
|
| 93 |
+
|
| 94 |
+
if st.session_state.role == "Estudiante":
|
| 95 |
+
# Código para la interfaz del estudiante
|
| 96 |
+
if 'input_text' not in st.session_state:
|
| 97 |
+
st.session_state.input_text = ""
|
| 98 |
+
|
| 99 |
+
sentence_input = st.text_area(t['input_label'], height=150, placeholder=t['input_placeholder'], value=st.session_state.input_text)
|
| 100 |
+
st.session_state.input_text = sentence_input
|
| 101 |
+
|
| 102 |
+
if st.button(t['analyze_button']):
|
| 103 |
+
if sentence_input:
|
| 104 |
+
doc = nlp_models[lang_code](sentence_input)
|
| 105 |
+
|
| 106 |
+
# Highlighted Repeated Words
|
| 107 |
+
with st.expander(t['repeated_words'], expanded=True):
|
| 108 |
+
word_colors = get_repeated_words_colors(doc)
|
| 109 |
+
highlighted_text = highlight_repeated_words(doc, word_colors)
|
| 110 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
| 111 |
+
|
| 112 |
+
# Legend for grammatical categories
|
| 113 |
+
st.markdown(f"##### {t['legend']}")
|
| 114 |
+
legend_html = "<div style='display: flex; flex-wrap: wrap;'>"
|
| 115 |
+
for pos, color in POS_COLORS.items():
|
| 116 |
+
if pos in POS_TRANSLATIONS:
|
| 117 |
+
legend_html += f"<div style='margin-right: 10px;'><span style='background-color: {color}; padding: 2px 5px;'>{POS_TRANSLATIONS[pos]}</span></div>"
|
| 118 |
+
legend_html += "</div>"
|
| 119 |
+
st.markdown(legend_html, unsafe_allow_html=True)
|
| 120 |
+
|
| 121 |
+
# Arc Diagram
|
| 122 |
+
with st.expander(t['arc_diagram'], expanded=True):
|
| 123 |
+
sentences = list(doc.sents)
|
| 124 |
+
for i, sent in enumerate(sentences):
|
| 125 |
+
st.subheader(f"{t['sentence']} {i+1}")
|
| 126 |
+
html = displacy.render(sent, style="dep", options={"distance": 100})
|
| 127 |
+
html = html.replace('height="375"', 'height="200"')
|
| 128 |
+
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
| 129 |
+
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
| 130 |
+
st.write(html, unsafe_allow_html=True)
|
| 131 |
+
|
| 132 |
+
# Network graph
|
| 133 |
+
with st.expander(t['network_diagram'], expanded=True):
|
| 134 |
+
fig = visualize_syntax(sentence_input, nlp_models[lang_code], lang_code)
|
| 135 |
+
st.pyplot(fig)
|
| 136 |
+
|
| 137 |
+
elif st.session_state.role == "Profesor":
|
| 138 |
+
# Código para la interfaz del profesor
|
| 139 |
+
st.write("Bienvenido, profesor. Aquí podrás ver el progreso de tus estudiantes.")
|
| 140 |
+
# Añade aquí la lógica para mostrar el progreso de los estudiantes
|
| 141 |
|
| 142 |
def main():
|
| 143 |
if 'logged_in' not in st.session_state:
|
|
|
|
| 157 |
main_app()
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
+
main()
|