Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import tempfile
|
|
| 8 |
import os
|
| 9 |
from PIL import Image
|
| 10 |
import string
|
|
|
|
| 11 |
|
| 12 |
# Initialize pipelines with caching
|
| 13 |
@st.cache_resource
|
|
@@ -45,12 +46,15 @@ def generate_content(image):
|
|
| 45 |
return_full_text=False
|
| 46 |
)[0]["generated_text"].strip()
|
| 47 |
|
| 48 |
-
# Define allowed characters to keep (removes symbols like *
|
| 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:
|
|
@@ -78,14 +82,10 @@ st.markdown("Upload a picture to make a funny story and hear it too! ๐ธ")
|
|
| 78 |
uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
|
| 79 |
|
| 80 |
if uploaded_image is None:
|
| 81 |
-
st.
|
| 82 |
else:
|
| 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! ๐")
|
|
|
|
| 8 |
import os
|
| 9 |
from PIL import Image
|
| 10 |
import string
|
| 11 |
+
import re
|
| 12 |
|
| 13 |
# Initialize pipelines with caching
|
| 14 |
@st.cache_resource
|
|
|
|
| 46 |
return_full_text=False
|
| 47 |
)[0]["generated_text"].strip()
|
| 48 |
|
| 49 |
+
# Define allowed characters to keep (removes symbols like * ~ , - and digits)
|
| 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 |
+
# Remove multiple spaces and trailing punctuation
|
| 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:
|
|
|
|
| 82 |
uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
|
| 83 |
|
| 84 |
if uploaded_image is None:
|
| 85 |
+
st.warning("Please upload a picture first! ๐ธ")
|
| 86 |
else:
|
| 87 |
st.image(uploaded_image, caption="Your Picture ๐", use_column_width=True)
|
| 88 |
+
if st.button("โจ Make My Story! โจ"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with st.spinner("๐ฎ Creating your magical story..."):
|
| 90 |
caption, story, audio_path = generate_content(uploaded_image)
|
| 91 |
st.success("๐ Your story is ready! ๐")
|