Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,28 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 5 |
-
os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
|
| 6 |
-
|
| 7 |
import streamlit as st
|
| 8 |
import spacy
|
| 9 |
from spacy import displacy
|
| 10 |
import re
|
| 11 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
from modules.auth import register_user, authenticate_user, get_user_role
|
| 14 |
from modules.morpho_analysis import get_repeated_words_colors, highlight_repeated_words, POS_COLORS, POS_TRANSLATIONS
|
|
@@ -22,13 +36,12 @@ st.set_page_config(
|
|
| 22 |
)
|
| 23 |
|
| 24 |
@st.cache_resource
|
| 25 |
-
|
| 26 |
def load_chatbot_model():
|
| 27 |
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
| 28 |
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
| 29 |
return tokenizer, model
|
| 30 |
|
| 31 |
-
#
|
| 32 |
chatbot_tokenizer, chatbot_model = load_chatbot_model()
|
| 33 |
|
| 34 |
def get_chatbot_response(input_text):
|
|
@@ -44,6 +57,20 @@ def load_spacy_models():
|
|
| 44 |
'fr': spacy.load("fr_core_news_lg")
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def login_page():
|
| 48 |
st.title("Iniciar Sesión")
|
| 49 |
username = st.text_input("Usuario")
|
|
@@ -64,14 +91,11 @@ def register_page():
|
|
| 64 |
new_password = st.text_input("Nueva Contraseña", type='password')
|
| 65 |
role = st.selectbox("Rol", ["Estudiante", "Profesor"])
|
| 66 |
|
| 67 |
-
# Aquí puedes añadir campos adicionales según el rol si lo deseas
|
| 68 |
additional_info = {}
|
| 69 |
if role == "Estudiante":
|
| 70 |
-
|
| 71 |
-
pass
|
| 72 |
elif role == "Profesor":
|
| 73 |
-
|
| 74 |
-
pass
|
| 75 |
|
| 76 |
if st.button("Registrarse"):
|
| 77 |
if register_user(new_username, new_password, role, additional_info):
|
|
@@ -132,45 +156,45 @@ def main_app():
|
|
| 132 |
# Use translations
|
| 133 |
t = translations[lang_code]
|
| 134 |
|
| 135 |
-
#
|
| 136 |
col1, col2 = st.columns([1, 2])
|
| 137 |
|
| 138 |
with col1:
|
| 139 |
st.markdown(f"### Chat con AIdeaText")
|
| 140 |
|
| 141 |
-
#
|
| 142 |
if 'chat_history' not in st.session_state:
|
| 143 |
st.session_state.chat_history = []
|
| 144 |
|
| 145 |
-
#
|
| 146 |
for i, (role, text) in enumerate(st.session_state.chat_history):
|
| 147 |
if role == "user":
|
| 148 |
st.text_area(f"Tú:", value=text, height=50, key=f"user_message_{i}", disabled=True)
|
| 149 |
else:
|
| 150 |
st.text_area(f"AIdeaText:", value=text, height=50, key=f"bot_message_{i}", disabled=True)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
user_input = st.text_input("Escribe tu mensaje aquí:")
|
| 154 |
|
| 155 |
if st.button("Enviar"):
|
| 156 |
if user_input:
|
| 157 |
-
#
|
| 158 |
st.session_state.chat_history.append(("user", user_input))
|
| 159 |
|
| 160 |
-
#
|
| 161 |
response = get_chatbot_response(user_input)
|
| 162 |
|
| 163 |
-
#
|
| 164 |
st.session_state.chat_history.append(("bot", response))
|
| 165 |
|
| 166 |
-
#
|
| 167 |
st.experimental_rerun()
|
| 168 |
|
| 169 |
with col2:
|
| 170 |
st.markdown(f"### {t['title']}")
|
| 171 |
|
| 172 |
if st.session_state.role == "Estudiante":
|
| 173 |
-
#
|
| 174 |
if 'input_text' not in st.session_state:
|
| 175 |
st.session_state.input_text = ""
|
| 176 |
|
|
@@ -199,6 +223,7 @@ def main_app():
|
|
| 199 |
# Arc Diagram
|
| 200 |
with st.expander(t['arc_diagram'], expanded=True):
|
| 201 |
sentences = list(doc.sents)
|
|
|
|
| 202 |
for i, sent in enumerate(sentences):
|
| 203 |
st.subheader(f"{t['sentence']} {i+1}")
|
| 204 |
html = displacy.render(sent, style="dep", options={"distance": 100})
|
|
@@ -206,16 +231,26 @@ def main_app():
|
|
| 206 |
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
| 207 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
| 208 |
st.write(html, unsafe_allow_html=True)
|
|
|
|
| 209 |
|
| 210 |
# Network graph
|
| 211 |
with st.expander(t['network_diagram'], expanded=True):
|
| 212 |
fig = visualize_syntax(sentence_input, nlp_models[lang_code], lang_code)
|
| 213 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
elif st.session_state.role == "Profesor":
|
| 216 |
-
#
|
| 217 |
st.write("Bienvenido, profesor. Aquí podrás ver el progreso de tus estudiantes.")
|
| 218 |
-
#
|
| 219 |
|
| 220 |
def main():
|
| 221 |
if 'logged_in' not in st.session_state:
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import spacy
|
| 4 |
from spacy import displacy
|
| 5 |
import re
|
| 6 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 7 |
+
from azure.cosmos import CosmosClient
|
| 8 |
+
from azure.cosmos.exceptions import CosmosHttpResponseError
|
| 9 |
+
from pymongo import MongoClient
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# Azure Cosmos DB configuration
|
| 13 |
+
cosmos_endpoint = "YOUR_COSMOS_DB_ENDPOINT"
|
| 14 |
+
cosmos_key = "YOUR_COSMOS_DB_KEY"
|
| 15 |
+
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
| 16 |
+
|
| 17 |
+
# SQL API database for user management
|
| 18 |
+
user_database = cosmos_client.get_database_client("user_database")
|
| 19 |
+
user_container = user_database.get_container_client("users")
|
| 20 |
+
|
| 21 |
+
# MongoDB API configuration for text analysis results
|
| 22 |
+
mongo_connection_string = "YOUR_MONGODB_CONNECTION_STRING"
|
| 23 |
+
mongo_client = MongoClient(mongo_connection_string)
|
| 24 |
+
mongo_db = mongo_client['aideatext_db']
|
| 25 |
+
analysis_collection = mongo_db['text_analysis']
|
| 26 |
|
| 27 |
from modules.auth import register_user, authenticate_user, get_user_role
|
| 28 |
from modules.morpho_analysis import get_repeated_words_colors, highlight_repeated_words, POS_COLORS, POS_TRANSLATIONS
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
@st.cache_resource
|
|
|
|
| 39 |
def load_chatbot_model():
|
| 40 |
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
| 41 |
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
| 42 |
return tokenizer, model
|
| 43 |
|
| 44 |
+
# Load the chatbot model
|
| 45 |
chatbot_tokenizer, chatbot_model = load_chatbot_model()
|
| 46 |
|
| 47 |
def get_chatbot_response(input_text):
|
|
|
|
| 57 |
'fr': spacy.load("fr_core_news_lg")
|
| 58 |
}
|
| 59 |
|
| 60 |
+
def store_analysis_result(username, text, repeated_words, arc_diagram, network_diagram):
|
| 61 |
+
try:
|
| 62 |
+
analysis_collection.insert_one({
|
| 63 |
+
'username': username,
|
| 64 |
+
'text': text,
|
| 65 |
+
'repeated_words': repeated_words,
|
| 66 |
+
'arc_diagram': arc_diagram,
|
| 67 |
+
'network_diagram': network_diagram
|
| 68 |
+
})
|
| 69 |
+
return True
|
| 70 |
+
except Exception as e:
|
| 71 |
+
st.error(f"Error storing analysis result: {e}")
|
| 72 |
+
return False
|
| 73 |
+
|
| 74 |
def login_page():
|
| 75 |
st.title("Iniciar Sesión")
|
| 76 |
username = st.text_input("Usuario")
|
|
|
|
| 91 |
new_password = st.text_input("Nueva Contraseña", type='password')
|
| 92 |
role = st.selectbox("Rol", ["Estudiante", "Profesor"])
|
| 93 |
|
|
|
|
| 94 |
additional_info = {}
|
| 95 |
if role == "Estudiante":
|
| 96 |
+
additional_info['carrera'] = st.text_input("Carrera")
|
|
|
|
| 97 |
elif role == "Profesor":
|
| 98 |
+
additional_info['departamento'] = st.text_input("Departamento")
|
|
|
|
| 99 |
|
| 100 |
if st.button("Registrarse"):
|
| 101 |
if register_user(new_username, new_password, role, additional_info):
|
|
|
|
| 156 |
# Use translations
|
| 157 |
t = translations[lang_code]
|
| 158 |
|
| 159 |
+
# Create two columns: one for chat and one for analysis
|
| 160 |
col1, col2 = st.columns([1, 2])
|
| 161 |
|
| 162 |
with col1:
|
| 163 |
st.markdown(f"### Chat con AIdeaText")
|
| 164 |
|
| 165 |
+
# Initialize chat history if it doesn't exist
|
| 166 |
if 'chat_history' not in st.session_state:
|
| 167 |
st.session_state.chat_history = []
|
| 168 |
|
| 169 |
+
# Display chat history
|
| 170 |
for i, (role, text) in enumerate(st.session_state.chat_history):
|
| 171 |
if role == "user":
|
| 172 |
st.text_area(f"Tú:", value=text, height=50, key=f"user_message_{i}", disabled=True)
|
| 173 |
else:
|
| 174 |
st.text_area(f"AIdeaText:", value=text, height=50, key=f"bot_message_{i}", disabled=True)
|
| 175 |
|
| 176 |
+
# User input field
|
| 177 |
user_input = st.text_input("Escribe tu mensaje aquí:")
|
| 178 |
|
| 179 |
if st.button("Enviar"):
|
| 180 |
if user_input:
|
| 181 |
+
# Add user message to history
|
| 182 |
st.session_state.chat_history.append(("user", user_input))
|
| 183 |
|
| 184 |
+
# Get chatbot response
|
| 185 |
response = get_chatbot_response(user_input)
|
| 186 |
|
| 187 |
+
# Add chatbot response to history
|
| 188 |
st.session_state.chat_history.append(("bot", response))
|
| 189 |
|
| 190 |
+
# Clear input field
|
| 191 |
st.experimental_rerun()
|
| 192 |
|
| 193 |
with col2:
|
| 194 |
st.markdown(f"### {t['title']}")
|
| 195 |
|
| 196 |
if st.session_state.role == "Estudiante":
|
| 197 |
+
# Student interface code
|
| 198 |
if 'input_text' not in st.session_state:
|
| 199 |
st.session_state.input_text = ""
|
| 200 |
|
|
|
|
| 223 |
# Arc Diagram
|
| 224 |
with st.expander(t['arc_diagram'], expanded=True):
|
| 225 |
sentences = list(doc.sents)
|
| 226 |
+
arc_diagrams = []
|
| 227 |
for i, sent in enumerate(sentences):
|
| 228 |
st.subheader(f"{t['sentence']} {i+1}")
|
| 229 |
html = displacy.render(sent, style="dep", options={"distance": 100})
|
|
|
|
| 231 |
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
| 232 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
| 233 |
st.write(html, unsafe_allow_html=True)
|
| 234 |
+
arc_diagrams.append(html)
|
| 235 |
|
| 236 |
# Network graph
|
| 237 |
with st.expander(t['network_diagram'], expanded=True):
|
| 238 |
fig = visualize_syntax(sentence_input, nlp_models[lang_code], lang_code)
|
| 239 |
st.pyplot(fig)
|
| 240 |
+
|
| 241 |
+
# Store analysis results
|
| 242 |
+
store_analysis_result(
|
| 243 |
+
st.session_state.username,
|
| 244 |
+
sentence_input,
|
| 245 |
+
highlighted_text,
|
| 246 |
+
arc_diagrams,
|
| 247 |
+
fig
|
| 248 |
+
)
|
| 249 |
|
| 250 |
elif st.session_state.role == "Profesor":
|
| 251 |
+
# Teacher interface code
|
| 252 |
st.write("Bienvenido, profesor. Aquí podrás ver el progreso de tus estudiantes.")
|
| 253 |
+
# Add logic to display student progress
|
| 254 |
|
| 255 |
def main():
|
| 256 |
if 'logged_in' not in st.session_state:
|