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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
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 (unchanged)
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 * ~ , - 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:
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.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! ๐ŸŽ‰")
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! ๐Ÿ“ธ")