aliicemill commited on
Commit
40fbfee
1 Parent(s): 40eeb61

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing Libraries
2
+ # author:@aliicemill
3
+ import nltk
4
+ from PIL import Image
5
+ import matplotlib.pyplot as plt
6
+ import streamlit as st
7
+ from nltk.corpus import stopwords
8
+ from nltk.tokenize import word_tokenize
9
+ import string
10
+ import stylecloud
11
+
12
+ # Downloading word clusters
13
+ nltk.download('stopwords')
14
+ nltk.download('punkt')
15
+
16
+ def preprocess_and_create_stylecloud(file_path, output_name='stylecloud.png',
17
+ icon_name='fas fa-laptop', lang='english'):
18
+ # read file
19
+ with open(file_path, 'r', encoding='utf-8') as f:
20
+ text = f.read()
21
+
22
+ # represent stopwords
23
+ stop_words = set(stopwords.words(lang))
24
+
25
+ # Remove punctuation marks
26
+ translator = str.maketrans('', '', string.punctuation)
27
+ text = text.translate(translator)
28
+
29
+ # Tokenize the text and convert to lowercase
30
+ tokens = word_tokenize(text.lower(), language=lang)
31
+
32
+ # Filter out stopwords
33
+ filtered_tokens = [word for word in tokens if word not in stop_words]
34
+
35
+ # Join filtered tokens
36
+ processed_text = ' '.join(filtered_tokens)
37
+
38
+ # Create StyleCloud
39
+ stylecloud.gen_stylecloud(text=processed_text,
40
+ icon_name=icon_name,
41
+ output_name=output_name)
42
+ # Show the generated StyleCloud
43
+ im = Image.open(output_name)
44
+ plt.figure(figsize=(10, 10))
45
+ plt.imshow(im)
46
+ plt.axis('off') # Hide axes
47
+ plt.show()
48
+
49
+ def create_stylecloud(text, language, icon):
50
+ output_file = "stylecloud.png"
51
+
52
+ stylecloud.gen_stylecloud(text=text,
53
+ icon_name=icon,
54
+ output_name=output_file)
55
+
56
+ return output_file
57
+
58
+ st.title("WordCloud Creator")
59
+
60
+ file = st.file_uploader("Import txt file", type=["txt"])
61
+
62
+ if file is not None:
63
+ text = file.getvalue().decode("utf-8")
64
+
65
+ language = st.radio("Language", ["tr", "en","fr","de"])
66
+
67
+ icon_options = ['fa-address-book', 'fa-address-card', 'fa-angry', 'fa-arrow-alt-circle-down', 'fa-arrow-alt-circle-left',
68
+ 'fa-arrow-alt-circle-right', 'fa-arrow-alt-circle-up', 'fa-bell', 'fa-bell-slash', 'fa-bookmark', 'fa-building',
69
+ 'fa-calendar', 'fa-calendar-alt', 'fa-calendar-check', 'fa-calendar-minus', 'fa-calendar-plus', 'fa-calendar-times',
70
+ 'fa-caret-square-down', 'fa-caret-square-left', 'fa-caret-square-right', 'fa-caret-square-up', 'fa-chart-bar',
71
+ 'fa-check-circle', 'fa-check-square', 'fa-circle', 'fa-clipboard', 'fa-clock', 'fa-clone', 'fa-closed-captioning',
72
+ 'fa-comment', 'fa-comment-alt', 'fa-comment-dots', 'fa-comments', 'fa-compass', 'fa-copy', 'fa-copyright', 'fa-credit-card',
73
+ 'fa-dizzy', 'fa-dot-circle', 'fa-edit', 'fa-envelope', 'fa-envelope-open', 'fa-eye', 'fa-eye-slash', 'fa-file',
74
+ 'fa-file-alt', 'fa-file-archive', 'fa-file-audio', 'fa-file-code', 'fa-file-excel', 'fa-file-image', 'fa-file-pdf',
75
+ 'fa-file-powerpoint', 'fa-file-video', 'fa-file-word', 'fa-flag', 'fa-flushed', 'fa-folder', 'fa-folder-open', 'fa-frown',
76
+ 'fa-frown-open', 'fa-futbol', 'fa-gem', 'fa-grimace', 'fa-grin', 'fa-grin-alt', 'fa-grin-beam', 'fa-grin-beam-sweat',
77
+ 'fa-grin-hearts', 'fa-grin-squint', 'fa-grin-squint-tears', 'fa-grin-stars', 'fa-grin-tears', 'fa-grin-tongue',
78
+ 'fa-grin-tongue-squint', 'fa-grin-tongue-wink', 'fa-grin-wink', 'fa-hand-lizard', 'fa-hand-paper', 'fa-hand-peace',
79
+ 'fa-hand-point-down', 'fa-hand-point-left', 'fa-hand-point-right', 'fa-hand-point-up', 'fa-hand-pointer', 'fa-hand-rock',
80
+ 'fa-hand-scissors', 'fa-hand-spock', 'fa-handshake', 'fa-hdd', 'fa-heart', 'fa-hospital', 'fa-hourglass', 'fa-id-badge',
81
+ 'fa-id-card', 'fa-image', 'fa-images', 'fa-keyboard', 'fa-kiss', 'fa-kiss-beam', 'fa-kiss-wink-heart', 'fa-laugh',
82
+ 'fa-laugh-beam', 'fa-laugh-squint', 'fa-laugh-wink', 'fa-lemon', 'fa-life-ring', 'fa-lightbulb', 'fa-list-alt',
83
+ 'fa-map', 'fa-meh', 'fa-meh-blank', 'fa-meh-rolling-eyes', 'fa-minus-square', 'fa-money-bill-alt', 'fa-moon',
84
+ 'fa-newspaper', 'fa-object-group', 'fa-object-ungroup', 'fa-paper-plane', 'fa-pause-circle', 'fa-play-circle',
85
+ 'fa-plus-square', 'fa-question-circle', 'fa-registered', 'fa-sad-cry', 'fa-sad-tear', 'fa-save', 'fa-share-square',
86
+ 'fa-smile', 'fa-smile-beam', 'fa-smile-wink', 'fa-snowflake', 'fa-square', 'fa-star', 'fa-star-half', 'fa-sticky-note',
87
+ 'fa-stop-circle', 'fa-sun', 'fa-surprise', 'fa-thumbs-down', 'fa-thumbs-up', 'fa-times-circle', 'fa-tired', 'fa-trash-alt',
88
+ 'fa-user', 'fa-user-circle', 'fa-window-close', 'fa-window-maximize', 'fa-window-minimize', 'fa-window-restore']
89
+ icon_options = list(map(lambda x : "fas "+x,icon_options))
90
+
91
+ icon = st.selectbox("Icon Selection", icon_options, index=1)
92
+
93
+ if st.button("Create"):
94
+ output_file = create_stylecloud(text, language, icon)
95
+ st.success(f"Here is your file: {output_file}")
96
+
97
+ image = Image.open(output_file)
98
+ st.image(image, caption='WordCloud', use_column_width=True)
99
+
100
+ with open(output_file, "rb") as file:
101
+ btn = st.download_button(
102
+ label="Download WordCloud",
103
+ data=file,
104
+ file_name=output_file,
105
+ mime="image/png"
106
+ )