Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -11
src/streamlit_app.py
CHANGED
|
@@ -3,14 +3,20 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
# ----------------------------
|
| 6 |
-
#
|
| 7 |
# ----------------------------
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ----------------------------
|
| 13 |
-
#
|
| 14 |
# ----------------------------
|
| 15 |
def clean_text(text):
|
| 16 |
text = re.sub(r'\s+', ' ', text)
|
|
@@ -19,25 +25,31 @@ def clean_text(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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 37 |
|
| 38 |
if st.button("Суммаризировать"):
|
| 39 |
-
if
|
| 40 |
-
st.error("Введите
|
| 41 |
else:
|
| 42 |
with st.spinner("Генерация суммаризации..."):
|
| 43 |
summary = summarize(input_text)
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
# ----------------------------
|
| 6 |
+
# Настройки модели
|
| 7 |
# ----------------------------
|
| 8 |
+
MODEL_NAME = "Waris01/google-t5-finetuning-text-summarization"
|
| 9 |
+
|
| 10 |
+
@st.cache_resource(show_spinner=False)
|
| 11 |
+
def load_model(model_name):
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 14 |
+
return tokenizer, model
|
| 15 |
+
|
| 16 |
+
tokenizer, model = load_model(MODEL_NAME)
|
| 17 |
|
| 18 |
# ----------------------------
|
| 19 |
+
# Функция очистки текста
|
| 20 |
# ----------------------------
|
| 21 |
def clean_text(text):
|
| 22 |
text = re.sub(r'\s+', ' ', text)
|
|
|
|
| 25 |
return text.strip()
|
| 26 |
|
| 27 |
# ----------------------------
|
| 28 |
+
# Функция суммаризации
|
| 29 |
# ----------------------------
|
| 30 |
def summarize(text):
|
| 31 |
cleaned = clean_text(text)
|
| 32 |
inputs = tokenizer("summarize: " + cleaned, return_tensors="pt", truncation=True, max_length=512)
|
| 33 |
+
summary_ids = model.generate(
|
| 34 |
+
inputs["input_ids"],
|
| 35 |
+
max_length=150,
|
| 36 |
+
min_length=40,
|
| 37 |
+
num_beams=2,
|
| 38 |
+
early_stopping=True
|
| 39 |
+
)
|
| 40 |
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 41 |
|
| 42 |
# ----------------------------
|
| 43 |
# Интерфейс Streamlit
|
| 44 |
# ----------------------------
|
| 45 |
st.title("🧬 Scientific Article Summarizer")
|
| 46 |
+
st.write("Вставьте текст статьи, чтобы получить краткую аннотацию.")
|
| 47 |
|
| 48 |
+
input_text = st.text_area("Введите текст статьи:", height=300)
|
| 49 |
|
| 50 |
if st.button("Суммаризировать"):
|
| 51 |
+
if not input_text.strip():
|
| 52 |
+
st.error("Введите текст статьи!")
|
| 53 |
else:
|
| 54 |
with st.spinner("Генерация суммаризации..."):
|
| 55 |
summary = summarize(input_text)
|