Spaces:
Sleeping
Sleeping
Create app.py
#1
by
MK-316
- opened
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
interface.launch()
|