|
import streamlit as st |
|
import requests |
|
import time |
|
import validators |
|
import pandas as pd |
|
|
|
MAX_RETRIES = 3 |
|
|
|
def is_valid_youtube_url(url): |
|
|
|
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") |