Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from wordcloud import WordCloud | |
| import matplotlib.pyplot as plt | |
| from PIL import Image | |
| import io | |
| def generate_wordcloud(text): | |
| # Create a word cloud | |
| wordcloud = WordCloud(width=800, height=800, background_color='white', min_font_size=10).generate(text) | |
| # Convert word cloud to an image using BytesIO to handle image in memory | |
| plt.figure(figsize=(8, 8), facecolor=None) | |
| plt.imshow(wordcloud) | |
| plt.axis("off") | |
| plt.tight_layout(pad=0) | |
| buf = io.BytesIO() | |
| plt.savefig(buf, format='png') | |
| plt.close() | |
| buf.seek(0) | |
| image = Image.open(buf) | |
| return image | |
| # Create a Gradio interface | |
| # interface = gr.Interface(fn=generate_wordcloud, | |
| # inputs="text", | |
| # outputs="image") | |
| # Create a Gradio interface | |
| interface = gr.Interface( | |
| fn=generate_wordcloud, | |
| inputs=gr.Textbox(placeholder="Paste your text here", label="Your Text"), # Use gr.Textbox directly | |
| outputs=gr.Image(type="pil"), # Use gr.Image for more explicit output definition | |
| title="Word Cloud Generator", | |
| description="Enter your text in the textbox and generate a word cloud." | |
| ) | |
| interface.launch() | |