awacke1 commited on
Commit
06493e4
·
verified ·
1 Parent(s): ecc334b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -47
app.py CHANGED
@@ -1,18 +1,12 @@
1
  import streamlit as st
2
- import streamlit.components.v1 as components
3
- import spacy
4
  import random
5
  import re
6
  from gtts import gTTS
7
  from PIL import Image, ImageDraw, ImageFont
8
  import io
9
- import numpy as np
10
  import base64
11
 
12
- # Load spaCy model for NLP
13
- nlp = spacy.load("en_core_web_sm")
14
-
15
- # Default word lists for storytelling classes (derived from original app)
16
  default_word_lists = {
17
  "Location": ["quiet town", "small village", "city", "forest", "mountain"],
18
  "Actions": ["walking", "pedaling", "running", "dancing", "exploring"],
@@ -38,27 +32,37 @@ sentence_templates = {
38
  "Dialogue": "Someone spoke with a {property} tone: {word}"
39
  }
40
 
41
- # Function to process user input and augment word lists
42
  def augment_word_lists(user_input):
43
- doc = nlp(user_input)
44
  augmented_lists = {key: list(set(val)) for key, val in default_word_lists.items()}
45
 
46
- for ent in doc.ents:
47
- if ent.label_ in ["GPE", "LOC"]:
48
- augmented_lists["Location"].append(ent.text)
49
 
50
- for token in doc:
51
- if token.pos_ == "VERB" and token.text not in augmented_lists["Actions"]:
52
- augmented_lists["Actions"].append(token.text)
53
- elif token.pos_ == "NOUN" and token.text not in augmented_lists["Location"]:
54
- if "emotion" in token.text.lower() or token.text in ["joy", "pain", "smile"]:
55
- augmented_lists["Emotions"].append(token.text)
56
- else:
57
- augmented_lists["Thoughts"].append(token.text)
58
 
 
59
  dialogues = re.findall(r'"[^"]*"', user_input)
60
  augmented_lists["Dialogue"].extend(dialogues)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  return augmented_lists
63
 
64
  # Create a 52-card deck
@@ -103,8 +107,8 @@ def generate_story_sentence(story_class, word, property):
103
 
104
  # Generate song lyrics
105
  def generate_song_lyrics(story_text):
106
- doc = nlp(story_text)
107
- key_elements = [token.text for token in doc if token.pos_ in ["NOUN", "VERB", "ADJ"]][:12] # ~60 seconds
108
  lyrics = "\n".join([f"{key_elements[i]} {key_elements[i+1]}" for i in range(0, len(key_elements)-1, 2)])
109
  return lyrics
110
 
@@ -136,7 +140,7 @@ def main():
136
  st.session_state.drawn_cards = 0
137
  st.success("Game started! Draw your first card.")
138
 
139
- # Draw card
140
  col1, col2 = st.columns([1, 3])
141
  with col1:
142
  if st.button("Draw Card") and st.session_state.drawn_cards < 52:
@@ -170,30 +174,20 @@ def main():
170
  tts.save(audio_file)
171
  st.audio(audio_file, format="audio/mp3")
172
 
173
- # Enhanced UI with p5.js
174
- st.markdown("## 🎮 Game Board")
175
- components.html("""
176
- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.2/p5.min.js"></script>
177
- <div id="sketch-holder"></div>
178
- <script>
179
- function setup() {
180
- let canvas = createCanvas(600, 200);
181
- canvas.parent('sketch-holder');
182
- background(240);
183
- textSize(20);
184
- textAlign(CENTER);
185
- text('Draw cards to weave your epic tale!', width/2, height/2);
186
- }
187
- function draw() {
188
- if (mouseIsPressed) {
189
- fill(255, 0, 0);
190
- ellipse(mouseX, mouseY, 20, 20);
191
- }
192
- }
193
- </script>
194
- """, height=220)
195
 
196
- # Retain original star layout
197
  st.markdown("## ⭐ Five Pillars of Storytelling ⭐")
198
  star_html = """
199
  <div style="position: relative; width: 400px; height: 400px; margin: auto; border: 1px dashed #aaa; border-radius: 50%;">
@@ -214,7 +208,7 @@ def main():
214
  </div>
215
  </div>
216
  """
217
- components.html(star_html, height=450)
218
 
219
  if __name__ == "__main__":
220
  main()
 
1
  import streamlit as st
 
 
2
  import random
3
  import re
4
  from gtts import gTTS
5
  from PIL import Image, ImageDraw, ImageFont
6
  import io
 
7
  import base64
8
 
9
+ # Default word lists for storytelling classes
 
 
 
10
  default_word_lists = {
11
  "Location": ["quiet town", "small village", "city", "forest", "mountain"],
12
  "Actions": ["walking", "pedaling", "running", "dancing", "exploring"],
 
32
  "Dialogue": "Someone spoke with a {property} tone: {word}"
33
  }
34
 
35
+ # Pure Python function to augment word lists from user input
36
  def augment_word_lists(user_input):
 
37
  augmented_lists = {key: list(set(val)) for key, val in default_word_lists.items()}
38
 
39
+ # Split input into words
40
+ words = user_input.lower().split()
 
41
 
42
+ # Simple heuristic lists for categorization
43
+ location_keywords = ["town", "village", "city", "forest", "mountain", "place", "land"]
44
+ action_keywords = ["walk", "run", "dance", "pedal", "explore", "move", "jump"]
45
+ emotion_keywords = ["joy", "pain", "smile", "storm", "fear", "love", "anger"]
 
 
 
 
46
 
47
+ # Extract dialogues with regex
48
  dialogues = re.findall(r'"[^"]*"', user_input)
49
  augmented_lists["Dialogue"].extend(dialogues)
50
 
51
+ # Categorize words
52
+ for word in words:
53
+ if any(keyword in word for keyword in location_keywords):
54
+ augmented_lists["Location"].append(word)
55
+ elif any(keyword in word for keyword in action_keywords):
56
+ augmented_lists["Actions"].append(word)
57
+ elif any(keyword in word for keyword in emotion_keywords):
58
+ augmented_lists["Emotions"].append(word)
59
+ elif "?" in word or "what" in word or "why" in word: # Simple thought detection
60
+ augmented_lists["Thoughts"].append(word)
61
+
62
+ # Remove duplicates
63
+ for key in augmented_lists:
64
+ augmented_lists[key] = list(set(augmented_lists[key]))
65
+
66
  return augmented_lists
67
 
68
  # Create a 52-card deck
 
107
 
108
  # Generate song lyrics
109
  def generate_song_lyrics(story_text):
110
+ words = story_text.split()
111
+ key_elements = [word for word in words if len(word) > 3][:12] # Simple filter for ~60 seconds
112
  lyrics = "\n".join([f"{key_elements[i]} {key_elements[i+1]}" for i in range(0, len(key_elements)-1, 2)])
113
  return lyrics
114
 
 
140
  st.session_state.drawn_cards = 0
141
  st.success("Game started! Draw your first card.")
142
 
143
+ # Draw card and story display
144
  col1, col2 = st.columns([1, 3])
145
  with col1:
146
  if st.button("Draw Card") and st.session_state.drawn_cards < 52:
 
174
  tts.save(audio_file)
175
  st.audio(audio_file, format="audio/mp3")
176
 
177
+ # Enhanced UI with HTML/CSS
178
+ st.markdown("""
179
+ ## 🎮 Game Board
180
+ <div style="background-color: #f0f0f0; padding: 20px; border-radius: 10px; text-align: center;">
181
+ <h3 style="color: #333;">Draw cards to weave your epic tale!</h3>
182
+ <button style="background-color: #ff9900; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;"
183
+ onmouseover="this.style.backgroundColor='#cc7700'"
184
+ onmouseout="this.style.backgroundColor='#ff9900'">
185
+ Hover for a surprise!
186
+ </button>
187
+ </div>
188
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
189
 
190
+ # Star layout
191
  st.markdown("## ⭐ Five Pillars of Storytelling ⭐")
192
  star_html = """
193
  <div style="position: relative; width: 400px; height: 400px; margin: auto; border: 1px dashed #aaa; border-radius: 50%;">
 
208
  </div>
209
  </div>
210
  """
211
+ st.markdown(star_html, unsafe_allow_html=True)
212
 
213
  if __name__ == "__main__":
214
  main()