Spaces:
Sleeping
Sleeping
File size: 8,713 Bytes
1c26e4c 8a22239 e211a6f 1c26e4c e211a6f 1a70ad2 8a22239 e211a6f 8a22239 e211a6f 1a70ad2 e211a6f 1a70ad2 e211a6f 8a22239 e211a6f 1a70ad2 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 1a70ad2 8a22239 e211a6f 8a22239 e211a6f 1c26e4c 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 1c26e4c 8a22239 e211a6f 8a22239 1c26e4c 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 e211a6f 8a22239 1c26e4c e211a6f |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
import streamlit as st
import streamlit.components.v1 as components
import spacy
import random
import re
from gtts import gTTS
from PIL import Image, ImageDraw, ImageFont
import io
import numpy as np
import base64
# Load spaCy model for NLP
nlp = spacy.load("en_core_web_sm")
# Default word lists for storytelling classes (derived from original app)
default_word_lists = {
"Location": ["quiet town", "small village", "city", "forest", "mountain"],
"Actions": ["walking", "pedaling", "running", "dancing", "exploring"],
"Thoughts": ["chasing shadows", "what if", "brilliance of years", "echoes", "secrets"],
"Emotions": ["joy", "pain", "trembling smile", "storm", "silent art"],
"Dialogue": ["\"Keep moving, dare to feel;\"", "\"Am I chasing shadows?\"", "\"The dawn awaits!\"", "\"I love you.\"", "\"Letโs go!\""]
}
# Suit properties for narrative flavor
suit_properties = {
"Hearts": "emotional or romantic",
"Diamonds": "wealthy or luxurious",
"Clubs": "conflict or struggle",
"Spades": "mysterious or dangerous"
}
# Sentence templates for story generation
sentence_templates = {
"Location": "The story unfolded in a {property} {word}.",
"Actions": "Suddenly, a {property} {word} changed everything.",
"Thoughts": "A {property} thought, '{word}', crossed their mind.",
"Emotions": "A {property} wave of {word} surged through them.",
"Dialogue": "Someone spoke with a {property} tone: {word}"
}
# Function to process user input and augment word lists
def augment_word_lists(user_input):
doc = nlp(user_input)
augmented_lists = {key: list(set(val)) for key, val in default_word_lists.items()}
for ent in doc.ents:
if ent.label_ in ["GPE", "LOC"]:
augmented_lists["Location"].append(ent.text)
for token in doc:
if token.pos_ == "VERB" and token.text not in augmented_lists["Actions"]:
augmented_lists["Actions"].append(token.text)
elif token.pos_ == "NOUN" and token.text not in augmented_lists["Location"]:
if "emotion" in token.text.lower() or token.text in ["joy", "pain", "smile"]:
augmented_lists["Emotions"].append(token.text)
else:
augmented_lists["Thoughts"].append(token.text)
dialogues = re.findall(r'"[^"]*"', user_input)
augmented_lists["Dialogue"].extend(dialogues)
return augmented_lists
# Create a 52-card deck
def create_deck():
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
ranks = list(range(1, 14))
deck = [(suit, rank) for suit in suits for rank in ranks]
random.shuffle(deck)
return deck
# Assign cards to classes
def assign_card_to_class(card_index):
if 0 <= card_index < 10:
return "Location"
elif 10 <= card_index < 20:
return "Actions"
elif 20 <= card_index < 30:
return "Thoughts"
elif 30 <= card_index < 40:
return "Emotions"
else:
return "Dialogue"
# Generate card image
def generate_card_image(suit, rank, story_class, word, property):
img = Image.new("RGB", (200, 300), color="white")
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
draw.text((10, 10), f"{rank} of {suit}", fill="black", font=font)
draw.text((10, 50), f"Class: {story_class}", fill="black", font=font)
draw.text((10, 90), f"Word: {word}", fill="black", font=font)
draw.text((10, 130), f"Property: {property}", fill="black", font=font)
buffer = io.BytesIO()
img.save(buffer, format="PNG")
return buffer.getvalue()
# Generate story sentence
def generate_story_sentence(story_class, word, property):
return sentence_templates[story_class].format(word=word, property=property)
# Generate song lyrics
def generate_song_lyrics(story_text):
doc = nlp(story_text)
key_elements = [token.text for token in doc if token.pos_ in ["NOUN", "VERB", "ADJ"]][:12] # ~60 seconds
lyrics = "\n".join([f"{key_elements[i]} {key_elements[i+1]}" for i in range(0, len(key_elements)-1, 2)])
return lyrics
# Main app
def main():
st.set_page_config(page_title="StoryForge: The Game", page_icon="๐ด", layout="wide")
st.title("๐ด StoryForge: A Storytelling Adventure ๐ด")
# User input
st.markdown("## ๐ Your Story Seed")
user_input = st.text_area("Paste your story inspiration here:", height=200)
# Session state initialization
if "augmented_lists" not in st.session_state:
st.session_state.augmented_lists = default_word_lists
if "deck" not in st.session_state:
st.session_state.deck = create_deck()
if "story" not in st.session_state:
st.session_state.story = []
if "drawn_cards" not in st.session_state:
st.session_state.drawn_cards = 0
# Process input
if st.button("Start Game"):
if user_input:
st.session_state.augmented_lists = augment_word_lists(user_input)
st.session_state.deck = create_deck()
st.session_state.story = []
st.session_state.drawn_cards = 0
st.success("Game started! Draw your first card.")
# Draw card
col1, col2 = st.columns([1, 3])
with col1:
if st.button("Draw Card") and st.session_state.drawn_cards < 52:
card_index = st.session_state.drawn_cards
suit, rank = st.session_state.deck[card_index]
story_class = assign_card_to_class(card_index)
word = random.choice(st.session_state.augmented_lists[story_class])
property = suit_properties[suit]
card_image = generate_card_image(suit, rank, story_class, word, property)
st.image(card_image, caption=f"Card {card_index + 1}")
sentence = generate_story_sentence(story_class, word, property)
st.session_state.story.append(sentence)
st.session_state.drawn_cards += 1
with col2:
st.markdown("### ๐ Your Story Unfolds")
if st.session_state.story:
st.write("\n".join(st.session_state.story))
# Song generation
if st.session_state.drawn_cards == 52:
full_story = "\n".join(st.session_state.story)
st.markdown("### ๐ต Story Song")
lyrics = generate_song_lyrics(full_story)
st.write(lyrics)
tts = gTTS(text=lyrics, lang="en")
audio_file = "story_song.mp3"
tts.save(audio_file)
st.audio(audio_file, format="audio/mp3")
# Enhanced UI with p5.js
st.markdown("## ๐ฎ Game Board")
components.html("""
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.2/p5.min.js"></script>
<div id="sketch-holder"></div>
<script>
function setup() {
let canvas = createCanvas(600, 200);
canvas.parent('sketch-holder');
background(240);
textSize(20);
textAlign(CENTER);
text('Draw cards to weave your epic tale!', width/2, height/2);
}
function draw() {
if (mouseIsPressed) {
fill(255, 0, 0);
ellipse(mouseX, mouseY, 20, 20);
}
}
</script>
""", height=220)
# Retain original star layout
st.markdown("## โญ Five Pillars of Storytelling โญ")
star_html = """
<div style="position: relative; width: 400px; height: 400px; margin: auto; border: 1px dashed #aaa; border-radius: 50%;">
<div style="position: absolute; top: 50px; left: 200px; transform: translate(-50%, -50%); text-align: center;">
<div style="font-size: 2em;">๐ </div><div><b>Location</b></div>
</div>
<div style="position: absolute; top: 154px; left: 57px; transform: translate(-50%, -50%); text-align: center;">
<div style="font-size: 2em;">๐</div><div><b>Actions</b></div>
</div>
<div style="position: absolute; top: 321px; left: 112px; transform: translate(-50%, -50%); text-align: center;">
<div style="font-size: 2em;">๐ง </div><div><b>Thoughts</b></div>
</div>
<div style="position: absolute; top: 321px; left: 288px; transform: translate(-50%, -50%); text-align: center;">
<div style="font-size: 2em;">๐ฒ</div><div><b>Emotions</b></div>
</div>
<div style="position: absolute; top: 154px; left: 343px; transform: translate(-50%, -50%); text-align: center;">
<div style="font-size: 2em;">๐ฌ</div><div><b>Dialogue</b></div>
</div>
</div>
"""
components.html(star_html, height=450)
if __name__ == "__main__":
main() |