Spaces:
Sleeping
Sleeping
Upload Visualization.py
Browse files- pages/Visualization.py +113 -0
pages/Visualization.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
import streamlit as st
|
9 |
+
|
10 |
+
|
11 |
+
# API для генерации изображения
|
12 |
+
API_URL_IMG = "https://api-inference.huggingface.co" \
|
13 |
+
"/models/playgroundai/playground-v2-1024px-aesthetic"
|
14 |
+
API_URL_SPE = "https://api-inference.huggingface.co" \
|
15 |
+
"/models/facebook/mms-tts-eng"
|
16 |
+
|
17 |
+
TOKEN = os.getenv('API_TOKEN')
|
18 |
+
HEADERS = {"Authorization": TOKEN}
|
19 |
+
|
20 |
+
st.set_page_config(page_title="Student Assistant")
|
21 |
+
|
22 |
+
|
23 |
+
if 'clicked_button' not in st.session_state:
|
24 |
+
st.session_state.clicked_button = False
|
25 |
+
if 'generated_image' not in st.session_state:
|
26 |
+
st.session_state.generated_image = None
|
27 |
+
|
28 |
+
|
29 |
+
def click_button():
|
30 |
+
st.session_state.clicked_button = True
|
31 |
+
st.session_state.generated_image = None
|
32 |
+
|
33 |
+
|
34 |
+
def hugging_api_request(url, payload):
|
35 |
+
response = requests.post(url, headers=HEADERS, json=payload, timeout=120)
|
36 |
+
|
37 |
+
if response.status_code == 500:
|
38 |
+
st.exception(RuntimeError(f'{response} {url.split("/")[-1]}'
|
39 |
+
' is currently unavailable'))
|
40 |
+
return
|
41 |
+
try:
|
42 |
+
body = response.json()
|
43 |
+
except json.JSONDecodeError:
|
44 |
+
return response.content
|
45 |
+
|
46 |
+
if 'error' in body:
|
47 |
+
print(response.status_code, body)
|
48 |
+
if 'estimated_time' in body:
|
49 |
+
st.info('Модель загружается. Она будет доступна '
|
50 |
+
f'через {body["estimated_time"]} сек.')
|
51 |
+
time.sleep(body['estimated_time'])
|
52 |
+
else:
|
53 |
+
return
|
54 |
+
hugging_api_request(url, payload)
|
55 |
+
return body
|
56 |
+
|
57 |
+
|
58 |
+
# Функция генерации изображения
|
59 |
+
def generate_img(payload) -> io.BytesIO:
|
60 |
+
return hugging_api_request(API_URL_IMG, payload)
|
61 |
+
|
62 |
+
|
63 |
+
def generate_speech(payload) -> io.BytesIO:
|
64 |
+
return hugging_api_request(API_URL_SPE, payload)
|
65 |
+
|
66 |
+
|
67 |
+
st.markdown('# :female-student: Персональный помощник для студентов')
|
68 |
+
st.divider()
|
69 |
+
st.markdown("# :sparkles: Изучение английского языка"
|
70 |
+
" через визуальное восприятие")
|
71 |
+
|
72 |
+
image_idea = st.text_input('Предложите свою тему для генерации изображения',
|
73 |
+
value="Astronaut riding a horse")
|
74 |
+
image_gen_btn = st.button('Генерировать изображение', on_click=click_button)
|
75 |
+
if st.session_state.clicked_button:
|
76 |
+
if not st.session_state.generated_image:
|
77 |
+
with st.spinner('Идёт загрузка изображения...'):
|
78 |
+
image_bytes = generate_img({"inputs": image_idea})
|
79 |
+
image_raw = io.BytesIO(image_bytes)
|
80 |
+
st.success('Готово')
|
81 |
+
st.session_state.generated_image = image_raw
|
82 |
+
|
83 |
+
st.image(st.session_state.generated_image)
|
84 |
+
st.markdown('## Опишите фотографию на английском языке')
|
85 |
+
st.markdown('## План ответа поможет вам:')
|
86 |
+
st.markdown('+ the place;')
|
87 |
+
st.markdown('+ the action;')
|
88 |
+
st.markdown('+ the person’s appearance;')
|
89 |
+
st.markdown('+ whether you like the picture or not;')
|
90 |
+
st.markdown('+ why.')
|
91 |
+
st.markdown('Start with: “I’d like to describe this picture.'
|
92 |
+
' The picture shows …” ')
|
93 |
+
st.divider()
|
94 |
+
|
95 |
+
description_text = st.text_area(
|
96 |
+
'Описание фотографии',
|
97 |
+
key='t_area', height=250,
|
98 |
+
placeholder=(
|
99 |
+
'I’d like to describe this picture.'
|
100 |
+
' The picture shows …'))
|
101 |
+
|
102 |
+
tts_gen_btn = st.button('Произнести текст')
|
103 |
+
if tts_gen_btn and description_text:
|
104 |
+
with st.spinner('Идёт загрузка аудио...'):
|
105 |
+
audio_bytes = generate_speech({
|
106 |
+
"inputs": description_text
|
107 |
+
})
|
108 |
+
if isinstance(audio_bytes, bytes):
|
109 |
+
st.audio(audio_bytes, format='audio/ogg')
|
110 |
+
else:
|
111 |
+
st.warning('Что-то пошло не так, попробуйте еще раз.')
|
112 |
+
|
113 |
+
st.success('Готово')
|