File size: 4,423 Bytes
cf72e88 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
from pathlib import Path
import random
import streamlit as st
import praw
HOME = Path(__name__).parent.absolute()
@st.cache_data
def create_instance(*args, **kwargs):
reddit = praw.Reddit(
client_id=kwargs.get('client_id'),
client_secret=kwargs.get('client_secret'),
user_agent=kwargs.get('user_agent'),
)
subreddit = get_subreddit(reddit=reddit, subreddit=kwargs.get(
'subreddit'), nsfw=kwargs.get('nsfw'))
submission = get_random_submission(subreddit=subreddit)
st.session_state['submission'] = submission
return True
def get_subreddit(*args, **kwargs):
reddit = kwargs.get('reddit')
subreddit = reddit.subreddit(kwargs.get('subreddit'))
nsfw = kwargs.get('nsfw')
try:
st.text(f"Subreddit: {subreddit.display_name}")
except Exception as exception:
st.exception(exception=exception)
if subreddit.over18 and not nsfw:
st.error(
body='subreddit has NSFW contents but you did not select to scrape them')
return subreddit
def get_random_submission(*args, **kwargs):
subreddit = kwargs.get('subreddit')
submissions = [submission for submission in subreddit.hot(limit=10)]
return random.choice(submissions)
# Streamlit Config
st.set_page_config(
page_title="Whisper-TikTok",
page_icon="π¬",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://github.com/MatteoFasulo/Whisper-TikTok',
'Report a bug': "https://github.com/MatteoFasulo/Whisper-TikTok/issues",
'About':
"""
# Whisper-TikTok
Whisper-TikTok is an innovative AI-powered tool that leverages the prowess of Edge TTS, OpenAI-Whisper, and FFMPEG to craft captivating TikTok videos also with a web application interface!
Mantainer: https://github.com/MatteoFasulo
If you find a bug or if you just have questions about the project feel free to reach me at https://github.com/MatteoFasulo/Whisper-TikTok
Any contribution to this project is welcome to improve the quality of work!
"""
}
)
st.page_link("app.py", label="Home", icon="π ")
st.page_link("https://github.com/MatteoFasulo/Whisper-TikTok",
label="GitHub", icon="π")
with st.sidebar:
with st.expander("βΉοΈ How to use"):
st.write(
"""
Before starting you will need to create a new [Reddit API App](https://www.reddit.com/prefs/apps) by selecting `script` (personal use).
Then, after putting the App name, http://localhost as `reddit uri` and `about url`, you have just to insert those values in this dashboard to use the Reddit API for scraping any subreddit.
""")
client_id = st.text_input(label='Reddit Client ID')
client_secret = st.text_input(
label='Reddit Client Secret', type='password')
user_agent = st.text_input(label='Reddit User Agent')
st.title("π Whisper-TikTok π")
st.subheader('Reddit section')
st.write("""
This section allows you to generate videos from subreddits.""")
st.divider()
LEFT, RIGHT = st.columns(2)
with LEFT:
num_videos = st.number_input(label='How many videos do you want to generate?',
min_value=1, max_value=10, value=1, step=1)
subreddit = st.text_input(
label='What Subreddit do you want to use', placeholder='AskReddit')
nsfw = st.checkbox(label='NSFW content?', value=False)
max_chars = st.slider(label='Maximum number of characters per line',
min_value=10, max_value=50, value=38, step=1)
max_words = st.number_input(label='Maximum number of words per line', min_value=1,
max_value=5, value=2, step=1)
result = st.button('Get subreddit')
with RIGHT:
if result:
create_instance(client_id=client_id, client_secret=client_secret,
user_agent=user_agent, subreddit=subreddit, nsfw=nsfw)
submission = st.session_state['submission']
title = submission.title
submission.comment_sort = "new"
top_level_comments = list(submission.comments)
max_comments = 10
st.subheader(title)
for comment in top_level_comments[:max_comments]:
st.text(comment.body)
st.divider()
|