Szeyu commited on
Commit
789ce84
ยท
verified ยท
1 Parent(s): b9ea623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
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 * 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:
@@ -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.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here! ๐Ÿ“ท", use_column_width=True)
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! ๐ŸŽ‰")