|
import language_tool_python |
|
import openai |
|
import streamlit as st |
|
import subprocess |
|
from pathlib import Path |
|
import html |
|
|
|
is_java_installed = False |
|
|
|
def install_java(): |
|
global is_java_installed |
|
if is_java_installed: return |
|
try: |
|
subprocess.run(["apt-get", "update"], check=True) |
|
subprocess.run(["apt-get", "install", "-y", "openjdk-17-jdk"], check=True) |
|
is_java_installed = True |
|
except Exception as e: |
|
st.error(f"Ошибка установки Java: {e}") |
|
|
|
@st.cache_resource |
|
def load_assets(): |
|
openai.api_key = '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' |
|
install_java() |
|
tool = language_tool_python.LanguageTool('ru-RU', language_tool_download_version="6.1") |
|
return tool |
|
|
|
def generate_gpt_comment(message, context): |
|
response = openai.ChatCompletion.create( |
|
model="gpt-4o", |
|
messages=[ |
|
{"role": "user", "content": f"Прокомментируй ошибку: {message} в данном тексте '{context}'. Не больше одного предложения, на русском."} |
|
] |
|
) |
|
return response.choices[0].message['content'] |
|
|
|
def check_text(text, tool): |
|
matches = tool.check(text) |
|
errors = [] |
|
for match in matches: |
|
error_info = { |
|
'start': match.offset, |
|
'end': match.offset + match.errorLength, |
|
'message': generate_gpt_comment(match.message, match.context), |
|
} |
|
errors.append(error_info) |
|
return errors |
|
|
|
def main(): |
|
st.markdown(""" |
|
<style> |
|
.error-highlight { |
|
background-color: #ffe066; |
|
border-bottom: 2px dotted #ff9900; |
|
color: black !important; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
st.title('Проверка орфографии') |
|
|
|
text_input = st.text_area("Введите текст для проверки:", height=200) |
|
text = text_input.replace("\n", " ") |
|
tool = load_assets() |
|
|
|
if st.button('Проверить текст'): |
|
if not text.strip(): |
|
st.warning("Введите текст для проверки") |
|
else: |
|
errors = check_text(text, tool) |
|
st.session_state.errors = sorted(errors, key=lambda x: x['start']) |
|
st.session_state.show_all_errors = True |
|
|
|
errors = st.session_state.get('errors', []) |
|
|
|
if errors: |
|
|
|
highlighted = [] |
|
last_pos = 0 |
|
|
|
for error in errors: |
|
highlighted.append(html.escape(text[last_pos:error['start']])) |
|
highlighted.append( |
|
f'<span class="error-highlight" title="{html.escape(error["message"])}">' |
|
f'{html.escape(text[error["start"]:error["end"]])}' |
|
f'</span>' |
|
) |
|
last_pos = error['end'] |
|
|
|
highlighted.append(html.escape(text[last_pos:])) |
|
|
|
st.markdown("### Результат проверки:") |
|
st.markdown( |
|
f'<div style="background: white; padding: 20px; border-radius: 8px; color: black;">' |
|
f'{"".join(highlighted)}' |
|
f'</div>', |
|
unsafe_allow_html=True |
|
) |
|
|
|
|
|
st.markdown("### Выберите ошибку для подробного описания:") |
|
selected_error = st.radio( |
|
"Список ошибок:", |
|
options=[f"Ошибка {i+1} (позиция {e['start']}-{e['end']})" for i, e in enumerate(errors)], |
|
index=None, |
|
key="error_selector" |
|
) |
|
|
|
|
|
if selected_error: |
|
error_index = int(selected_error.split()[1]) - 1 |
|
error = errors[error_index] |
|
st.markdown(f"**Описание:** {error['message']}") |
|
else: |
|
st.info("Кликните на ошибку в списке выше, чтобы увидеть её описание.") |
|
|
|
if __name__ == "__main__": |
|
main() |