File size: 810 Bytes
0733a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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")

interface.launch()