Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +44 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,45 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
# ----------------------------
|
| 6 |
+
# Загрузка модели
|
| 7 |
+
# ----------------------------
|
| 8 |
+
model_name = "Waris01/google-t5-finetuning-text-summarization"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# ----------------------------
|
| 13 |
+
# Очистка текста
|
| 14 |
+
# ----------------------------
|
| 15 |
+
def clean_text(text):
|
| 16 |
+
text = re.sub(r'\s+', ' ', text)
|
| 17 |
+
text = re.sub(r'\[[0-9]+\]', '', text)
|
| 18 |
+
text = re.sub(r'http\S+', '', text)
|
| 19 |
+
return text.strip()
|
| 20 |
+
|
| 21 |
+
# ----------------------------
|
| 22 |
+
# Генерация суммаризации
|
| 23 |
+
# ----------------------------
|
| 24 |
+
def summarize(text):
|
| 25 |
+
cleaned = clean_text(text)
|
| 26 |
+
inputs = tokenizer("summarize: " + cleaned, return_tensors="pt", truncation=True, max_length=512)
|
| 27 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=150, num_beams=2, early_stopping=True)
|
| 28 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
# ----------------------------
|
| 31 |
+
# Интерфейс Streamlit
|
| 32 |
+
# ----------------------------
|
| 33 |
+
st.title("🧬 Scientific Article Summarizer")
|
| 34 |
+
st.write("Вставьте текст статьи и получите краткую аннотацию.")
|
| 35 |
+
|
| 36 |
+
input_text = st.text_area("Введите текст статьи:", height=250)
|
| 37 |
+
|
| 38 |
+
if st.button("Суммаризировать"):
|
| 39 |
+
if len(input_text.strip()) == 0:
|
| 40 |
+
st.error("Введите текст!")
|
| 41 |
+
else:
|
| 42 |
+
with st.spinner("Генерация суммаризации..."):
|
| 43 |
+
summary = summarize(input_text)
|
| 44 |
+
st.subheader("📘 Краткое содержание:")
|
| 45 |
+
st.write(summary)
|