Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,16 @@ from docx import Document
|
|
| 6 |
from fpdf import FPDF
|
| 7 |
from langdetect import detect
|
| 8 |
|
| 9 |
-
#
|
| 10 |
summarizers = {
|
| 11 |
"en": pipeline("summarization", model="facebook/bart-large-cnn"),
|
| 12 |
"ru": pipeline("summarization", model="IlyaGusev/mbart_ru_sum_gazeta"),
|
| 13 |
"kz": pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum")
|
| 14 |
}
|
| 15 |
|
|
|
|
| 16 |
def read_file(file):
|
| 17 |
-
if file
|
| 18 |
return ""
|
| 19 |
filename = file.name.lower()
|
| 20 |
text = ""
|
|
@@ -30,45 +31,58 @@ def read_file(file):
|
|
| 30 |
else:
|
| 31 |
text = file.read().decode("utf-8", errors="ignore")
|
| 32 |
except Exception as e:
|
| 33 |
-
return f"Ошибка при чтении файла: {
|
| 34 |
return text.strip()
|
| 35 |
|
|
|
|
| 36 |
def detect_language(text):
|
| 37 |
try:
|
| 38 |
lang = detect(text)
|
| 39 |
if lang.startswith("ru"):
|
| 40 |
return "ru"
|
| 41 |
-
elif lang.startswith("kk") or
|
| 42 |
return "kz"
|
| 43 |
else:
|
| 44 |
return "en"
|
| 45 |
except:
|
| 46 |
return "en"
|
| 47 |
|
|
|
|
| 48 |
def summarize_text(text):
|
| 49 |
if not text or len(text) < 50:
|
| 50 |
-
return "⚠️ Недостаточно текста для
|
| 51 |
-
|
| 52 |
lang = detect_language(text)
|
| 53 |
model = summarizers.get(lang, summarizers["en"])
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
flags = {"ru": "🇷🇺 Русский", "en": "🇬🇧 English", "kz": "🇰🇿 Қазақ тілі"}
|
| 56 |
-
lang_label = flags.get(lang, "🌐 Unknown")
|
| 57 |
model_label = model.model.name_or_path if hasattr(model.model, "name_or_path") else "Custom"
|
| 58 |
-
|
| 59 |
chunk_size = 2500
|
| 60 |
overlap = 200
|
| 61 |
summaries = []
|
|
|
|
| 62 |
for i in range(0, len(text), chunk_size - overlap):
|
| 63 |
chunk = text[i:i + chunk_size]
|
| 64 |
try:
|
| 65 |
result = model(chunk, max_length=180, min_length=40, do_sample=False)
|
| 66 |
summaries.append(result[0]['summary_text'])
|
| 67 |
except Exception as e:
|
| 68 |
-
summaries.append(f"[Ошибка в части {len(summaries)+1}: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
def save_summary_as_docx(summary_text):
|
| 74 |
path = "summary.docx"
|
|
@@ -78,12 +92,6 @@ def save_summary_as_docx(summary_text):
|
|
| 78 |
doc.save(path)
|
| 79 |
return path
|
| 80 |
|
| 81 |
-
def save_summary_as_txt(summary_text):
|
| 82 |
-
path = "summary.txt"
|
| 83 |
-
with open(path, "w", encoding="utf-8") as f:
|
| 84 |
-
f.write(summary_text)
|
| 85 |
-
return path
|
| 86 |
-
|
| 87 |
def save_summary_as_pdf(summary_text):
|
| 88 |
path = "summary.pdf"
|
| 89 |
pdf = FPDF()
|
|
@@ -97,29 +105,36 @@ def save_summary_as_pdf(summary_text):
|
|
| 97 |
pdf.output(path)
|
| 98 |
return path
|
| 99 |
|
|
|
|
| 100 |
def summarize_file(file):
|
| 101 |
text = read_file(file)
|
| 102 |
if text.startswith("Ошибка"):
|
| 103 |
-
return text, "❌
|
| 104 |
-
|
|
|
|
| 105 |
txt_path = save_summary_as_txt(summary)
|
| 106 |
docx_path = save_summary_as_docx(summary)
|
| 107 |
pdf_path = save_summary_as_pdf(summary)
|
| 108 |
-
return summary, lang_label, model_label, txt_path, docx_path, pdf_path
|
| 109 |
|
|
|
|
|
|
|
|
|
|
| 110 |
demo = gr.Interface(
|
| 111 |
fn=summarize_file,
|
| 112 |
-
inputs=gr.File(label="📁 Загрузите документ (.pdf, .docx
|
| 113 |
outputs=[
|
| 114 |
gr.Textbox(label="🧾 Краткое резюме"),
|
| 115 |
gr.Textbox(label="🌍 Определённый язык"),
|
| 116 |
gr.Textbox(label="��� Используемая модель"),
|
|
|
|
|
|
|
|
|
|
| 117 |
gr.File(label="📄 Скачать TXT"),
|
| 118 |
gr.File(label="📘 Скачать DOCX"),
|
| 119 |
-
gr.File(label="📕 Скачать PDF")
|
| 120 |
],
|
| 121 |
-
title="🧠 Eroha Summarizer",
|
| 122 |
-
description="
|
| 123 |
)
|
| 124 |
|
| 125 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 6 |
from fpdf import FPDF
|
| 7 |
from langdetect import detect
|
| 8 |
|
| 9 |
+
# === Загрузка моделей один раз при старте ===
|
| 10 |
summarizers = {
|
| 11 |
"en": pipeline("summarization", model="facebook/bart-large-cnn"),
|
| 12 |
"ru": pipeline("summarization", model="IlyaGusev/mbart_ru_sum_gazeta"),
|
| 13 |
"kz": pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum")
|
| 14 |
}
|
| 15 |
|
| 16 |
+
# === Чтение файла ===
|
| 17 |
def read_file(file):
|
| 18 |
+
if not file:
|
| 19 |
return ""
|
| 20 |
filename = file.name.lower()
|
| 21 |
text = ""
|
|
|
|
| 31 |
else:
|
| 32 |
text = file.read().decode("utf-8", errors="ignore")
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"Ошибка при чтении файла: {e}"
|
| 35 |
return text.strip()
|
| 36 |
|
| 37 |
+
# === Определение языка ===
|
| 38 |
def detect_language(text):
|
| 39 |
try:
|
| 40 |
lang = detect(text)
|
| 41 |
if lang.startswith("ru"):
|
| 42 |
return "ru"
|
| 43 |
+
elif lang.startswith("kk") or any(x in text for x in "әіңғүұқөһ"):
|
| 44 |
return "kz"
|
| 45 |
else:
|
| 46 |
return "en"
|
| 47 |
except:
|
| 48 |
return "en"
|
| 49 |
|
| 50 |
+
# === Суммаризация текста ===
|
| 51 |
def summarize_text(text):
|
| 52 |
if not text or len(text) < 50:
|
| 53 |
+
return "⚠️ Недостаточно текста для анализа.", "❌", "❌", 0, 0, "❌"
|
| 54 |
+
|
| 55 |
lang = detect_language(text)
|
| 56 |
model = summarizers.get(lang, summarizers["en"])
|
| 57 |
+
flags = {"ru": "🇷🇺 Русский", "kz": "🇰🇿 Қазақ тілі", "en": "🇬🇧 English"}
|
| 58 |
+
lang_label = flags.get(lang, "🌍 Unknown")
|
| 59 |
|
|
|
|
|
|
|
| 60 |
model_label = model.model.name_or_path if hasattr(model.model, "name_or_path") else "Custom"
|
|
|
|
| 61 |
chunk_size = 2500
|
| 62 |
overlap = 200
|
| 63 |
summaries = []
|
| 64 |
+
|
| 65 |
for i in range(0, len(text), chunk_size - overlap):
|
| 66 |
chunk = text[i:i + chunk_size]
|
| 67 |
try:
|
| 68 |
result = model(chunk, max_length=180, min_length=40, do_sample=False)
|
| 69 |
summaries.append(result[0]['summary_text'])
|
| 70 |
except Exception as e:
|
| 71 |
+
summaries.append(f"[Ошибка в части {len(summaries)+1}: {e}]")
|
| 72 |
+
|
| 73 |
+
summary = "\n\n".join(summaries).strip()
|
| 74 |
+
src_len = len(text)
|
| 75 |
+
sum_len = len(summary)
|
| 76 |
+
compression = round(100 * (1 - sum_len / src_len), 1) if src_len > 0 else 0
|
| 77 |
+
|
| 78 |
+
return summary, lang_label, model_label, src_len, sum_len, f"{compression}%"
|
| 79 |
|
| 80 |
+
# === Сохранение файлов ===
|
| 81 |
+
def save_summary_as_txt(summary_text):
|
| 82 |
+
path = "summary.txt"
|
| 83 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 84 |
+
f.write(summary_text)
|
| 85 |
+
return path
|
| 86 |
|
| 87 |
def save_summary_as_docx(summary_text):
|
| 88 |
path = "summary.docx"
|
|
|
|
| 92 |
doc.save(path)
|
| 93 |
return path
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def save_summary_as_pdf(summary_text):
|
| 96 |
path = "summary.pdf"
|
| 97 |
pdf = FPDF()
|
|
|
|
| 105 |
pdf.output(path)
|
| 106 |
return path
|
| 107 |
|
| 108 |
+
# === Основная функция ===
|
| 109 |
def summarize_file(file):
|
| 110 |
text = read_file(file)
|
| 111 |
if text.startswith("Ошибка"):
|
| 112 |
+
return text, "❌", "❌", 0, 0, "❌", None, None, None
|
| 113 |
+
|
| 114 |
+
summary, lang_label, model_label, src_len, sum_len, compression = summarize_text(text)
|
| 115 |
txt_path = save_summary_as_txt(summary)
|
| 116 |
docx_path = save_summary_as_docx(summary)
|
| 117 |
pdf_path = save_summary_as_pdf(summary)
|
|
|
|
| 118 |
|
| 119 |
+
return summary, lang_label, model_label, src_len, sum_len, compression, txt_path, docx_path, pdf_path
|
| 120 |
+
|
| 121 |
+
# === Интерфейс ===
|
| 122 |
demo = gr.Interface(
|
| 123 |
fn=summarize_file,
|
| 124 |
+
inputs=gr.File(label="📁 Загрузите документ (.pdf, .docx, .txt)"),
|
| 125 |
outputs=[
|
| 126 |
gr.Textbox(label="🧾 Краткое резюме"),
|
| 127 |
gr.Textbox(label="🌍 Определённый язык"),
|
| 128 |
gr.Textbox(label="��� Используемая модель"),
|
| 129 |
+
gr.Number(label="📄 Длина исходного текста (символов)"),
|
| 130 |
+
gr.Number(label="📝 Длина резюме (символов)"),
|
| 131 |
+
gr.Textbox(label="📉 Степень сокращения"),
|
| 132 |
gr.File(label="📄 Скачать TXT"),
|
| 133 |
gr.File(label="📘 Скачать DOCX"),
|
| 134 |
+
gr.File(label="📕 Скачать PDF"),
|
| 135 |
],
|
| 136 |
+
title="🧠 Eroha Summarizer PRO",
|
| 137 |
+
description="⚡ Автоматически определяет язык (🇷🇺 / 🇬🇧 / 🇰🇿), создаёт краткое резюме и сохраняет в TXT, DOCX, PDF. Поддерживает кириллицу в PDF.",
|
| 138 |
)
|
| 139 |
|
| 140 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|