JSY8 commited on
Commit
8d03f81
Β·
verified Β·
1 Parent(s): cd9cb87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -29,33 +29,31 @@ 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, interesting children's story that precisely centered on this scene {caption}\nStory:"
35
- f"in third-person narrative, that describes this scene exactly: {caption} "
36
- f"mention the exact place, location or venue within {caption}"
37
- f"Avoid numbers,random letter combinations\n,and single-letter combinations"
38
- )
39
-
40
- # Generate raw story
41
- raw = storyer(
42
- prompt,
43
- max_new_tokens=150,
44
- temperature=0.7,
45
- top_p=0.9,
46
- no_repeat_ngram_size=2,
47
- return_full_text=False
48
- )[0]["generated_text"].strip()
49
 
50
- # Define allowed characters to keep (removes symbols like * and ~)
51
- allowed_chars = string.ascii_letters + string.digits + " .,!?\"'-"
52
-
53
- # Clean the raw story by keeping only allowed characters
54
- clean_raw = ''.join(c for c in raw if c in allowed_chars)
55
-
56
- # Split into words and trim to 100 words
57
- words = clean_raw.split()
58
- story = " ".join(words[:100])
 
 
 
 
 
 
 
 
 
59
 
60
  st.write("**πŸ“– Your funny story: πŸ“–**")
61
  st.write(story)
@@ -81,7 +79,7 @@ uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "p
81
  if uploaded_image is None:
82
  st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here!", use_container_width=True)
83
  else:
84
- st.image(uploaded_image, caption="Your Picture 🌟", use_container_width=True)
85
 
86
  if st.button("Generate a story"):
87
  if uploaded_image is not None:
 
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, interesting children's story centered on this scene: {caption}\n"
35
+ f"Story in third-person narrative, describing this scene exactly: {caption} "
36
+ f"Mention the exact place, location, or venue within {caption}. "
37
+ f"Avoid numbers, random letter combinations, and single-letter words.")
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ # Generate raw story with optimized parameters
40
+ raw = storyer(
41
+ prompt,
42
+ max_new_tokens=100,
43
+ temperature=0.6,
44
+ top_p=0.85,
45
+ no_repeat_ngram_size=0,
46
+ return_full_text=False
47
+ )[0]["generated_text"].strip()
48
+
49
+ # Combine cleaning and word trimming in one step
50
+ # Use regex to keep only allowed characters and remove single-letter words
51
+ allowed_pattern = re.compile(r'[a-zA-Z0-9.,!?"\'-]+\b(?<!\b\w\b)')
52
+ clean_raw = ' '.join(word for word in re.findall(allowed_pattern, raw) if len(word) > 1)
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: πŸ“–**")
59
  st.write(story)
 
79
  if uploaded_image is None:
80
  st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here!", use_container_width=True)
81
  else:
82
+ st.image(uploaded_image, caption="Your Picture ", use_container_width=True)
83
 
84
  if st.button("Generate a story"):
85
  if uploaded_image is not None: