Update app.py
Browse files
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 +
|
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
|
|
|
|
|
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)
|
|
|
|