File size: 1,202 Bytes
e6a9813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b89a0
 
 
 
dfba500
 
34b89a0
 
dfba500
 
 
 
34b89a0
e6a9813
 
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
32
33
34
35
36
37
38
39
40
41
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()