|
|
|
import streamlit as st |
|
import re |
|
import io |
|
from io import BytesIO |
|
import base64 |
|
import matplotlib.pyplot as plt |
|
import plotly.graph_objects as go |
|
import pandas as pd |
|
import numpy as np |
|
import time |
|
from datetime import datetime |
|
from streamlit_player import st_player |
|
from spacy import displacy |
|
import logging |
|
import random |
|
|
|
|
|
from ..utils.widget_utils import generate_unique_key |
|
|
|
from ..database.morphosintax_mongo_db import store_student_morphosyntax_result |
|
from ..database.chat_db import store_chat_history |
|
from ..database.morphosintaxis_export import export_user_interactions |
|
|
|
import logging |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
def display_semantic_analysis_interface(nlp_models, lang_code): |
|
|
|
t = translations[lang_code] |
|
|
|
st.header(t['title']) |
|
|
|
|
|
text_input = st.text_area( |
|
t['text_input_label'], |
|
height=150, |
|
placeholder=t['text_input_placeholder'], |
|
) |
|
|
|
|
|
uploaded_file = st.file_uploader(t['file_uploader'], type=['txt']) |
|
|
|
if st.button(t['analyze_button']): |
|
if text_input or uploaded_file is not None: |
|
if uploaded_file: |
|
text_content = uploaded_file.getvalue().decode('utf-8') |
|
else: |
|
text_content = text_input |
|
|
|
|
|
analysis_result = perform_semantic_analysis(text_content, nlp_models[lang_code], lang_code) |
|
|
|
|
|
st.session_state.semantic_result = analysis_result |
|
|
|
|
|
display_semantic_results(st.session_state.semantic_result, lang_code, t) |
|
|
|
|
|
if store_semantic_result(st.session_state.username, text_content, analysis_result): |
|
st.success(t['success_message']) |
|
else: |
|
st.error(t['error_message']) |
|
else: |
|
st.warning(t['warning_message']) |
|
|
|
elif 'semantic_result' in st.session_state: |
|
|
|
|
|
display_semantic_results(st.session_state.semantic_result, lang_code, t) |
|
|
|
else: |
|
st.info(t['initial_message']) |
|
|
|
def display_semantic_results(result, lang_code, t): |
|
if result is None: |
|
st.warning(t['no_results']) |
|
return |
|
|
|
|
|
with st.expander(t['key_concepts'], expanded=True): |
|
concept_text = " | ".join([f"{concept} ({frequency:.2f})" for concept, frequency in result['key_concepts']]) |
|
st.write(concept_text) |
|
|
|
|
|
with st.expander(t['conceptual_relations'], expanded=True): |
|
st.pyplot(result['relations_graph']) |
|
|