JSY8 commited on
Commit
c7bc4b8
Β·
verified Β·
1 Parent(s): be244f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
  import textwrap
@@ -8,6 +7,7 @@ import tempfile
8
  import os
9
  from PIL import Image
10
  import string
 
11
 
12
  # Initialize pipelines with caching
13
  @st.cache_resource
@@ -19,7 +19,11 @@ def load_pipelines():
19
 
20
  captioner, storyer, tts = load_pipelines()
21
 
22
- # Function part
 
 
 
 
23
  # Function to generate content from an image
24
  def generate_content(image):
25
  pil_image = Image.open(image)
@@ -29,36 +33,34 @@ 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 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
  def generate_story(raw, caption, tts):
55
- # Split into words and trim to 100 words
56
- words = raw.split()
 
57
  story = " ".join(words[:100])
58
 
59
- # Clean the story using clean_generated_story
60
- story = clean_generated_story(raw)
61
-
62
  # Display story in Streamlit
63
  st.write("**πŸ“– Your funny story: πŸ“–**")
64
  st.write(story)
@@ -75,23 +77,29 @@ def generate_story(raw, caption, tts):
75
  return caption, story, temp_file_path
76
 
77
  # Streamlit UI
78
- st.title("😎Story Maker")
79
  st.markdown("Upload a picture, I will generate a story for you")
80
 
81
  uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
82
 
83
- # Streamlit UI (modified image display section)
84
  if uploaded_image is None:
85
- st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here!", use_container_width=True)
86
  else:
87
- st.image(uploaded_image, caption="Your Picture ", use_container_width=True)
88
 
89
  if st.button("Generate a story"):
90
  if uploaded_image is not None:
91
  with st.spinner("Processing"):
92
- caption, story, audio_path = generate_content(uploaded_image)
93
- st.success(" Your story is ready!😊")
94
- st.audio(audio_path, format="audio/wav")
95
- os.remove(audio_path)
 
 
 
 
 
 
96
  else:
97
- st.warning("Please upload a picture first! ")
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  import textwrap
 
7
  import os
8
  from PIL import Image
9
  import string
10
+ import re
11
 
12
  # Initialize pipelines with caching
13
  @st.cache_resource
 
19
 
20
  captioner, storyer, tts = load_pipelines()
21
 
22
+ # Function to clean raw story
23
+ def clean_generated_story(raw):
24
+ allowed_pattern = re.compile(r'[a-zA-Z0-9.,!?"\'-]+\b(?<!\b\w\b)')
25
+ return ' '.join(word for word in re.findall(allowed_pattern, raw) if len(word) > 1)
26
+
27
  # Function to generate content from an image
28
  def generate_content(image):
29
  pil_image = Image.open(image)
 
33
  st.write("**🌟 What's in the picture: 🌟**")
34
  st.write(caption)
35
 
36
+ # Create prompt for story
37
+ prompt = (
38
+ f"Write a funny, interesting children's story centered on this scene: {caption}\n"
39
+ f"Story in third-person narrative, describing this scene exactly: {caption} "
40
+ f"Mention the exact place, location, or venue within {caption}. "
41
+ f"Avoid numbers, random letter combinations, and single-letter words.")
42
 
43
+ # Generate raw story
44
+ raw = storyer(
45
+ prompt,
46
+ max_new_tokens=100,
47
+ temperature=0.6,
48
+ top_p=0.85,
49
+ no_repeat_ngram_size=0,
50
+ return_full_text=False
51
+ )[0]["generated_text"].strip()
52
 
53
+ # Generate story and audio
54
+ caption, story, audio_path = generate_story(raw, caption, tts)
55
+ return caption, story, audio_path
 
56
 
57
+ # Function to generate story and audio
58
  def generate_story(raw, caption, tts):
59
+ # Clean and trim story
60
+ story = clean_generated_story(raw)
61
+ words = story.split()
62
  story = " ".join(words[:100])
63
 
 
 
 
64
  # Display story in Streamlit
65
  st.write("**πŸ“– Your funny story: πŸ“–**")
66
  st.write(story)
 
77
  return caption, story, temp_file_path
78
 
79
  # Streamlit UI
80
+ st.title("😎 Story Maker")
81
  st.markdown("Upload a picture, I will generate a story for you")
82
 
83
  uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
84
 
85
+ # Display image
86
  if uploaded_image is None:
87
+ st.image("https://via.placeholder.com/300", caption="Upload your picture here!", use_container_width=True)
88
  else:
89
+ st.image(uploaded_image, caption="Your Picture", use_container_width=True)
90
 
91
  if st.button("Generate a story"):
92
  if uploaded_image is not None:
93
  with st.spinner("Processing"):
94
+ try:
95
+ caption, story, audio_path = generate_content(uploaded_image)
96
+ st.success("Your story is ready! 😊")
97
+ st.audio(audio_path, format="audio/wav")
98
+ try:
99
+ os.remove(audio_path)
100
+ except OSError as e:
101
+ st.warning(f"Failed to delete temporary audio file: {e}")
102
+ except Exception as e:
103
+ st.error(f"Error generating story: {e}")
104
  else:
105
+ st.warning("Please upload a picture first!")