import language_tool_python import openai import streamlit as st import subprocess from search_errors_logic import check_text import html import docx import mammoth from io import BytesIO is_java_installed = False def install_java(): global is_java_installed if is_java_installed: return try: # Устанавливаем OpenJDK 17 subprocess.run(["apt-get", "update"], check=True) subprocess.run(["apt-get", "install", "-y", "openjdk-17-jdk"], check=True) #st.success("Java 17 установлена!") 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") tool = None return tool def extract_text_from_docx(file): text = mammoth.convert_to_markdown(file).value for symbol in ".,!?()[]:;": text = text.replace(f"\{symbol}", symbol) return text def main(): st.markdown(""" """, unsafe_allow_html=True) st.title('Проверка орфографии') # Добавляем переключатель между режимами ввода input_mode = st.radio( "Выберите способ ввода текста:", ("Ввести текст вручную", "Загрузить Word документ"), horizontal=True ) text = "" if input_mode == "Загрузить Word документ": uploaded_file = st.file_uploader("Загрузите Word документ", type=['docx']) if uploaded_file is not None: try: text = extract_text_from_docx(uploaded_file) st.text_area("Текст из документа:", value=text, height=200, key="docx_text") except Exception as e: st.error(f"Ошибка при чтении файла: {e}") else: text = st.text_area("Введите текст для проверки:", height=200, key="manual_text") tool = load_assets() if st.button('Проверить текст'): if not text.strip(): st.warning("Введите текст для проверки") else: text, errors = check_text(text, tool, mode="chat_gpt", highlight_mode=True) #text = text.replace("\n", " ") if not errors: st.success("Ошибок не найдено.") else: sorted_errors = sorted(errors, key=lambda x: x['start']) errors_for_higlight = [sorted_errors[0].copy()] for error in sorted_errors[1:]: if errors_for_higlight[-1]["end"] >= error["start"]: errors_for_higlight[-1]["end"] = max(errors_for_higlight[-1]["end"], error["end"]) else: errors_for_higlight.append(error) highlighted = [] last_pos = 0 for error in errors_for_higlight: highlighted.append(html.escape(text[last_pos:error['start']])) highlighted.append( f'' f'{html.escape(text[error["start"]:error["end"]])}' f'' ) last_pos = error['end'] highlighted.append(html.escape(text[last_pos:])) html_content = f"""