import gradio as gr import random def generate_poem(topic): # A list of possible rhymes for each line 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"] ] # A list of possible adjectives for each line 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"] ] # A list of possible nouns for each line 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"] ] # A list of possible verbs for each line 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"] ] # A list of possible prepositions for each line 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"] ] # A list of possible connectors for each line 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"] ] # A template for each line of the poem 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}" ] # Choose a random rhyme scheme for the poem rhyme_scheme = random.choice(rhymes) # Generate the poem line by line poem = "" for i in range(5): # Choose a random word for each placeholder in the template 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] # Replace the placeholders with the chosen words line = template[i].format(verb=verb, adjective=adjective, noun=noun, preposition=preposition, connector=connector, rhyme=rhyme) # Capitalize the first letter of the line line = line[0].upper() + line[1:] # Add the line to the poem poem += line # Add a comma or a period at the end of the line if i < 4: poem += "," else: poem += "." # Add a newline character after the line poem += "\n" # Yield the poem so far yield poem # Create a text streaming mode interface with gradio demo = gr.Interface( generate_poem, # The function to run gr.Textbox("Enter a topic for the poem"), # The input component gr.Textbox(streaming=True), # The output component title="Poem Generator", # The title of the interface 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." # The description of the interface ) # Launch the interface demo.launch()