Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import tempfile
|
|
8 |
import os
|
9 |
from PIL import Image
|
10 |
import string
|
11 |
-
import re
|
12 |
|
13 |
# Initialize pipelines with caching
|
14 |
@st.cache_resource
|
@@ -30,35 +29,31 @@ def generate_content(image):
|
|
30 |
st.write("**๐ What's in the picture: ๐**")
|
31 |
st.write(caption)
|
32 |
|
33 |
-
# Create prompt for story
|
34 |
prompt = (
|
35 |
f"Write a funny, warm children's story for ages 3-10, 50โ100 words, "
|
36 |
-
f"Completely and precisely centered on this scene {caption}\nStory:"
|
37 |
)
|
38 |
|
39 |
# Generate raw story
|
40 |
raw = storyer(
|
41 |
prompt,
|
42 |
max_new_tokens=150,
|
|
|
43 |
temperature=0.7,
|
44 |
top_p=0.9,
|
45 |
no_repeat_ngram_size=2,
|
46 |
return_full_text=False
|
47 |
)[0]["generated_text"].strip()
|
48 |
|
49 |
-
# Define allowed characters to keep (removes symbols like *
|
50 |
-
allowed_chars = string.ascii_letters + "
|
51 |
|
52 |
# Clean the raw story by keeping only allowed characters
|
53 |
clean_raw = ''.join(c for c in raw if c in allowed_chars)
|
54 |
|
55 |
-
#
|
56 |
-
clean_raw = re.sub(r'\s+', ' ', clean_raw).strip('.!? ')
|
57 |
-
|
58 |
-
# Split into words and ensure at least 50 words, trim to 100 words
|
59 |
words = clean_raw.split()
|
60 |
-
if len(words) < 50:
|
61 |
-
words.extend("The children laughed and played happily, making new friends in the sunny park.".split())
|
62 |
story = " ".join(words[:100])
|
63 |
|
64 |
st.write("**๐ Your funny story: ๐**")
|
@@ -82,12 +77,16 @@ st.markdown("Upload a picture to make a funny story and hear it too! ๐ธ")
|
|
82 |
uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
|
83 |
|
84 |
if uploaded_image is None:
|
85 |
-
st.
|
86 |
else:
|
87 |
st.image(uploaded_image, caption="Your Picture ๐", use_column_width=True)
|
88 |
-
|
|
|
|
|
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)
|
|
|
|
|
|
8 |
import os
|
9 |
from PIL import Image
|
10 |
import string
|
|
|
11 |
|
12 |
# Initialize pipelines with caching
|
13 |
@st.cache_resource
|
|
|
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:"
|
36 |
)
|
37 |
|
38 |
# Generate raw story
|
39 |
raw = storyer(
|
40 |
prompt,
|
41 |
max_new_tokens=150,
|
42 |
+
min_new_tokens=50
|
43 |
temperature=0.7,
|
44 |
top_p=0.9,
|
45 |
no_repeat_ngram_size=2,
|
46 |
return_full_text=False
|
47 |
)[0]["generated_text"].strip()
|
48 |
|
49 |
+
# Define allowed characters to keep (removes symbols like * and ~)
|
50 |
+
allowed_chars = string.ascii_letters + string.digits + " .,!?\"'-"
|
51 |
|
52 |
# Clean the raw story by keeping only allowed characters
|
53 |
clean_raw = ''.join(c for c in raw if c in allowed_chars)
|
54 |
|
55 |
+
# Split into words and trim to 100 words
|
|
|
|
|
|
|
56 |
words = clean_raw.split()
|
|
|
|
|
57 |
story = " ".join(words[:100])
|
58 |
|
59 |
st.write("**๐ Your funny story: ๐**")
|
|
|
77 |
uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
|
78 |
|
79 |
if uploaded_image is None:
|
80 |
+
st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here! ๐ท", use_column_width=True)
|
81 |
else:
|
82 |
st.image(uploaded_image, caption="Your Picture ๐", use_column_width=True)
|
83 |
+
|
84 |
+
if st.button("โจ Make My Story! โจ"):
|
85 |
+
if uploaded_image is not None:
|
86 |
with st.spinner("๐ฎ Creating your magical story..."):
|
87 |
caption, story, audio_path = generate_content(uploaded_image)
|
88 |
st.success("๐ Your story is ready! ๐")
|
89 |
st.audio(audio_path, format="audio/wav")
|
90 |
+
os.remove(audio_path)
|
91 |
+
else:
|
92 |
+
st.warning("Please upload a picture first! ๐ธ")
|