Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,188 +6,214 @@ from transformers import pipeline
|
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
# Cache resources to avoid reloading models on every interaction
|
| 11 |
@st.cache_resource
|
| 12 |
def get_image_captioner(model_name="Salesforce/blip-image-captioning-base"):
|
| 13 |
-
|
| 14 |
-
return pipeline("image-to-text", model=model_name, device="cpu")
|
| 15 |
|
| 16 |
@st.cache_resource
|
| 17 |
def get_story_pipe(model_name="google/flan-t5-base"):
|
| 18 |
-
|
| 19 |
-
return pipeline("text2text-generation", model=model_name, device="cpu")
|
| 20 |
|
| 21 |
@st.cache_resource
|
| 22 |
def get_tts_pipe(model_name="facebook/mms-tts-eng"):
|
| 23 |
-
|
| 24 |
-
return pipeline("text-to-speech", model=model_name, device="cpu")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
def part1_image_to_text(pil_img, captioner):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return results[0].get("generated_text", "") if results else ""
|
| 31 |
|
| 32 |
def part2_text_to_story(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
) -> str:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if not raw:
|
| 75 |
-
return ""
|
| 76 |
-
|
| 77 |
-
# Remove prompt echo if present
|
| 78 |
-
if raw.lower().startswith(prompt.lower()):
|
| 79 |
-
story = raw[len(prompt):].strip()
|
| 80 |
-
else:
|
| 81 |
-
story = raw
|
| 82 |
-
|
| 83 |
-
# Ensure story ends with proper punctuation
|
| 84 |
-
idx = story.rfind(".")
|
| 85 |
-
return story[:idx+1] if idx != -1 else story
|
| 86 |
|
| 87 |
def part3_text_to_speech_bytes(text: str, tts_pipe) -> bytes:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
return buffer.read()
|
| 110 |
-
|
| 111 |
-
# --- 3) STREAMLIT UI --------------------------------------------------------
|
| 112 |
-
# Configure page settings (must be first Streamlit command)
|
| 113 |
st.set_page_config(
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
)
|
| 118 |
|
| 119 |
-
# Custom CSS for
|
| 120 |
st.markdown("""
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
""", unsafe_allow_html=True)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
st.markdown("<div class='section-header'>Picture to Story Magic! β¨</div>",
|
| 135 |
-
unsafe_allow_html=True)
|
| 136 |
|
| 137 |
# Image upload section
|
| 138 |
with st.container():
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
pil_img = Image.open(uploaded)
|
| 152 |
-
st.image(pil_img, use_column_width=True)
|
| 153 |
-
|
| 154 |
-
# Caption generation section
|
| 155 |
with st.container():
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
f"</div>",
|
| 163 |
-
unsafe_allow_html=True
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
# Story generation and audio section
|
| 167 |
with st.container():
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
)
|
| 184 |
-
|
| 185 |
-
# Generate and display audio
|
| 186 |
-
tts_pipe = get_tts_pipe()
|
| 187 |
-
with st.spinner("Turning your story into sound..."):
|
| 188 |
-
audio_bytes = part3_text_to_speech_bytes(story, tts_pipe)
|
| 189 |
-
st.audio(audio_bytes, format="audio/wav")
|
| 190 |
-
|
| 191 |
-
# Success indicators
|
| 192 |
-
st.success("Yay! Your story is ready! π")
|
| 193 |
-
st.balloons()
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# βββ 1) MODEL LOADING (cached) ββββββββββββββββ
|
|
|
|
| 10 |
@st.cache_resource
|
| 11 |
def get_image_captioner(model_name="Salesforce/blip-image-captioning-base"):
|
| 12 |
+
return pipeline("image-to-text", model=model_name, device="cpu")
|
|
|
|
| 13 |
|
| 14 |
@st.cache_resource
|
| 15 |
def get_story_pipe(model_name="google/flan-t5-base"):
|
| 16 |
+
return pipeline("text2text-generation", model=model_name, device="cpu")
|
|
|
|
| 17 |
|
| 18 |
@st.cache_resource
|
| 19 |
def get_tts_pipe(model_name="facebook/mms-tts-eng"):
|
| 20 |
+
return pipeline("text-to-speech", model=model_name, device="cpu")
|
|
|
|
| 21 |
|
| 22 |
+
# βββ 2) TRANSFORM FUNCTIONS ββββββββββββββββ
|
| 23 |
def part1_image_to_text(pil_img, captioner):
|
| 24 |
+
results = captioner(pil_img)
|
| 25 |
+
return results[0].get("generated_text", "") if results else ""
|
|
|
|
| 26 |
|
| 27 |
def part2_text_to_story(
|
| 28 |
+
caption: str,
|
| 29 |
+
story_pipe,
|
| 30 |
+
target_words: int = 100,
|
| 31 |
+
max_length: int = 100,
|
| 32 |
+
min_length: int = 80,
|
| 33 |
+
do_sample: bool = True,
|
| 34 |
+
top_k: int = 100,
|
| 35 |
+
top_p: float= 0.9,
|
| 36 |
+
temperature: float= 0.7,
|
| 37 |
+
repetition_penalty: float = 1.1,
|
| 38 |
+
no_repeat_ngram_size: int = 4
|
| 39 |
) -> str:
|
| 40 |
+
prompt = (
|
| 41 |
+
f"Write a vivid, imaginative short story of about {target_words} words "
|
| 42 |
+
f"describing this scene: {caption}"
|
| 43 |
+
)
|
| 44 |
+
out = story_pipe(
|
| 45 |
+
prompt,
|
| 46 |
+
max_length=max_length,
|
| 47 |
+
min_length=min_length,
|
| 48 |
+
do_sample=do_sample,
|
| 49 |
+
top_k=top_k,
|
| 50 |
+
top_p=top_p,
|
| 51 |
+
temperature=temperature,
|
| 52 |
+
repetition_penalty=repetition_penalty,
|
| 53 |
+
no_repeat_ngram_size=no_repeat_ngram_size,
|
| 54 |
+
early_stopping=False
|
| 55 |
+
)
|
| 56 |
+
raw = out[0].get("generated_text", "").strip()
|
| 57 |
+
if not raw:
|
| 58 |
+
return ""
|
| 59 |
+
# strip echo of prompt
|
| 60 |
+
if raw.lower().startswith(prompt.lower()):
|
| 61 |
+
story = raw[len(prompt):].strip()
|
| 62 |
+
else:
|
| 63 |
+
story = raw
|
| 64 |
+
# cut at last full stop
|
| 65 |
+
idx = story.rfind(".")
|
| 66 |
+
if idx != -1:
|
| 67 |
+
story = story[:idx+1]
|
| 68 |
+
return story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def part3_text_to_speech_bytes(text: str, tts_pipe) -> bytes:
|
| 71 |
+
out = tts_pipe(text)
|
| 72 |
+
if isinstance(out, list):
|
| 73 |
+
out = out[0]
|
| 74 |
+
audio_array = out["audio"] # np.ndarray (channels, samples)
|
| 75 |
+
rate = out["sampling_rate"] # int
|
| 76 |
+
data = audio_array.T if audio_array.ndim == 2 else audio_array
|
| 77 |
+
pcm = (data * 32767).astype(np.int16)
|
| 78 |
+
|
| 79 |
+
buffer = io.BytesIO()
|
| 80 |
+
wf = wave.open(buffer, "wb")
|
| 81 |
+
channels = 1 if data.ndim == 1 else data.shape[1]
|
| 82 |
+
wf.setnchannels(channels)
|
| 83 |
+
wf.setsampwidth(2)
|
| 84 |
+
wf.setframerate(rate)
|
| 85 |
+
wf.writeframes(pcm.tobytes())
|
| 86 |
+
wf.close()
|
| 87 |
+
buffer.seek(0)
|
| 88 |
+
return buffer.read()
|
| 89 |
+
|
| 90 |
+
# βββ 3) STREAMLIT UI ββββββββββββββββββββββββββββ
|
| 91 |
+
# Set page config as the first Streamlit command
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
st.set_page_config(
|
| 93 |
+
page_title="Picture to Story Magic",
|
| 94 |
+
page_icon="β¨",
|
| 95 |
+
layout="centered"
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Custom CSS for kid-friendly styling with improved readability
|
| 99 |
st.markdown("""
|
| 100 |
+
<style>
|
| 101 |
+
.main {
|
| 102 |
+
background-color: #e6f3ff;
|
| 103 |
+
padding: 20px;
|
| 104 |
+
border-radius: 15px;
|
| 105 |
+
}
|
| 106 |
+
.stButton>button {
|
| 107 |
+
background-color: #ffcccb;
|
| 108 |
+
color: #000000; /* Black text */
|
| 109 |
+
border-radius: 10px;
|
| 110 |
+
border: 2px solid #ff9999;
|
| 111 |
+
font-size: 18px;
|
| 112 |
+
font-weight: bold;
|
| 113 |
+
padding: 10px 20px;
|
| 114 |
+
transition: all 0.3s;
|
| 115 |
+
}
|
| 116 |
+
.stButton>button:hover {
|
| 117 |
+
background-color: #ff9999;
|
| 118 |
+
color: #ffffff; /* White text on hover for contrast */
|
| 119 |
+
transform: scale(1.05);
|
| 120 |
+
}
|
| 121 |
+
.stFileUploader {
|
| 122 |
+
background-color: #ffb300; /* Darker yellow for better contrast with white label text */
|
| 123 |
+
border: 2px dashed #ff8c00; /* Darker orange border to match */
|
| 124 |
+
border-radius: 10px;
|
| 125 |
+
padding: 10px;
|
| 126 |
+
}
|
| 127 |
+
/* Style for the file uploader's inner text */
|
| 128 |
+
.stFileUploader div[role="button"] {
|
| 129 |
+
background-color: #f0f0f0; /* Very light gray background for contrast with black text */
|
| 130 |
+
border-radius: 10px;
|
| 131 |
+
padding: 10px;
|
| 132 |
+
}
|
| 133 |
+
.stFileUploader div[role="button"] > div {
|
| 134 |
+
color: #000000 !important; /* Black text */
|
| 135 |
+
font-size: 16px;
|
| 136 |
+
}
|
| 137 |
+
/* Style for the "Browse files" button inside the file uploader */
|
| 138 |
+
.stFileUploader button {
|
| 139 |
+
background-color: #ffca28 !important; /* Yellow button background */
|
| 140 |
+
color: #000000 !important; /* Black text */
|
| 141 |
+
border-radius: 8px !important;
|
| 142 |
+
border: 2px solid #ffb300 !important; /* Match the container background */
|
| 143 |
+
padding: 5px 15px !important;
|
| 144 |
+
font-weight: bold !important;
|
| 145 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.2) !important; /* Subtle shadow to make button stand out */
|
| 146 |
+
}
|
| 147 |
+
.stFileUploader button:hover {
|
| 148 |
+
background-color: #ff8c00 !important; /* Slightly darker yellow on hover */
|
| 149 |
+
color: #000000 !important; /* Keep black text */
|
| 150 |
+
}
|
| 151 |
+
.stImage {
|
| 152 |
+
border: 3px solid #81c784;
|
| 153 |
+
border-radius: 10px;
|
| 154 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 155 |
+
}
|
| 156 |
+
.section-header {
|
| 157 |
+
background-color: #b3e5fc;
|
| 158 |
+
padding: 10px;
|
| 159 |
+
border-radius: 10px;
|
| 160 |
+
text-align: center;
|
| 161 |
+
font-size: 24px;
|
| 162 |
+
font-weight: bold;
|
| 163 |
+
color: #000000; /* Black text */
|
| 164 |
+
margin-bottom: 10px;
|
| 165 |
+
}
|
| 166 |
+
.caption-box, .story-box {
|
| 167 |
+
background-color: #f0f4c3;
|
| 168 |
+
padding: 15px;
|
| 169 |
+
border-radius: 10px;
|
| 170 |
+
border: 2px solid #d4e157;
|
| 171 |
+
margin-bottom: 20px;
|
| 172 |
+
color: #000000; /* Black text */
|
| 173 |
+
}
|
| 174 |
+
.caption-box b, .story-box b {
|
| 175 |
+
color: #000000; /* Black text for bold headers */
|
| 176 |
+
}
|
| 177 |
+
</style>
|
| 178 |
""", unsafe_allow_html=True)
|
| 179 |
|
| 180 |
+
# Main title
|
| 181 |
+
st.markdown("<div class='section-header'>Picture to Story Magic! β¨</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Image upload section
|
| 184 |
with st.container():
|
| 185 |
+
st.markdown("<div class='section-header'>1οΈβ£ Pick a Fun Picture! πΌοΈ</div>", unsafe_allow_html=True)
|
| 186 |
+
uploaded = st.file_uploader("Choose a picture to start the magic! π", type=["jpg","jpeg","png"])
|
| 187 |
+
if not uploaded:
|
| 188 |
+
st.info("Upload a picture, and let's make a story! π")
|
| 189 |
+
st.stop()
|
| 190 |
+
|
| 191 |
+
# Show image
|
| 192 |
+
with st.spinner("Looking at your picture..."):
|
| 193 |
+
pil_img = Image.open(uploaded)
|
| 194 |
+
st.image(pil_img, use_container_width=True)
|
| 195 |
+
|
| 196 |
+
# Caption section
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
with st.container():
|
| 198 |
+
captioner = get_image_captioner()
|
| 199 |
+
with st.spinner("Figuring out what's in your picture..."):
|
| 200 |
+
caption = part1_image_to_text(pil_img, captioner)
|
| 201 |
+
st.markdown(f"<div class='caption-box'><b>What's in the Picture? π§</b><br>{caption}</div>", unsafe_allow_html=True)
|
| 202 |
+
|
| 203 |
+
# Story and audio section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
with st.container():
|
| 205 |
+
st.markdown("<div class='section-header'>2οΈβ£ Make a Story and Hear It! π΅</div>", unsafe_allow_html=True)
|
| 206 |
+
if st.button("Create My Story! π"):
|
| 207 |
+
# Story
|
| 208 |
+
story_pipe = get_story_pipe()
|
| 209 |
+
with st.spinner("Writing a super cool story..."):
|
| 210 |
+
story = part2_text_to_story(caption, story_pipe)
|
| 211 |
+
st.markdown(f"<div class='story-box'><b>Your Cool Story! π</b><br>{story}</div>", unsafe_allow_html=True)
|
| 212 |
+
|
| 213 |
+
# TTS
|
| 214 |
+
tts_pipe = get_tts_pipe()
|
| 215 |
+
with st.spinner("Turning your story into sound..."):
|
| 216 |
+
audio_bytes = part3_text_to_speech_bytes(story, tts_pipe)
|
| 217 |
+
st.audio(audio_bytes, format="audio/wav")
|
| 218 |
+
st.success("Yay! Your story is ready! π")
|
| 219 |
+
st.balloons() # Fun animation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|