Spaces:
Sleeping
Sleeping
sbgonenc96
commited on
Commit
•
558983d
1
Parent(s):
943cc95
init commit
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import stylecloud
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
from utils import data_to_text
|
6 |
+
from reddit import RedditContent
|
7 |
+
|
8 |
+
|
9 |
+
def create_stylecloud(subreddit, icon):
|
10 |
+
## Get content
|
11 |
+
rdt_content = RedditContent(
|
12 |
+
thread_limit=10,
|
13 |
+
include_nsfw=False,
|
14 |
+
max_comment_length=2000,
|
15 |
+
min_comment_length=50,
|
16 |
+
min_comment_score=10,
|
17 |
+
)
|
18 |
+
rdt_content.process(search_query=subreddit, exact_search=True)
|
19 |
+
|
20 |
+
# Load the content
|
21 |
+
text = data_to_text(rdt_content.contents)
|
22 |
+
|
23 |
+
# Initiate params
|
24 |
+
output_file = 'stylecloud.png'
|
25 |
+
icon = icon_dict[icon]
|
26 |
+
|
27 |
+
# Generate the word cloud
|
28 |
+
stylecloud.gen_stylecloud(
|
29 |
+
text=text,
|
30 |
+
icon_name=icon,
|
31 |
+
size=500,
|
32 |
+
output_name=output_file,
|
33 |
+
)
|
34 |
+
|
35 |
+
# Load the generated image
|
36 |
+
image = Image.open(output_file)
|
37 |
+
image = image.resize((300, 300)) # Resize the image to 300x300 pixels
|
38 |
+
|
39 |
+
# Return the image
|
40 |
+
return image
|
41 |
+
|
42 |
+
|
43 |
+
icon_dict = {
|
44 |
+
"Car" : "fas fa-car",
|
45 |
+
"Star and Crescent" : "fas fa-star-and-crescent",
|
46 |
+
"Trophy" : "fas fa-trophy",
|
47 |
+
"Heart" : "fas fa-heart",
|
48 |
+
"Flag" : "fas fa-flag"
|
49 |
+
}
|
50 |
+
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown('Word Cloud for Reddit Comments')
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
subreddits = gr.Textbox(lines=1,
|
57 |
+
label="Write subreddit or search query here",
|
58 |
+
placeholder="Enter the subreddits you want to get content from")
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
icon = gr.Dropdown(choices=list(icon_dict.keys()),
|
62 |
+
label='Select Icon', value="Heart")
|
63 |
+
with gr.Row():
|
64 |
+
create_button = gr.Button('Submit')
|
65 |
+
output_image = gr.Image(label='Word Cloud')
|
66 |
+
|
67 |
+
create_button.click(
|
68 |
+
create_stylecloud,
|
69 |
+
inputs=[subreddits, icon],
|
70 |
+
outputs=[output_image]
|
71 |
+
)
|
72 |
+
|
73 |
+
demo.launch(share=True)
|