Emil25 commited on
Commit
b5d0459
1 Parent(s): 21de7a5

Upload Summary.py

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