Spaces:
Running
Running
Upload Summary.py
Browse files- pages/Summary.py +137 -0
pages/Summary.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import re
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import pandas as pd
|
7 |
+
import requests
|
8 |
+
from wordcloud import WordCloud
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
|
11 |
+
|
12 |
+
# Установка API URL и заголовков
|
13 |
+
API_URL_TRA = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ru"
|
14 |
+
API_URL_KEY = "https://api-inference.huggingface.co/models/ml6team/keyphrase-extraction-kbir-inspec"
|
15 |
+
API_URL_SUM = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
16 |
+
|
17 |
+
TOKEN = os.getenv('API_TOKEN')
|
18 |
+
headers = {"Authorization": TOKEN}
|
19 |
+
|
20 |
+
|
21 |
+
# Функция для получения ключевых слов
|
22 |
+
def get_key_words(payload):
|
23 |
+
response = requests.post(API_URL_KEY, headers=headers, json=payload)
|
24 |
+
body = response.json()
|
25 |
+
if 'error' in body:
|
26 |
+
if 'estimated_time' in body:
|
27 |
+
time.sleep(body['estimated_time'])
|
28 |
+
else:
|
29 |
+
print(body)
|
30 |
+
return
|
31 |
+
get_key_words(payload)
|
32 |
+
return body
|
33 |
+
|
34 |
+
# Функция для перевода слова
|
35 |
+
def translate_key_words(payload):
|
36 |
+
response = requests.post(API_URL_TRA, headers=headers, json=payload)
|
37 |
+
body = response.json()
|
38 |
+
if 'error' in body:
|
39 |
+
if 'estimated_time' in body:
|
40 |
+
time.sleep(body['estimated_time'])
|
41 |
+
else:
|
42 |
+
print(body)
|
43 |
+
return
|
44 |
+
translate_key_words(payload)
|
45 |
+
return body
|
46 |
+
|
47 |
+
# Функция для составления конспекта
|
48 |
+
def make_summary(payload):
|
49 |
+
response = requests.post(API_URL_SUM, headers=headers, json=payload)
|
50 |
+
body = response.json()
|
51 |
+
if 'error' in body:
|
52 |
+
if 'estimated_time' in body:
|
53 |
+
time.sleep(body['estimated_time'])
|
54 |
+
else:
|
55 |
+
print(body)
|
56 |
+
return
|
57 |
+
make_summary(payload)
|
58 |
+
return body
|
59 |
+
|
60 |
+
|
61 |
+
# Очищаем список слов
|
62 |
+
def clean_list(words_list):
|
63 |
+
cleaned_words_list = []
|
64 |
+
for word in words_list:
|
65 |
+
word = word.lower()
|
66 |
+
word = re.sub(r"[^а-яА-Яa-zA-Z\s]", "", word)
|
67 |
+
word = word.lstrip()
|
68 |
+
word = word.rstrip()
|
69 |
+
cleaned_words_list.append(word)
|
70 |
+
return cleaned_words_list
|
71 |
+
|
72 |
+
|
73 |
+
# Настраиваем заголовок и название страницы
|
74 |
+
st.set_page_config(layout="wide", page_title="Students' Personal Assistant")
|
75 |
+
st.markdown(' # :female-student: Персональный помощник для студентов')
|
76 |
+
|
77 |
+
st.divider()
|
78 |
+
st.markdown('# :blue_book: Конспект на английском языке')
|
79 |
+
|
80 |
+
col1, col2 = st.columns(2)
|
81 |
+
text_from_tarea = col1.text_area('Введите тект статьи на английском языке', key='t_area', height=500)
|
82 |
+
|
83 |
+
button_start = st.button('Обработать текст')
|
84 |
+
key_words_list = []
|
85 |
+
|
86 |
+
|
87 |
+
if button_start:
|
88 |
+
with st.spinner('Составляем конспект...'):
|
89 |
+
# Составляем конспект
|
90 |
+
summary_text = make_summary({"inputs": text_from_tarea})
|
91 |
+
col2.text_area('Конспект статьи', height=500, key='sum_area', value=summary_text[0]['summary_text'])
|
92 |
+
|
93 |
+
with st.spinner('Получаем ключевые слова...'):
|
94 |
+
# Извлекаем ключевые слова
|
95 |
+
kew_words = get_key_words({"inputs": text_from_tarea})
|
96 |
+
for key_word in kew_words:
|
97 |
+
key_words_list.append(key_word['word'].lower())
|
98 |
+
|
99 |
+
sorted_keywords = set(sorted(key_words_list))
|
100 |
+
sorted_keywords = clean_list(sorted_keywords)
|
101 |
+
|
102 |
+
with st.spinner('Переводим ключевые слова...'):
|
103 |
+
# Переводим ключевые слова
|
104 |
+
translated_words_dict = translate_key_words({"inputs": sorted_keywords})
|
105 |
+
translated_words_list = [word['translation_text'] for word in translated_words_dict]
|
106 |
+
|
107 |
+
# Создаем карточки
|
108 |
+
cleaned_words_list_ru = clean_list(translated_words_list)
|
109 |
+
cards_list = []
|
110 |
+
for item1, item2 in zip(sorted_keywords, cleaned_words_list_ru):
|
111 |
+
cards_list.append([item1, item2])
|
112 |
+
|
113 |
+
st.success('Готово')
|
114 |
+
|
115 |
+
with st.spinner('Создаем WordCloud...'):
|
116 |
+
# Выводим Word Cloud
|
117 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
118 |
+
words_str = ', '.join(sorted_keywords)
|
119 |
+
w = WordCloud(background_color="white", width=1600, height=800).generate(words_str)
|
120 |
+
plt.imshow(w, interpolation='bilinear')
|
121 |
+
plt.imshow(w)
|
122 |
+
plt.axis("off")
|
123 |
+
st.pyplot()
|
124 |
+
|
125 |
+
# Выводим карточки
|
126 |
+
st.markdown('# :bookmark_tabs: Карточки из ключевых слов')
|
127 |
+
col1, col2, col3 = st.columns(3)
|
128 |
+
columns = [col1, col2, col3]
|
129 |
+
for index, el in enumerate(cards_list):
|
130 |
+
with columns[(index + 1) % 3]:
|
131 |
+
with st.container(border=True):
|
132 |
+
col4, col5 = st.columns([0.1, 0.9])
|
133 |
+
with col4:
|
134 |
+
st.write("# :flower_playing_cards:")
|
135 |
+
with col5:
|
136 |
+
st.write(f'## :green[{el[0]}]')
|
137 |
+
st.write(f'### :blue[{el[1]}]')
|