Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,68 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
const poems = [
|
| 4 |
-
{
|
| 5 |
-
text: "In the quiet of the evening, under skies so wide, I find my heart beating close to yours, my love. The stars above whisper secrets of our bond, eternal as the night, deep as the ocean's song.",
|
| 6 |
-
keywords: ["evening", "love", "stars"]
|
| 7 |
-
},
|
| 8 |
-
{
|
| 9 |
-
text: "Autumn leaves dance in crimson and gold, mirroring the fire that burns within my soul. Each leaf that falls is a promise made, that in your arms is where I'll fade.",
|
| 10 |
-
keywords: ["autumn", "leaves", "fire"]
|
| 11 |
-
},
|
| 12 |
-
{
|
| 13 |
-
text: "Your eyes are the moonlight that guides me through the dark, your touch the spark that ignites the fire in my heart. In this vast universe, you are my constant star, my love, my life, no matter near or far.",
|
| 14 |
-
keywords: ["moon", "eyes", "universe"]
|
| 15 |
-
},
|
| 16 |
-
{
|
| 17 |
-
text: "Like the river finds the sea, my heart has found its way to thee. Through storm and sunshine, come what may, in your love I'll always stay.",
|
| 18 |
-
keywords: ["river", "sea", "love"]
|
| 19 |
-
},
|
| 20 |
-
{
|
| 21 |
-
text: "In the garden of your heart, I plant my love so true. With every beat it grows, a flower just for you. The petals soft as whispers, the scent so sweet and light, blooming in the sunshine of your love so bright.",
|
| 22 |
-
keywords: ["garden", "heart", "flower"]
|
| 23 |
-
}
|
| 24 |
-
];
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
];
|
| 41 |
-
|
| 42 |
-
let currentBgIndex = 0;
|
| 43 |
-
|
| 44 |
-
// Set initial background
|
| 45 |
-
updateBackground();
|
| 46 |
-
|
| 47 |
-
// Event listeners
|
| 48 |
-
generateBtn.addEventListener('click', generatePoem);
|
| 49 |
-
poemPrompt.addEventListener('keypress', function(e) {
|
| 50 |
-
if (e.key === 'Enter') {
|
| 51 |
-
generatePoem();
|
| 52 |
-
}
|
| 53 |
-
});
|
| 54 |
-
|
| 55 |
-
prevBgBtn.addEventListener('click', showPreviousBackground);
|
| 56 |
-
nextBgBtn.addEventListener('click', showNextBackground);
|
| 57 |
-
bgSelect.addEventListener('change', function() {
|
| 58 |
-
const selectedId = this.value;
|
| 59 |
-
const index = backgrounds.findIndex(bg => bg.id === selectedId);
|
| 60 |
-
if (index !== -1) {
|
| 61 |
-
currentBgIndex = index;
|
| 62 |
-
updateBackground();
|
| 63 |
-
}
|
| 64 |
-
});
|
| 65 |
-
|
| 66 |
-
// Functions
|
| 67 |
-
function generatePoem() {
|
| 68 |
-
const input = poemPrompt.value.toLowerCase();
|
| 69 |
-
|
| 70 |
-
// Simple filtering based on input keywords
|
| 71 |
-
let filteredPoems = poems;
|
| 72 |
-
if (input.trim()) {
|
| 73 |
-
const keywords = input.split(/\s+/);
|
| 74 |
-
filteredPoems = poems.filter(poem => {
|
| 75 |
-
return keywords.some(keyword =>
|
| 76 |
-
poem.keywords.includes(keyword)
|
| 77 |
-
);
|
| 78 |
-
});
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
// If no matching poems, use all poems
|
| 82 |
-
if (filteredPoems.length === 0) {
|
| 83 |
-
filteredPoems = poems;
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
// Select random poem
|
| 87 |
-
const randomIndex = Math.floor(Math.random() * filteredPoems.length);
|
| 88 |
-
const selectedPoem = filteredPoems[randomIndex];
|
| 89 |
-
|
| 90 |
-
// Display poem with animation
|
| 91 |
-
poemText.style.opacity = 0;
|
| 92 |
-
setTimeout(() => {
|
| 93 |
-
poemText.textContent = selectedPoem.text;
|
| 94 |
-
poemText.style.opacity = 1;
|
| 95 |
-
|
| 96 |
-
// Change background
|
| 97 |
-
showNextBackground();
|
| 98 |
-
}, 300);
|
| 99 |
}
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Sample data for backgrounds and poems
|
| 5 |
+
backgrounds = ["autumn", "stars", "hearts"]
|
| 6 |
+
poems = [
|
| 7 |
+
{
|
| 8 |
+
"text": "In the quiet of the evening, under skies so wide, I find my heart beating close to yours, my love. The stars above whisper secrets of our bond, eternal as the night, deep as the ocean's song.",
|
| 9 |
+
"keywords": ["evening", "love", "stars"]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"text": "Autumn leaves dance in crimson and gold, mirroring the fire that burns within my soul. Each leaf that falls is a promise made, that in your arms is where I'll fade.",
|
| 13 |
+
"keywords": ["autumn", "leaves", "fire"]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"text": "Your eyes are the moonlight that guides me through the dark, your touch the spark that ignites the fire in my heart. In this vast universe, you are my constant star, my love, my life, no matter near or far.",
|
| 17 |
+
"keywords": ["moon", "eyes", "universe"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
]
|
| 20 |
|
| 21 |
+
def generate_poem(prompt):
|
| 22 |
+
# Simple filtering based on input keywords
|
| 23 |
+
filtered_poems = [poem for poem in poems if any(keyword.lower() in prompt.lower() for keyword in poem["keywords"])]
|
| 24 |
+
|
| 25 |
+
# If no matching poems, use all poems
|
| 26 |
+
if not filtered_poems:
|
| 27 |
+
filtered_poems = poems
|
| 28 |
+
|
| 29 |
+
# Select random poem
|
| 30 |
+
selected_poem = random.choice(filtered_poems)
|
| 31 |
+
return selected_poem["text"]
|
| 32 |
|
| 33 |
+
def change_background(direction):
|
| 34 |
+
global current_background_index
|
| 35 |
+
if direction == "next":
|
| 36 |
+
current_background_index = (current_background_index + 1) % len(backgrounds)
|
| 37 |
+
elif direction == "previous":
|
| 38 |
+
current_background_index = (current_background_index - 1) % len(backgrounds)
|
| 39 |
+
return backgrounds[current_background_index]
|
| 40 |
|
| 41 |
+
current_background_index = 0
|
| 42 |
+
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# Stellar Verse - Romantic Poetry Generator")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
poem_prompt = gr.Textbox(label="Enter themes or keywords for your poem", placeholder="Enter themes (love, moon, heart...)")
|
| 48 |
+
generate_btn = gr.Button("Generate Poem")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
prev_bg_btn = gr.Button("Previous Background", value="≺")
|
| 52 |
+
bg_select = gr.Dropdown(label="Select background", choices=backgrounds)
|
| 53 |
+
next_bg_btn = gr.Button("Next Background", value="≻")
|
| 54 |
+
|
| 55 |
+
poem_text = gr.Textbox(label="Your romantic poem will appear here...", lines=5, interactive=False)
|
| 56 |
+
|
| 57 |
+
def update_poem(prompt):
|
| 58 |
+
return generate_poem(prompt)
|
| 59 |
+
|
| 60 |
+
def update_background(*args):
|
| 61 |
+
direction = args[0]
|
| 62 |
+
return change_background(direction)
|
| 63 |
+
|
| 64 |
+
generate_btn.click(fn=update_poem, inputs=[poem_prompt], outputs=[poem_text])
|
| 65 |
+
prev_bg_btn.click(fn=update_background, inputs="previous", outputs=bg_select)
|
| 66 |
+
next_bg_btn.click(fn=update_background, inputs="next", outputs=bg_select)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|