import gradio as gr import matplotlib.pyplot as plt from wordcloud import WordCloud import tempfile import os def generate_wordcloud(text, file): if file is not None: text = file.decode('utf-8') elif not text: return None wordcloud = WordCloud(width=1600, height=800, background_color='white', scale=2, min_font_size=10).generate(text) plt.figure(figsize=(20, 10), dpi=300) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp: plt.savefig(tmp.name, format='png', dpi=300, bbox_inches='tight') plt.close() return tmp.name iface = gr.Interface( fn=generate_wordcloud, inputs=[ gr.Textbox(label="Enter text"), gr.File(label="Or upload a text file", type="binary") ], outputs=gr.Image(type="filepath", label="Generated Word Cloud"), title="High-Quality Word Cloud Generator", description="Enter text or upload a .txt file to generate a high-quality word cloud." ) iface.launch()