Spaces:
Running
Running
Upload Project.py
Browse files- pages/Project.py +109 -0
pages/Project.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_gen = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill"
|
11 |
+
API_URL_tra = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ru"
|
12 |
+
API_URL_key = "https://api-inference.huggingface.co/models/ml6team/keyphrase-extraction-kbir-inspec"
|
13 |
+
API_URL_sum = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
14 |
+
|
15 |
+
headers = {"Authorization": os.getenv("api_token")}
|
16 |
+
|
17 |
+
# Функция для генерирования предложения
|
18 |
+
def generate_example(payload):
|
19 |
+
response = requests.post(API_URL_gen, headers=headers, json=payload)
|
20 |
+
return response.json()
|
21 |
+
|
22 |
+
# Функция для получения ключевых слов
|
23 |
+
def get_key_words(payload):
|
24 |
+
response = requests.post(API_URL_key, headers=headers, json=payload)
|
25 |
+
return response.json()
|
26 |
+
|
27 |
+
# Функция для перевода слова
|
28 |
+
def translate_key_words(payload):
|
29 |
+
response = requests.post(API_URL_tra, headers=headers, json=payload)
|
30 |
+
return response.json()
|
31 |
+
|
32 |
+
# Функция для составления конспекта
|
33 |
+
def make_summary(payload):
|
34 |
+
response = requests.post(API_URL_sum, headers=headers, json=payload)
|
35 |
+
return response.json()
|
36 |
+
|
37 |
+
|
38 |
+
# Очищаем список слов
|
39 |
+
def clean_list(words_list):
|
40 |
+
cleaned_words_list = []
|
41 |
+
for word in words_list:
|
42 |
+
word = word.lower()
|
43 |
+
word =re.sub(r"[^а-яА-Яa-zA-Z\s]", "", word)
|
44 |
+
word = word.lstrip()
|
45 |
+
word = word.rstrip()
|
46 |
+
cleaned_words_list.append(word)
|
47 |
+
return cleaned_words_list
|
48 |
+
|
49 |
+
|
50 |
+
# Настраеваем заголовок и название страницы
|
51 |
+
st.set_page_config(layout="wide", page_title="Students' Personal Assistant")
|
52 |
+
st.markdown(' # :female-student: Персональный помощник для студентов')
|
53 |
+
|
54 |
+
st.divider()
|
55 |
+
st.markdown('## :flower_playing_cards: Как назвать?')
|
56 |
+
|
57 |
+
|
58 |
+
st.markdown('# :bookmark_tabs: :bookmark_tabs: :bookmark_tabs: :bookmark_tabs: ')
|
59 |
+
col1, col2 = st.columns(2)
|
60 |
+
text_from_tarea = col1.text_area('Введите тект статьи на английском языке', height=500)
|
61 |
+
|
62 |
+
|
63 |
+
button_start = st.button('Обработать текст')
|
64 |
+
|
65 |
+
key_words_list = []
|
66 |
+
if button_start:
|
67 |
+
|
68 |
+
with st.spinner('...'):
|
69 |
+
summary_text = make_summary({"inputs": text_from_tarea})
|
70 |
+
col2.text_area('Конспект статьи', height=500, value=summary_text[0]['summary_text'])
|
71 |
+
kew_words = get_key_words({ "inputs": text_from_tarea,})
|
72 |
+
for key_word in kew_words :
|
73 |
+
key_words_list.append(key_word['word'].lower())
|
74 |
+
|
75 |
+
sorted_keywords = set(sorted(key_words_list))
|
76 |
+
sorted_keywords = clean_list(sorted_keywords)
|
77 |
+
|
78 |
+
translated_words_list = []
|
79 |
+
for key_word in sorted_keywords:
|
80 |
+
res = translate_key_words({"inputs": key_word,})
|
81 |
+
translated_words_list.append(res[0]['translation_text'])
|
82 |
+
|
83 |
+
cleaned_words_list_ru = clean_list(translated_words_list)
|
84 |
+
|
85 |
+
cards_list = []
|
86 |
+
for item1, item2 in zip(sorted_keywords, cleaned_words_list_ru):
|
87 |
+
cards_list.append([item1, item2])
|
88 |
+
|
89 |
+
# Преобразуем полученные данные в DataFrame
|
90 |
+
#cards_df = pd.DataFrame(cards_list, columns=['word', 'translated', 'example'])
|
91 |
+
st.success('Готово')
|
92 |
+
|
93 |
+
# Выводим Word Cloud
|
94 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
95 |
+
words_str = ', '.join(sorted_keywords)
|
96 |
+
w = WordCloud(background_color="white").generate(words_str)
|
97 |
+
plt.imshow(w, interpolation='bilinear')
|
98 |
+
plt.imshow(w)
|
99 |
+
plt.axis("off")
|
100 |
+
st.pyplot()
|
101 |
+
|
102 |
+
# Выводим карточки
|
103 |
+
for el in cards_list:
|
104 |
+
with st.chat_message("assistant"):
|
105 |
+
#st.divider()
|
106 |
+
st.markdown('# :flower_playing_cards:')
|
107 |
+
st.markdown(f'# :green[{el[0]}]')
|
108 |
+
st.markdown(f'## :blue[{el[1]}]')
|
109 |
+
st.divider()
|