Emil25 commited on
Commit
d910a94
1 Parent(s): 9fc9c4e

Upload Final_project.py

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