Szeyu commited on
Commit
b95df49
ยท
verified ยท
1 Parent(s): 2335bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -4,7 +4,7 @@ from transformers import pipeline
4
  import textwrap
5
  import numpy as np
6
  import soundfile as sf
7
- import tempfile
8
  import os
9
  from PIL import Image
10
  import string
@@ -29,7 +29,7 @@ def generate_content(image):
29
  st.write("**๐ŸŒŸ What's in the picture: ๐ŸŒŸ**")
30
  st.write(caption)
31
 
32
- # Create prompt for story
33
  prompt = (
34
  f"Write a funny, warm children's story for ages 3-10, 50โ€“100 words, "
35
  f"Completely and precisely centered on this scene {caption}\nStory:"
@@ -45,14 +45,16 @@ def generate_content(image):
45
  return_full_text=False
46
  )[0]["generated_text"].strip()
47
 
48
- # Define allowed characters to keep (removes symbols like * and ~)
49
- allowed_chars = string.ascii_letters + string.digits + " .,!?\"'-"
50
 
51
  # Clean the raw story by keeping only allowed characters
52
  clean_raw = ''.join(c for c in raw if c in allowed_chars)
53
 
54
- # Split into words and trim to 100 words
55
  words = clean_raw.split()
 
 
56
  story = " ".join(words[:100])
57
 
58
  st.write("**๐Ÿ“– Your funny story: ๐Ÿ“–**")
@@ -81,11 +83,11 @@ else:
81
  st.image(uploaded_image, caption="Your Picture ๐ŸŒŸ", use_column_width=True)
82
 
83
  if st.button("โœจ Make My Story! โœจ"):
84
- if uploaded_image is not None:
 
 
85
  with st.spinner("๐Ÿ”ฎ Creating your magical story..."):
86
  caption, story, audio_path = generate_content(uploaded_image)
87
  st.success("๐ŸŽ‰ Your story is ready! ๐ŸŽ‰")
88
  st.audio(audio_path, format="audio/wav")
89
- os.remove(audio_path)
90
- else:
91
- st.warning("Please upload a picture first! ๐Ÿ“ธ")
 
4
  import textwrap
5
  import numpy as np
6
  import soundfile as sf
7
+ ็Ÿญๆ–‡ import tempfile
8
  import os
9
  from PIL import Image
10
  import string
 
29
  st.write("**๐ŸŒŸ What's in the picture: ๐ŸŒŸ**")
30
  st.write(caption)
31
 
32
+ # Create prompt for story (unchanged)
33
  prompt = (
34
  f"Write a funny, warm children's story for ages 3-10, 50โ€“100 words, "
35
  f"Completely and precisely centered on this scene {caption}\nStory:"
 
45
  return_full_text=False
46
  )[0]["generated_text"].strip()
47
 
48
+ # Define allowed characters to keep (removes symbols like * and ~, and digits)
49
+ allowed_chars = string.ascii_letters + " .,!?\"'-"
50
 
51
  # Clean the raw story by keeping only allowed characters
52
  clean_raw = ''.join(c for c in raw if c in allowed_chars)
53
 
54
+ # Split into words and ensure at least 50 words, trim to 100 words
55
  words = clean_raw.split()
56
+ if len(words) < 50:
57
+ words.extend("The children laughed and played happily, making new friends in the sunny park.".split())
58
  story = " ".join(words[:100])
59
 
60
  st.write("**๐Ÿ“– Your funny story: ๐Ÿ“–**")
 
83
  st.image(uploaded_image, caption="Your Picture ๐ŸŒŸ", use_column_width=True)
84
 
85
  if st.button("โœจ Make My Story! โœจ"):
86
+ if uploaded_image is None:
87
+ st.warning("Please upload a picture first! ๐Ÿ“ธ")
88
+ else:
89
  with st.spinner("๐Ÿ”ฎ Creating your magical story..."):
90
  caption, story, audio_path = generate_content(uploaded_image)
91
  st.success("๐ŸŽ‰ Your story is ready! ๐ŸŽ‰")
92
  st.audio(audio_path, format="audio/wav")
93
+ os.remove(audio_path)