fixes_v5
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
-
import language_tool_python
|
2 |
-
import openai
|
3 |
import streamlit as st
|
|
|
4 |
import subprocess
|
5 |
-
from pathlib import Path
|
6 |
import html
|
|
|
7 |
|
8 |
is_java_installed = False
|
9 |
|
@@ -19,7 +18,7 @@ def install_java():
|
|
19 |
|
20 |
@st.cache_resource
|
21 |
def load_assets():
|
22 |
-
openai.api_key = 'sk-proj-
|
23 |
install_java()
|
24 |
tool = language_tool_python.LanguageTool('ru-RU', language_tool_download_version="6.1")
|
25 |
return tool
|
@@ -51,36 +50,48 @@ def main():
|
|
51 |
.error-highlight {
|
52 |
background-color: #ffe066;
|
53 |
border-bottom: 2px dotted #ff9900;
|
|
|
54 |
color: black !important;
|
55 |
}
|
|
|
|
|
|
|
56 |
</style>
|
57 |
""", unsafe_allow_html=True)
|
58 |
|
59 |
st.title('Проверка орфографии')
|
60 |
|
61 |
-
|
|
|
|
|
|
|
62 |
text = text_input.replace("\n", " ")
|
63 |
tool = load_assets()
|
64 |
|
65 |
if st.button('Проверить текст'):
|
|
|
66 |
if not text.strip():
|
67 |
st.warning("Введите текст для проверки")
|
68 |
else:
|
69 |
errors = check_text(text, tool)
|
70 |
-
st.session_state.errors =
|
71 |
-
st.session_state.
|
|
|
72 |
|
|
|
73 |
errors = st.session_state.get('errors', [])
|
74 |
-
|
|
|
75 |
if errors:
|
76 |
-
#
|
77 |
highlighted = []
|
78 |
last_pos = 0
|
79 |
|
80 |
-
for error in
|
81 |
highlighted.append(html.escape(text[last_pos:error['start']]))
|
82 |
highlighted.append(
|
83 |
-
f'<span class="error-highlight"
|
|
|
84 |
f'{html.escape(text[error["start"]:error["end"]])}'
|
85 |
f'</span>'
|
86 |
)
|
@@ -88,30 +99,55 @@ def main():
|
|
88 |
|
89 |
highlighted.append(html.escape(text[last_pos:]))
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
st.markdown("### Результат проверки:")
|
92 |
-
st.markdown(
|
93 |
-
f'<div style="background: white; padding: 20px; border-radius: 8px; color: black;">'
|
94 |
-
f'{"".join(highlighted)}'
|
95 |
-
f'</div>',
|
96 |
-
unsafe_allow_html=True
|
97 |
-
)
|
98 |
|
99 |
-
#
|
100 |
-
st.markdown("
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
else:
|
114 |
-
st.
|
|
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
-
main()
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import language_tool_python
|
3 |
import subprocess
|
|
|
4 |
import html
|
5 |
+
import openai
|
6 |
|
7 |
is_java_installed = False
|
8 |
|
|
|
18 |
|
19 |
@st.cache_resource
|
20 |
def load_assets():
|
21 |
+
openai.api_key = 'sk-proj-WY9cBkzPHS9iZq_PruWf9_t1D3BlbkFJaNjZSiuy2VBtUX6zzR6dmauyN1OB5vCrxwHv0dLmDl6bXQt5JlbyzDW7qBa7PZM-GLJpEqBqQA'
|
22 |
install_java()
|
23 |
tool = language_tool_python.LanguageTool('ru-RU', language_tool_download_version="6.1")
|
24 |
return tool
|
|
|
50 |
.error-highlight {
|
51 |
background-color: #ffe066;
|
52 |
border-bottom: 2px dotted #ff9900;
|
53 |
+
cursor: pointer;
|
54 |
color: black !important;
|
55 |
}
|
56 |
+
.error-highlight:hover {
|
57 |
+
background-color: #ffd700;
|
58 |
+
}
|
59 |
</style>
|
60 |
""", unsafe_allow_html=True)
|
61 |
|
62 |
st.title('Проверка орфографии')
|
63 |
|
64 |
+
# Сохраняем текст в session_state
|
65 |
+
text_input = st.text_area("Введите текст для проверки:",
|
66 |
+
value=st.session_state.get('text_to_check', ''),
|
67 |
+
height=200)
|
68 |
text = text_input.replace("\n", " ")
|
69 |
tool = load_assets()
|
70 |
|
71 |
if st.button('Проверить текст'):
|
72 |
+
st.session_state.text_to_check = text_input
|
73 |
if not text.strip():
|
74 |
st.warning("Введите текст для проверки")
|
75 |
else:
|
76 |
errors = check_text(text, tool)
|
77 |
+
st.session_state.errors = errors
|
78 |
+
st.session_state.sorted_errors = sorted(errors, key=lambda x: x['start'])
|
79 |
+
st.session_state.selected_error = None # Сброс выбранной ошибки
|
80 |
|
81 |
+
# Используем сохраненные ошибки при наличии
|
82 |
errors = st.session_state.get('errors', [])
|
83 |
+
sorted_errors = st.session_state.get('sorted_errors', [])
|
84 |
+
|
85 |
if errors:
|
86 |
+
# Генерация HTML с кликабельными ошибками
|
87 |
highlighted = []
|
88 |
last_pos = 0
|
89 |
|
90 |
+
for index, error in enumerate(sorted_errors):
|
91 |
highlighted.append(html.escape(text[last_pos:error['start']]))
|
92 |
highlighted.append(
|
93 |
+
f'<span class="error-highlight" onclick="window.parent.postMessage({{index: {index}}}, \'*\')" '
|
94 |
+
f'title="Кликните для подробностей">'
|
95 |
f'{html.escape(text[error["start"]:error["end"]])}'
|
96 |
f'</span>'
|
97 |
)
|
|
|
99 |
|
100 |
highlighted.append(html.escape(text[last_pos:]))
|
101 |
|
102 |
+
html_content = f"""
|
103 |
+
<div style="
|
104 |
+
background: white;
|
105 |
+
padding: 20px;
|
106 |
+
border-radius: 8px;
|
107 |
+
border: 1px solid #ddd;
|
108 |
+
white-space: pre-wrap;
|
109 |
+
font-family: monospace;
|
110 |
+
color: #000000;
|
111 |
+
">
|
112 |
+
{''.join(highlighted)}
|
113 |
+
</div>
|
114 |
+
"""
|
115 |
+
|
116 |
st.markdown("### Результат проверки:")
|
117 |
+
st.markdown(html_content, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
# JavaScript для обработки кликов
|
120 |
+
st.markdown("""
|
121 |
+
<script>
|
122 |
+
window.addEventListener('message', function(event) {
|
123 |
+
if (event.data.index !== undefined) {
|
124 |
+
const index = event.data.index;
|
125 |
+
// Отправляем индекс в Streamlit
|
126 |
+
const streamlitDoc = window.parent.document;
|
127 |
+
const iframe = streamlitDoc.querySelector('iframe[data-testid="stIFrame"]');
|
128 |
+
if (iframe) {
|
129 |
+
iframe.contentWindow.postMessage({type: 'streamlit:setComponentValue', value: index}, '*');
|
130 |
+
}
|
131 |
+
}
|
132 |
+
});
|
133 |
+
</script>
|
134 |
+
""", unsafe_allow_html=True)
|
135 |
|
136 |
+
# Создаем компонент для получения индекса ошибки
|
137 |
+
clicked_error = st.session_state.get('clicked_error', None)
|
138 |
+
|
139 |
+
# Обработка кликов через custom component
|
140 |
+
if clicked_error is not None:
|
141 |
+
st.session_state.selected_error = clicked_error
|
142 |
+
|
143 |
+
# Обработка выбранной ошибки
|
144 |
+
if 'selected_error' in st.session_state and st.session_state.selected_error is not None:
|
145 |
+
error = sorted_errors[st.session_state.selected_error]
|
146 |
+
st.markdown("### Описание ошибки:")
|
147 |
+
st.markdown(f"**Ошибка {st.session_state.selected_error + 1}**: {error['message']}")
|
148 |
else:
|
149 |
+
st.markdown("### Найденные ошибки:")
|
150 |
+
st.markdown("🔍 **Кликните на выделенный текст, чтобы увидеть описание ошибки**")
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
+
main()
|