Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from wordcloud import WordCloud
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
def generate_wordcloud(text):
|
| 8 |
+
# Create a word cloud
|
| 9 |
+
wordcloud = WordCloud(width=800, height=800, background_color='white', min_font_size=10).generate(text)
|
| 10 |
+
|
| 11 |
+
# Convert word cloud to an image using BytesIO to handle image in memory
|
| 12 |
+
plt.figure(figsize=(8, 8), facecolor=None)
|
| 13 |
+
plt.imshow(wordcloud)
|
| 14 |
+
plt.axis("off")
|
| 15 |
+
plt.tight_layout(pad=0)
|
| 16 |
+
|
| 17 |
+
buf = io.BytesIO()
|
| 18 |
+
plt.savefig(buf, format='png')
|
| 19 |
+
plt.close()
|
| 20 |
+
buf.seek(0)
|
| 21 |
+
image = Image.open(buf)
|
| 22 |
+
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
# Create a Gradio interface
|
| 26 |
+
# interface = gr.Interface(fn=generate_wordcloud,
|
| 27 |
+
# inputs="text",
|
| 28 |
+
# outputs="image")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Create a Gradio interface
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=generate_wordcloud,
|
| 34 |
+
inputs=gr.Textbox(placeholder="Paste your text here", label="Your Text"), # Use gr.Textbox directly
|
| 35 |
+
outputs=gr.Image(type="pil"), # Use gr.Image for more explicit output definition
|
| 36 |
+
title="Word Cloud Generator",
|
| 37 |
+
description="Enter your text in the textbox and generate a word cloud."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
interface.launch()
|