File size: 1,427 Bytes
c75ee7e 90bdced c75ee7e 90bdced c75ee7e 2b0f531 c75ee7e 90bdced c75ee7e 90bdced c75ee7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import streamlit as st
import requests
import time
import validators
import pandas as pd
MAX_RETRIES = 3
def is_valid_youtube_url(url):
# проверяем, является ли URL допустимым URL YouTube
return validators.url(url) and ("youtube.com" in url or "youtu.be" in url)
def make_request_put(url, query):
retries = 0
while retries < MAX_RETRIES:
try:
response = requests.post(url, data={"url_video": query})
response.raise_for_status() # генерировать исключение, если ответ содержит ошибку
return response.json()
except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e:
time.sleep(10) # подождать перед повторной попыткой
retries += 1
st.error("error")
return None
def color_negative_red(val):
color = 'red' if val == 'NEGATIVE' else 'grey' if val == 'NEUTRAL' else 'green'
return f'color: {color}'
url = st.text_input("YouTube url")
clicked = st.button("Sentiment Analysis")
if clicked and is_valid_youtube_url(url):
data = make_request_put("https://macrdel-sentiment-summarize-youtube-comms.hf.space/comments", url)
if data:
data = pd.DataFrame(data)
st.write(data.style.applymap(color_negative_red, subset=['sentiment']))
else:
st.error("error url") |