word_cloud / app.py
szk1ck's picture
lfs
5a9e2db
raw
history blame
674 Bytes
from janome.tokenizer import Tokenizer
from wordcloud import WordCloud
import gradio as gr
from PIL import Image
def generate_cloud(text):
t = Tokenizer()
tokens = t.tokenize(text)
wc = WordCloud(width=480, height=320, regexp="[\w']+", font_path="./ipaexm00401/ipaexm.ttf")
nouns = [token.base_form for token in tokens if token.part_of_speech.startswith('名詞')]
nouns = (" ").join(nouns)
wc.generate(nouns)
wc.to_file("./wordcloud.png")
image = Image.open("./wordcloud.png")
return image
gr.Interface(
fn=generate_cloud,
inputs="text",
outputs=gr.Image(type="pil"),
title="WordCloud Demonstration"
).launch()