yunuseduran commited on
Commit
1931174
·
verified ·
1 Parent(s): c7f4293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -20
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import nltk
2
  from nltk.corpus import stopwords
3
  from nltk.tokenize import word_tokenize
@@ -6,25 +7,27 @@ import stylecloud
6
  from PIL import Image
7
  import matplotlib.pyplot as plt
8
  import streamlit as st
9
- import tempfile
10
  import os
11
- os.environ['NLTK_DATA'] = '/path/to/nltk_data'
12
- from nltk.corpus import stopwords
13
 
14
  # NLTK kütüphanesinden gerekli bileşenleri indir
15
  nltk.download('stopwords')
16
  nltk.download('punkt')
17
 
18
- def preprocess_and_create_stylecloud(text, language, icon):
 
 
 
 
 
19
  # Dil için stopwords listesini yükle
20
- stop_words = set(stopwords.words(language))
21
 
22
  # Noktalama işaretlerini kaldır
23
  translator = str.maketrans('', '', string.punctuation)
24
  text = text.translate(translator)
25
 
26
  # Metni tokenlere ayır ve küçük harfe çevir
27
- tokens = word_tokenize(text.lower(), language=language)
28
 
29
  # Stopwords'ü filtrele
30
  filtered_tokens = [word for word in tokens if word not in stop_words]
@@ -33,11 +36,24 @@ def preprocess_and_create_stylecloud(text, language, icon):
33
  processed_text = ' '.join(filtered_tokens)
34
 
35
  # StyleCloud oluştur
36
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
37
- stylecloud.gen_stylecloud(text=processed_text,
38
- icon_name=icon,
39
- output_name=temp_file.name)
40
- return temp_file.name
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  st.title("WordCloud Creator")
43
 
@@ -76,12 +92,11 @@ if file is not None:
76
  icon = [icon for icon, label in icon_options if label == icon_selection][0]
77
 
78
  if st.button("Create"):
79
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
80
- output_file = preprocess_and_create_stylecloud(text, language, icon)
81
- st.download_button(
82
- label="Download WordCloud",
83
- data=open(output_file, "rb").read(),
84
- file_name="wordcloud.png",
85
- mime="image/png",
86
- )
87
- st.image(output_file, caption='WordCloud', use_column_width=True)
 
1
+ Gerekli kütüphaneleri içe aktar
2
  import nltk
3
  from nltk.corpus import stopwords
4
  from nltk.tokenize import word_tokenize
 
7
  from PIL import Image
8
  import matplotlib.pyplot as plt
9
  import streamlit as st
 
10
  import os
 
 
11
 
12
  # NLTK kütüphanesinden gerekli bileşenleri indir
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
+ # Metni dosyadan oku
19
+ with open(file_path, 'r', encoding='utf-8') as f:
20
+ text = f.read()
21
+
22
  # Dil için stopwords listesini yükle
23
+ stop_words = set(stopwords.words(lang))
24
 
25
  # Noktalama işaretlerini kaldır
26
  translator = str.maketrans('', '', string.punctuation)
27
  text = text.translate(translator)
28
 
29
  # Metni tokenlere ayır ve küçük harfe çevir
30
+ tokens = word_tokenize(text.lower(), language=lang)
31
 
32
  # Stopwords'ü filtrele
33
  filtered_tokens = [word for word in tokens if word not in stop_words]
 
36
  processed_text = ' '.join(filtered_tokens)
37
 
38
  # StyleCloud oluştur
39
+ stylecloud.gen_stylecloud(text=processed_text,
40
+ icon_name=icon_name,
41
+ output_name=output_name)
42
+ # Oluşturulan StyleCloud'u göster
43
+ im = Image.open(output_name)
44
+ plt.figure(figsize=(10, 10))
45
+ plt.imshow(im)
46
+ plt.axis('off') # Eksenleri gizle
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
 
 
92
  icon = [icon for icon, label in icon_options if label == icon_selection][0]
93
 
94
  if st.button("Create"):
95
+ output_file = create_stylecloud(text, language, icon)
96
+ st.markdown(f"### [Download WordCloud](./{output_file})")
97
+
98
+ image = Image.open(output_file)
99
+ st.image(image, caption='WordCloud', use_column_width=True)
100
+ # Ensure the file is deleted after display
101
+ if os.path.exists(output_file):
102
+ os.remove(output_file)