Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
# Generate raw story
|
40 |
-
raw = storyer(
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
)[0]["generated_text"].strip()
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
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 |
-
#
|
56 |
-
|
|
|
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 |
-
#
|
84 |
if uploaded_image is None:
|
85 |
-
st.image("https://
|
86 |
else:
|
87 |
-
st.image(uploaded_image, caption="Your Picture
|
88 |
|
89 |
if st.button("Generate a story"):
|
90 |
if uploaded_image is not None:
|
91 |
with st.spinner("Processing"):
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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!")
|