Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -173,120 +173,65 @@ st.markdown("""
|
|
173 |
# ======================================
|
174 |
# Main Application Interface
|
175 |
# ======================================
|
176 |
-
|
177 |
-
|
178 |
-
#
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
#
|
186 |
-
|
187 |
-
st.
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
199 |
|
200 |
-
# Create three columns for prompt buttons
|
201 |
-
col1, col2, col3 = st.columns(3)
|
202 |
with col1:
|
203 |
-
|
204 |
-
help="Generate educational story with life lessons",
|
205 |
-
key="edu_btn"):
|
206 |
-
st.session_state.selected_prompt = "educational"
|
207 |
-
st.session_state.confirmed = False
|
208 |
-
with col2:
|
209 |
-
if st.button("๐ Fantasy Adventure",
|
210 |
-
help="Create magical adventure story",
|
211 |
-
key="fantasy_btn"):
|
212 |
-
st.session_state.selected_prompt = "adventure"
|
213 |
-
st.session_state.confirmed = False
|
214 |
-
with col3:
|
215 |
-
if st.button("๐ป Animal Friends",
|
216 |
-
help="Make story about friendly animals",
|
217 |
-
key="animal_btn"):
|
218 |
-
st.session_state.selected_prompt = "animal"
|
219 |
-
st.session_state.confirmed = False
|
220 |
-
|
221 |
-
# Add confirmation button
|
222 |
-
with st.container():
|
223 |
-
st.subheader("Step 3: Confirm Selection")
|
224 |
-
if st.button("๐ฎ Start Magic Creation!",
|
225 |
-
help="Click to generate story after choosing style",
|
226 |
-
type="primary"):
|
227 |
-
st.session_state.confirmed = True
|
228 |
-
|
229 |
-
# Only show generation when confirmed
|
230 |
-
if st.session_state.get('confirmed', False):
|
231 |
-
# Generate image caption with loading state
|
232 |
-
with st.spinner("๐ Analyzing image and generating description..."):
|
233 |
-
image_caption = generate_image_caption(image)
|
234 |
-
|
235 |
-
# Display caption results using CSS class
|
236 |
-
st.subheader("๐ Image Understanding")
|
237 |
-
st.markdown(f'<div class="story-container image-caption">{image_caption}</div>',
|
238 |
-
unsafe_allow_html=True)
|
239 |
-
st.write("") # Add spacing
|
240 |
-
|
241 |
-
# Define prompt templates
|
242 |
-
PROMPT_TEMPLATES = {
|
243 |
-
"educational": {
|
244 |
-
"system": "You are a children's educator writer. Create a 100-word educational story about ",
|
245 |
-
"icon": "๐"
|
246 |
-
},
|
247 |
-
"adventure": {
|
248 |
-
"system": "You are a fantasy writer. Create a 100-word adventure story about ",
|
249 |
-
"icon": "๐ "
|
250 |
-
},
|
251 |
-
"animal": {
|
252 |
-
"system": "You are an animal themes writer. Create a 100-word animal fairy tales about ",
|
253 |
-
"icon": "๐ป"
|
254 |
-
}
|
255 |
-
}
|
256 |
-
|
257 |
-
# Safe access with default fallback
|
258 |
-
selected_prompt = st.session_state.get("selected_prompt", "educational")
|
259 |
|
260 |
-
#
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
5. Enjoy your personalized story and audio!
|
292 |
-
""")
|
|
|
173 |
# ======================================
|
174 |
# Main Application Interface
|
175 |
# ======================================
|
176 |
+
def main():
|
177 |
+
"""Main application interface for Streamlit"""
|
178 |
+
# Page configuration
|
179 |
+
st.set_page_config(
|
180 |
+
page_title="Fantasy Adventure Generator",
|
181 |
+
layout="wide",
|
182 |
+
initial_sidebar_state="collapsed"
|
183 |
+
)
|
184 |
+
|
185 |
+
# Title and description
|
186 |
+
st.title("๐งโโ๏ธ Fantasy Adventure Story Generator")
|
187 |
+
st.markdown("""
|
188 |
+
Upload an image and get:
|
189 |
+
- Automatic scene description
|
190 |
+
- AI-generated adventure story
|
191 |
+
- Audio version of the story
|
192 |
+
""")
|
193 |
+
|
194 |
+
# File uploader
|
195 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
196 |
+
|
197 |
+
if uploaded_file is not None:
|
198 |
+
# Process image
|
199 |
+
image = Image.open(uploaded_file).convert("RGB")
|
200 |
+
|
201 |
+
# Layout columns
|
202 |
+
col1, col2 = st.columns(2)
|
203 |
|
|
|
|
|
204 |
with col1:
|
205 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
+
# Generation button
|
208 |
+
if st.button("โจ Generate Story & Audio"):
|
209 |
+
with st.spinner("Processing your request..."):
|
210 |
+
# Generate outputs
|
211 |
+
caption = generate_image_caption(image)
|
212 |
+
sys_prompt = "You are a fantasy writer. Create a 100-word adventure story about "
|
213 |
+
story = generate_story_content(sys_prompt, caption)
|
214 |
+
speech = generate_audio_from_story(story)
|
215 |
+
|
216 |
+
# Display results
|
217 |
+
with col2:
|
218 |
+
st.subheader("๐ Scene Description")
|
219 |
+
st.write(caption)
|
220 |
+
|
221 |
+
st.subheader("๐ Generated Story")
|
222 |
+
st.write(story)
|
223 |
+
|
224 |
+
st.subheader("๐ Audio Playback")
|
225 |
+
st.audio(speech["audio"], format='audio/wav')
|
226 |
+
|
227 |
+
# Download button
|
228 |
+
with open(speech["audio"], "rb") as audio_file:
|
229 |
+
st.download_button(
|
230 |
+
label="๐ฅ Download Audio",
|
231 |
+
data=audio_file,
|
232 |
+
file_name="fantasy_adventure.wav",
|
233 |
+
mime="audio/wav"
|
234 |
+
)
|
235 |
+
|
236 |
+
if __name__ == "__main__":
|
237 |
+
main()
|
|
|
|