avinash
commited on
Commit
·
9e02977
1
Parent(s):
e9bb45f
added new things
Browse files- app.py +91 -0
- explanation.json +0 -0
- explanations.json +15 -0
- poem.txt +25 -0
- requirements.txt +2 -0
app.py
CHANGED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from gtts import gTTS
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
|
7 |
+
# Load poem and explanations
|
8 |
+
with open('poem.txt', 'r', encoding='utf-8') as f:
|
9 |
+
poem_data = f.read()
|
10 |
+
|
11 |
+
with open('explanations.json', 'r', encoding='utf-8') as f:
|
12 |
+
explanations = json.load(f)
|
13 |
+
|
14 |
+
# Stanza split
|
15 |
+
stanzas = poem_data.strip().split("Stanza")
|
16 |
+
stanza_map = {str(i): s.strip() for i, s in enumerate(stanzas[1:], start=1)}
|
17 |
+
|
18 |
+
# Poem Generator
|
19 |
+
def generate_poem(objects):
|
20 |
+
starters = [
|
21 |
+
f"In the world of the {objects[0]}, stories unfold,",
|
22 |
+
f"The {objects[0]} stands tall and bold.",
|
23 |
+
f"A {objects[0]} whispers under the sky,",
|
24 |
+
f"Where the {objects[0]} and {objects[1]} lie."
|
25 |
+
]
|
26 |
+
middles = [
|
27 |
+
f"The {objects[1]} flows with tales unknown,",
|
28 |
+
f"Near the {objects[1]}, seeds are sown.",
|
29 |
+
f"And the {objects[1]} sings a gentle tune,",
|
30 |
+
f"Under stars and a watching moon."
|
31 |
+
]
|
32 |
+
endings = [
|
33 |
+
"In quiet places, poems are made,",
|
34 |
+
"With every breeze, old memories fade.",
|
35 |
+
"Such beauty lives in things so small,",
|
36 |
+
"They write their poems after all."
|
37 |
+
]
|
38 |
+
return f"{random.choice(starters)}\n{random.choice(middles)}\n{random.choice(endings)}"
|
39 |
+
|
40 |
+
# Logic
|
41 |
+
def handle_input(user_input, poem_topic):
|
42 |
+
user_input = user_input.lower()
|
43 |
+
response = ""
|
44 |
+
|
45 |
+
if "read stanza" in user_input:
|
46 |
+
for i in range(1, 4):
|
47 |
+
if str(i) in user_input:
|
48 |
+
response = f"Stanza {i}:\n{stanza_map[str(i)]}"
|
49 |
+
break
|
50 |
+
elif "explain" in user_input:
|
51 |
+
for i in range(1, 4):
|
52 |
+
if str(i) in user_input:
|
53 |
+
response = f"Explanation:\n{explanations[str(i)]['explanation']}"
|
54 |
+
break
|
55 |
+
elif "poetic devices" in user_input or "devices" in user_input:
|
56 |
+
for i in range(1, 4):
|
57 |
+
if str(i) in user_input:
|
58 |
+
devices = ", ".join(explanations[str(i)]["devices"])
|
59 |
+
response = f"Poetic Devices in stanza {i}: {devices}"
|
60 |
+
break
|
61 |
+
elif "poet" in user_input:
|
62 |
+
response = explanations["poet"]
|
63 |
+
elif poem_topic:
|
64 |
+
objs = [x.strip() for x in poem_topic.split("and") if x.strip()]
|
65 |
+
if len(objs) < 2:
|
66 |
+
objs.append("dreams")
|
67 |
+
poem = generate_poem(objs[:2])
|
68 |
+
response = f"Here's a poem about {', '.join(objs[:2])}:\n\n{poem}"
|
69 |
+
else:
|
70 |
+
response = "Please ask me to read or explain a stanza, or generate a poem."
|
71 |
+
|
72 |
+
tts = gTTS(response)
|
73 |
+
tts.save("poembot_response.mp3")
|
74 |
+
|
75 |
+
return response, "poembot_response.mp3"
|
76 |
+
|
77 |
+
iface = gr.Interface(
|
78 |
+
fn=handle_input,
|
79 |
+
inputs=[
|
80 |
+
gr.Textbox(label="🎤 Ask me something (e.g., 'Read stanza 1')"),
|
81 |
+
gr.Textbox(label="🎨 Make a poem about (e.g., 'cloud and time')"),
|
82 |
+
],
|
83 |
+
outputs=[
|
84 |
+
gr.Textbox(label="📖 Response"),
|
85 |
+
gr.Audio(label="🔊 Voice Response", autoplay=True)
|
86 |
+
],
|
87 |
+
title="PoemBot - Voice Poetry Assistant",
|
88 |
+
description="Ask me to read stanzas, explain, find poetic devices or create poems from your words!"
|
89 |
+
)
|
90 |
+
|
91 |
+
iface.launch()
|
explanation.json
DELETED
File without changes
|
explanations.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"1": {
|
3 |
+
"explanation": "This stanza reflects on a photograph of the poet's mother as a child, capturing a moment of innocence and joy. It introduces the theme of time and transience.",
|
4 |
+
"devices": ["Imagery", "Alliteration"]
|
5 |
+
},
|
6 |
+
"2": {
|
7 |
+
"explanation": "The poet recalls how her mother used to laugh at the photograph, showing how memories become precious after a loss. It explores the contrast between past and present.",
|
8 |
+
"devices": ["Contrast", "Enjambment"]
|
9 |
+
},
|
10 |
+
"3": {
|
11 |
+
"explanation": "This stanza expresses deep grief. The poet reflects on her mother's death and how silence becomes the only response to sorrow.",
|
12 |
+
"devices": ["Oxymoron", "Metaphor"]
|
13 |
+
},
|
14 |
+
"poet": "Shirley Toulson was an English poet known for her reflective and emotional writing. 'A Photograph' is one of her best-known poems, exploring memory, loss, and the passage of time."
|
15 |
+
}
|
poem.txt
CHANGED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Stanza 1:
|
2 |
+
The cardboard shows me how it was
|
3 |
+
When the two girl cousins went paddling,
|
4 |
+
Each one holding one of my mother’s hands,
|
5 |
+
And she the big girl – some twelve years or so.
|
6 |
+
|
7 |
+
All three stood still to smile through their hair
|
8 |
+
At the uncle with the camera, A sweet face,
|
9 |
+
My mother’s, that was before I was born
|
10 |
+
And the sea, which appears to have changed less,
|
11 |
+
Washed their terribly transient feet.
|
12 |
+
|
13 |
+
Stanza 2:
|
14 |
+
Some twenty-thirty years later
|
15 |
+
She’d laugh at the snapshot. “See Betty
|
16 |
+
And Dolly,” she’d say, “and look how they
|
17 |
+
Dressed us for the beach.” The sea holiday
|
18 |
+
Was her past, mine is her laughter. Both wry
|
19 |
+
With the laboured ease of loss.
|
20 |
+
|
21 |
+
Stanza 3:
|
22 |
+
Now she’s been dead nearly as many years
|
23 |
+
As that girl lived. And of this circumstance
|
24 |
+
There is nothing to say at all,
|
25 |
+
Its silence silences.
|
requirements.txt
CHANGED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
gtts
|