Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import stylecloud | |
| from PIL import Image | |
| import os | |
| from utils import data_to_text | |
| from reddit import RedditContent | |
| def create_stylecloud(subreddit, icon): | |
| ## Get content | |
| rdt_content = RedditContent( | |
| thread_limit=10, | |
| include_nsfw=False, | |
| max_comment_length=2000, | |
| min_comment_length=50, | |
| min_comment_score=10, | |
| ) | |
| rdt_content.process(search_query=subreddit, exact_search=True) | |
| # Load the content | |
| text = data_to_text(rdt_content.contents) | |
| # Initiate params | |
| output_file = 'stylecloud.png' | |
| icon = icon_dict[icon] | |
| # Generate the word cloud | |
| stylecloud.gen_stylecloud( | |
| text=text, | |
| icon_name=icon, | |
| size=500, | |
| output_name=output_file, | |
| ) | |
| # Load the generated image | |
| image = Image.open(output_file) | |
| #image = image.resize((300, 300)) # Resize the image to 300x300 pixels | |
| # Return the image | |
| return image | |
| icon_dict = { | |
| "Car" : "fas fa-car", | |
| "Star and Crescent" : "fas fa-star-and-crescent", | |
| "Trophy" : "fas fa-trophy", | |
| "Heart" : "fas fa-heart", | |
| "Flag" : "fas fa-flag" | |
| } | |
| with gr.Blocks() as demo: | |
| gr.Markdown('Word Cloud for Reddit Comments') | |
| with gr.Row(): | |
| subreddits = gr.Textbox(lines=1, | |
| label="Write subreddit or search query here", | |
| placeholder="Enter the subreddits you want to get content from") | |
| with gr.Row(): | |
| icon = gr.Dropdown(choices=list(icon_dict.keys()), | |
| label='Select Icon', value="Heart") | |
| with gr.Row(): | |
| create_button = gr.Button('Submit') | |
| output_image = gr.Image(label='Word Cloud') | |
| create_button.click( | |
| create_stylecloud, | |
| inputs=[subreddits, icon], | |
| outputs=[output_image] | |
| ) | |
| demo.launch(share=True) |