File size: 1,857 Bytes
558983d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76ff5c0
558983d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)