Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,100 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
|
| 4 |
+
def generate_poem(topic):
|
| 5 |
+
# A list of possible rhymes for each line
|
| 6 |
+
rhymes = [
|
| 7 |
+
["sky", "fly", "high", "eye", "why"],
|
| 8 |
+
["moon", "soon", "tune", "spoon", "dune"],
|
| 9 |
+
["star", "far", "car", "bar", "jar"],
|
| 10 |
+
["light", "night", "bright", "sight", "fight"],
|
| 11 |
+
["day", "way", "say", "play", "stay"]
|
| 12 |
+
]
|
| 13 |
+
# A list of possible adjectives for each line
|
| 14 |
+
adjectives = [
|
| 15 |
+
["blue", "clear", "vast", "deep", "bright"],
|
| 16 |
+
["white", "full", "round", "shiny", "silver"],
|
| 17 |
+
["sparkling", "distant", "shooting", "glowing", "twinkling"],
|
| 18 |
+
["warm", "soft", "gentle", "calm", "sweet"],
|
| 19 |
+
["sunny", "happy", "beautiful", "lovely", "wonderful"]
|
| 20 |
+
]
|
| 21 |
+
# A list of possible nouns for each line
|
| 22 |
+
nouns = [
|
| 23 |
+
["sky", "heaven", "cloud", "bird", "plane"],
|
| 24 |
+
["moon", "lune", "globe", "cheese", "dream"],
|
| 25 |
+
["star", "fire", "light", "wish", "heart"],
|
| 26 |
+
["light", "sun", "ray", "glow", "smile"],
|
| 27 |
+
["day", "life", "joy", "love", "hope"]
|
| 28 |
+
]
|
| 29 |
+
# A list of possible verbs for each line
|
| 30 |
+
verbs = [
|
| 31 |
+
["see", "feel", "touch", "reach", "dream"],
|
| 32 |
+
["watch", "follow", "sing", "dance", "love"],
|
| 33 |
+
["catch", "hold", "keep", "make", "share"],
|
| 34 |
+
["shine", "burn", "glow", "spark", "blaze"],
|
| 35 |
+
["live", "enjoy", "celebrate", "cherish", "embrace"]
|
| 36 |
+
]
|
| 37 |
+
# A list of possible prepositions for each line
|
| 38 |
+
prepositions = [
|
| 39 |
+
["in", "on", "under", "above", "over"],
|
| 40 |
+
["by", "with", "from", "to", "for"],
|
| 41 |
+
["of", "about", "around", "through", "across"],
|
| 42 |
+
["like", "as", "than", "more", "less"],
|
| 43 |
+
["at", "during", "since", "until", "after"]
|
| 44 |
+
]
|
| 45 |
+
# A list of possible connectors for each line
|
| 46 |
+
connectors = [
|
| 47 |
+
["and", "but", "or", "nor", "yet"],
|
| 48 |
+
["so", "for", "because", "although", "though"],
|
| 49 |
+
["if", "then", "else", "when", "while"],
|
| 50 |
+
["where", "how", "why", "what", "who"],
|
| 51 |
+
["which", "that", "this", "these", "those"]
|
| 52 |
+
]
|
| 53 |
+
# A template for each line of the poem
|
| 54 |
+
template = [
|
| 55 |
+
"I {verb} the {adjective} {noun} {preposition} the {rhyme}",
|
| 56 |
+
"{connector}, I {verb} the {adjective} {noun} {preposition} the {rhyme}",
|
| 57 |
+
"I {verb} the {adjective} {noun} {preposition} the {rhyme}",
|
| 58 |
+
"{connector}, I {verb} the {adjective} {noun} {preposition} the {rhyme}",
|
| 59 |
+
"I {verb} the {adjective} {noun} {preposition} the {rhyme}"
|
| 60 |
+
]
|
| 61 |
+
# Choose a random rhyme scheme for the poem
|
| 62 |
+
rhyme_scheme = random.choice(rhymes)
|
| 63 |
+
# Generate the poem line by line
|
| 64 |
+
poem = ""
|
| 65 |
+
for i in range(5):
|
| 66 |
+
# Choose a random word for each placeholder in the template
|
| 67 |
+
verb = random.choice(verbs[i])
|
| 68 |
+
adjective = random.choice(adjectives[i])
|
| 69 |
+
noun = random.choice(nouns[i])
|
| 70 |
+
preposition = random.choice(prepositions[i])
|
| 71 |
+
connector = random.choice(connectors[i])
|
| 72 |
+
rhyme = rhyme_scheme[i]
|
| 73 |
+
# Replace the placeholders with the chosen words
|
| 74 |
+
line = template[i].format(verb=verb, adjective=adjective, noun=noun, preposition=preposition, connector=connector, rhyme=rhyme)
|
| 75 |
+
# Capitalize the first letter of the line
|
| 76 |
+
line = line[0].upper() + line[1:]
|
| 77 |
+
# Add the line to the poem
|
| 78 |
+
poem += line
|
| 79 |
+
# Add a comma or a period at the end of the line
|
| 80 |
+
if i < 4:
|
| 81 |
+
poem += ","
|
| 82 |
+
else:
|
| 83 |
+
poem += "."
|
| 84 |
+
# Add a newline character after the line
|
| 85 |
+
poem += "\n"
|
| 86 |
+
# Yield the poem so far
|
| 87 |
+
yield poem
|
| 88 |
|
| 89 |
+
# Create a text streaming mode interface with gradio
|
| 90 |
+
demo = gr.Interface(
|
| 91 |
+
generate_poem, # The function to run
|
| 92 |
+
gr.Textbox("Enter a topic for the poem"), # The input component
|
| 93 |
+
gr.Textbox(streaming=True), # The output component
|
| 94 |
+
title="Poem Generator", # The title of the interface
|
| 95 |
+
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
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Launch the interface
|
| 99 |
+
demo.launch()
|
| 100 |
+
|