sabirbagwan's picture
Upload 2 files
d06cae4
import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Streamlit app title and description
st.title("Word Cloud Generator")
st.write("Enter your words below and generate a word cloud!")
# User input
words = st.text_input("Enter your words (comma-separated)", "")
# Background color selection
background_color = st.selectbox("Select Background Color", ("dark", "light"))
# Generate word cloud
if st.button("Generate Word Cloud"):
# Split words by comma and remove leading/trailing whitespaces
word_list = [word.strip() for word in words.split(",")]
# Concatenate all words into a single string
text = " ".join(word_list)
# Set background color
if background_color == "dark":
bg_color = "black"
else:
bg_color = "white"
# Generate word cloud
wordcloud = WordCloud(width=800, height=400, background_color=bg_color).generate(text)
# Display word cloud using Matplotlib
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
st.pyplot(plt)
# Save word cloud image
if st.button("Save Image"):
image_filename = "word_cloud.png"
wordcloud.to_file(image_filename)
st.write(f"Word cloud image saved as {image_filename}!")
# Instructions for user
st.write("Enter words separated by commas and click 'Generate Word Cloud' to visualize the word cloud.")
st.write("Select the background color and click 'Generate Word Cloud' to change the background color.")
st.write("Click 'Save Image' to save the generated word cloud as an image file.")