File size: 3,897 Bytes
fee252b
b6e8d74
fee252b
b6e8d74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fee252b
b6e8d74
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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()