Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,10 @@ from docx import Document
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
from langdetect import detect
|
| 9 |
import urllib.request
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
os.makedirs("/app/models", exist_ok=True)
|
| 13 |
FONT_PATH = "DejaVuSans.ttf"
|
| 14 |
|
|
@@ -19,6 +21,7 @@ if not os.path.exists(FONT_PATH):
|
|
| 19 |
FONT_PATH
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
# === ⚙️ Загрузка моделей ===
|
| 23 |
def load_model(task, model_name):
|
| 24 |
print(f"🔹 Загружается модель: {model_name}")
|
|
@@ -30,7 +33,8 @@ summarizers = {
|
|
| 30 |
"kz": load_model("summarization", "csebuetnlp/mT5_multilingual_XLSum")
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
def read_file(file):
|
| 35 |
if not file:
|
| 36 |
return ""
|
|
@@ -40,9 +44,8 @@ def read_file(file):
|
|
| 40 |
if filename.endswith(".pdf"):
|
| 41 |
with pdfplumber.open(file.name) as pdf:
|
| 42 |
for page in pdf.pages:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
text += page_text + "\n"
|
| 46 |
elif filename.endswith(".docx"):
|
| 47 |
text = docx2txt.process(file.name)
|
| 48 |
else:
|
|
@@ -51,6 +54,7 @@ def read_file(file):
|
|
| 51 |
return f"⚠️ Ошибка при чтении файла: {e}"
|
| 52 |
return text.strip()
|
| 53 |
|
|
|
|
| 54 |
# === 🌐 Определение языка ===
|
| 55 |
def detect_language(text):
|
| 56 |
try:
|
|
@@ -64,6 +68,7 @@ def detect_language(text):
|
|
| 64 |
except:
|
| 65 |
return "en"
|
| 66 |
|
|
|
|
| 67 |
# === 🧠 Суммаризация ===
|
| 68 |
def summarize_text(text):
|
| 69 |
if not text or len(text) < 50:
|
|
@@ -73,8 +78,8 @@ def summarize_text(text):
|
|
| 73 |
model = summarizers.get(lang, summarizers["en"])
|
| 74 |
flags = {"ru": "🇷🇺 Русский", "kz": "🇰🇿 Қазақ тілі", "en": "🇬🇧 English"}
|
| 75 |
lang_label = flags.get(lang, "🌍 Unknown")
|
| 76 |
-
model_label = model.model.name_or_path if hasattr(model.model, "name_or_path") else "Custom"
|
| 77 |
|
|
|
|
| 78 |
chunk_size = 2500
|
| 79 |
overlap = 200
|
| 80 |
summaries = []
|
|
@@ -94,7 +99,8 @@ def summarize_text(text):
|
|
| 94 |
|
| 95 |
return summary, lang_label, model_label, src_len, sum_len, f"{compression}%"
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
def save_summary_as_txt(summary_text):
|
| 99 |
path = "summary.txt"
|
| 100 |
with open(path, "w", encoding="utf-8") as f:
|
|
@@ -119,7 +125,8 @@ def save_summary_as_pdf(summary_text):
|
|
| 119 |
pdf.output(path)
|
| 120 |
return path
|
| 121 |
|
| 122 |
-
|
|
|
|
| 123 |
def summarize_file(file):
|
| 124 |
text = read_file(file)
|
| 125 |
if text.startswith("⚠️"):
|
|
@@ -136,33 +143,30 @@ def summarize_file(file):
|
|
| 136 |
|
| 137 |
return summary, lang_label, model_label, src_len, sum_len, compression, txt_path, docx_path, pdf_path
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
gr.Markdown("## 🧠 Eroha Summarizer PRO (автономная версия)")
|
| 142 |
gr.Markdown("🚀 Определяет язык (🇷🇺 / 🇰🇿 / 🇬🇧), создаёт краткое резюме и сохраняет в TXT, DOCX, PDF с поддержкой кириллицы.")
|
| 143 |
|
| 144 |
-
|
| 145 |
-
file_input = gr.File(label="📂 Загрузите документ (.pdf, .docx, .txt)")
|
| 146 |
-
|
| 147 |
-
with gr.Row():
|
| 148 |
-
summary_output = gr.Textbox(label="🧾 Краткое резюме", lines=10)
|
| 149 |
-
|
| 150 |
-
with gr.Row():
|
| 151 |
-
lang_output = gr.Textbox(label="🌍 Определённый язык")
|
| 152 |
-
model_output = gr.Textbox(label="🧠 Используемая модель")
|
| 153 |
-
|
| 154 |
-
with gr.Row():
|
| 155 |
-
src_len = gr.Number(label="📄 Длина исходного текста")
|
| 156 |
-
sum_len = gr.Number(label="📝 Длина резюме")
|
| 157 |
-
compression = gr.Textbox(label="📉 Степень сокращения")
|
| 158 |
-
|
| 159 |
-
with gr.Row():
|
| 160 |
-
txt_file = gr.File(label="📄 TXT файл")
|
| 161 |
-
docx_file = gr.File(label="📘 DOCX файл")
|
| 162 |
-
pdf_file = gr.File(label="📕 PDF файл")
|
| 163 |
-
|
| 164 |
run_btn = gr.Button("🔍 Сгенерировать резюме", variant="primary")
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
run_btn.click(
|
| 167 |
summarize_file,
|
| 168 |
inputs=[file_input],
|
|
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
from langdetect import detect
|
| 9 |
import urllib.request
|
| 10 |
+
import gradio.themes.base as base_theme
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# === 🗂️ Создание директорий и шрифта ===
|
| 14 |
os.makedirs("/app/models", exist_ok=True)
|
| 15 |
FONT_PATH = "DejaVuSans.ttf"
|
| 16 |
|
|
|
|
| 21 |
FONT_PATH
|
| 22 |
)
|
| 23 |
|
| 24 |
+
|
| 25 |
# === ⚙️ Загрузка моделей ===
|
| 26 |
def load_model(task, model_name):
|
| 27 |
print(f"🔹 Загружается модель: {model_name}")
|
|
|
|
| 33 |
"kz": load_model("summarization", "csebuetnlp/mT5_multilingual_XLSum")
|
| 34 |
}
|
| 35 |
|
| 36 |
+
|
| 37 |
+
# === 📄 Чтение документа ===
|
| 38 |
def read_file(file):
|
| 39 |
if not file:
|
| 40 |
return ""
|
|
|
|
| 44 |
if filename.endswith(".pdf"):
|
| 45 |
with pdfplumber.open(file.name) as pdf:
|
| 46 |
for page in pdf.pages:
|
| 47 |
+
if page.extract_text():
|
| 48 |
+
text += page.extract_text() + "\n"
|
|
|
|
| 49 |
elif filename.endswith(".docx"):
|
| 50 |
text = docx2txt.process(file.name)
|
| 51 |
else:
|
|
|
|
| 54 |
return f"⚠️ Ошибка при чтении файла: {e}"
|
| 55 |
return text.strip()
|
| 56 |
|
| 57 |
+
|
| 58 |
# === 🌐 Определение языка ===
|
| 59 |
def detect_language(text):
|
| 60 |
try:
|
|
|
|
| 68 |
except:
|
| 69 |
return "en"
|
| 70 |
|
| 71 |
+
|
| 72 |
# === 🧠 Суммаризация ===
|
| 73 |
def summarize_text(text):
|
| 74 |
if not text or len(text) < 50:
|
|
|
|
| 78 |
model = summarizers.get(lang, summarizers["en"])
|
| 79 |
flags = {"ru": "🇷🇺 Русский", "kz": "🇰🇿 Қазақ тілі", "en": "🇬🇧 English"}
|
| 80 |
lang_label = flags.get(lang, "🌍 Unknown")
|
|
|
|
| 81 |
|
| 82 |
+
model_label = model.model.name_or_path if hasattr(model.model, "name_or_path") else "Custom"
|
| 83 |
chunk_size = 2500
|
| 84 |
overlap = 200
|
| 85 |
summaries = []
|
|
|
|
| 99 |
|
| 100 |
return summary, lang_label, model_label, src_len, sum_len, f"{compression}%"
|
| 101 |
|
| 102 |
+
|
| 103 |
+
# === 💾 Сохранение ===
|
| 104 |
def save_summary_as_txt(summary_text):
|
| 105 |
path = "summary.txt"
|
| 106 |
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
| 125 |
pdf.output(path)
|
| 126 |
return path
|
| 127 |
|
| 128 |
+
|
| 129 |
+
# === 🚀 Главная функция ===
|
| 130 |
def summarize_file(file):
|
| 131 |
text = read_file(file)
|
| 132 |
if text.startswith("⚠️"):
|
|
|
|
| 143 |
|
| 144 |
return summary, lang_label, model_label, src_len, sum_len, compression, txt_path, docx_path, pdf_path
|
| 145 |
|
| 146 |
+
|
| 147 |
+
# === 🎨 Интерфейс (Gradio 4.44+, без css/theme аргументов) ===
|
| 148 |
+
theme = base_theme.Base().set(
|
| 149 |
+
primary_hue="violet",
|
| 150 |
+
neutral_hue="gray"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
with gr.Blocks(theme=theme) as demo:
|
| 154 |
gr.Markdown("## 🧠 Eroha Summarizer PRO (автономная версия)")
|
| 155 |
gr.Markdown("🚀 Определяет язык (🇷🇺 / 🇰🇿 / 🇬🇧), создаёт краткое резюме и сохраняет в TXT, DOCX, PDF с поддержкой кириллицы.")
|
| 156 |
|
| 157 |
+
file_input = gr.File(label="📂 Загрузите документ (.pdf, .docx, .txt)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
run_btn = gr.Button("🔍 Сгенерировать резюме", variant="primary")
|
| 159 |
|
| 160 |
+
summary_output = gr.Textbox(label="🧾 Краткое резюме", lines=10)
|
| 161 |
+
lang_output = gr.Textbox(label="🌍 Определённый язык")
|
| 162 |
+
model_output = gr.Textbox(label="���� Используемая модель")
|
| 163 |
+
src_len = gr.Number(label="📄 Длина исходного текста")
|
| 164 |
+
sum_len = gr.Number(label="📝 Длина резюме")
|
| 165 |
+
compression = gr.Textbox(label="📉 Степень сокращения")
|
| 166 |
+
txt_file = gr.File(label="📄 TXT файл")
|
| 167 |
+
docx_file = gr.File(label="📘 DOCX файл")
|
| 168 |
+
pdf_file = gr.File(label="📕 PDF файл")
|
| 169 |
+
|
| 170 |
run_btn.click(
|
| 171 |
summarize_file,
|
| 172 |
inputs=[file_input],
|