|
import gradio as gr |
|
import random |
|
|
|
def generate_poem(topic): |
|
|
|
rhymes = [ |
|
["sky", "fly", "high", "eye", "why"], |
|
["moon", "soon", "tune", "spoon", "dune"], |
|
["star", "far", "car", "bar", "jar"], |
|
["light", "night", "bright", "sight", "fight"], |
|
["day", "way", "say", "play", "stay"] |
|
] |
|
|
|
adjectives = [ |
|
["blue", "clear", "vast", "deep", "bright"], |
|
["white", "full", "round", "shiny", "silver"], |
|
["sparkling", "distant", "shooting", "glowing", "twinkling"], |
|
["warm", "soft", "gentle", "calm", "sweet"], |
|
["sunny", "happy", "beautiful", "lovely", "wonderful"] |
|
] |
|
|
|
nouns = [ |
|
["sky", "heaven", "cloud", "bird", "plane"], |
|
["moon", "lune", "globe", "cheese", "dream"], |
|
["star", "fire", "light", "wish", "heart"], |
|
["light", "sun", "ray", "glow", "smile"], |
|
["day", "life", "joy", "love", "hope"] |
|
] |
|
|
|
verbs = [ |
|
["see", "feel", "touch", "reach", "dream"], |
|
["watch", "follow", "sing", "dance", "love"], |
|
["catch", "hold", "keep", "make", "share"], |
|
["shine", "burn", "glow", "spark", "blaze"], |
|
["live", "enjoy", "celebrate", "cherish", "embrace"] |
|
] |
|
|
|
prepositions = [ |
|
["in", "on", "under", "above", "over"], |
|
["by", "with", "from", "to", "for"], |
|
["of", "about", "around", "through", "across"], |
|
["like", "as", "than", "more", "less"], |
|
["at", "during", "since", "until", "after"] |
|
] |
|
|
|
connectors = [ |
|
["and", "but", "or", "nor", "yet"], |
|
["so", "for", "because", "although", "though"], |
|
["if", "then", "else", "when", "while"], |
|
["where", "how", "why", "what", "who"], |
|
["which", "that", "this", "these", "those"] |
|
] |
|
|
|
template = [ |
|
"I {verb} the {adjective} {noun} {preposition} the {rhyme}", |
|
"{connector}, I {verb} the {adjective} {noun} {preposition} the {rhyme}", |
|
"I {verb} the {adjective} {noun} {preposition} the {rhyme}", |
|
"{connector}, I {verb} the {adjective} {noun} {preposition} the {rhyme}", |
|
"I {verb} the {adjective} {noun} {preposition} the {rhyme}" |
|
] |
|
|
|
rhyme_scheme = random.choice(rhymes) |
|
|
|
poem = "" |
|
for i in range(5): |
|
|
|
verb = random.choice(verbs[i]) |
|
adjective = random.choice(adjectives[i]) |
|
noun = random.choice(nouns[i]) |
|
preposition = random.choice(prepositions[i]) |
|
connector = random.choice(connectors[i]) |
|
rhyme = rhyme_scheme[i] |
|
|
|
line = template[i].format(verb=verb, adjective=adjective, noun=noun, preposition=preposition, connector=connector, rhyme=rhyme) |
|
|
|
line = line[0].upper() + line[1:] |
|
|
|
poem += line |
|
|
|
if i < 4: |
|
poem += "," |
|
else: |
|
poem += "." |
|
|
|
poem += "\n" |
|
|
|
yield poem |
|
|
|
|
|
demo = gr.Interface( |
|
generate_poem, |
|
gr.Textbox("Enter a topic for the poem"), |
|
gr.Textbox(streaming=True), |
|
title="Poem Generator", |
|
description="This is a simple poem generator that creates a 5-line poem based on a topic. The poem has a random rhyme scheme and uses random words from a predefined list. Enter a topic and see the poem being generated in real time." |
|
) |
|
|
|
|
|
demo.launch() |
|
|