adi-123 commited on
Commit
5838dbf
Β·
verified Β·
1 Parent(s): fd6e145

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -47
app.py CHANGED
@@ -143,10 +143,6 @@ def main():
143
 
144
  with col2:
145
  if uploaded_file is not None:
146
- # Reset session state variables when a new image is uploaded
147
- st.session_state.story = ""
148
- st.session_state.audio_file_path = ""
149
-
150
  # Display uploaded image
151
  st.markdown("## πŸ–ΌοΈ Your Image")
152
  bytes_data = uploaded_file.read()
@@ -154,53 +150,42 @@ def main():
154
  file.write(bytes_data)
155
  st.image(uploaded_file, use_column_width=True)
156
 
157
- # Generate image caption as soon as image is uploaded
158
- with st.spinner("πŸ“ Generating image caption..."):
159
- try:
160
- scenario = img2txt("uploaded_image.jpg")
161
- st.session_state.caption = scenario # Store caption in session state
162
- st.success("Image caption generated.")
163
- except Exception as e:
164
- st.error(f"An error occurred while generating image caption: {str(e)}")
165
- st.warning("Please try again or contact support if the problem persists.")
166
-
167
- # Display image caption
168
- if st.session_state.caption:
169
- st.markdown("## πŸ“œ Image Caption")
170
- st.write(st.session_state.caption)
171
-
172
- # Generate Story button
173
- if st.session_state.caption:
174
- if st.button("🎨 Generate Story"):
175
- with st.spinner("πŸ“ Generating story..."):
176
- try:
177
- # Create story prompt using st.session_state.caption
178
- prompt = f"""Based on the image description: '{st.session_state.caption}',
179
- create a {preferences['genre']} story set in {preferences['setting']}
180
- in {preferences['continent']}. The story should have a {preferences['tone']}
181
- tone and explore the theme of {preferences['theme']}. The main conflict
182
- should be {preferences['conflict']}. The story should have a {preferences['twist']}
183
- and end with a {preferences['ending']} ending."""
184
-
185
- # Generate story
186
- story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
187
- st.session_state.story = story # Store story in session state
188
- st.success("Story generated.")
189
-
190
- # Convert to audio
191
- with st.spinner("πŸ”Š Generating audio story..."):
192
- txt2speech(story)
193
- st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state
194
- st.success("Audio story generated.")
195
-
196
- except Exception as e:
197
- st.error(f"An error occurred: {str(e)}")
198
- st.warning("Please try again or contact support if the problem persists.")
199
 
200
  # Display results if story exists in session state
201
  if st.session_state.story:
202
  st.markdown("---")
203
 
 
 
 
 
204
  # Story text
205
  with st.expander("πŸ“– Generated Story", expanded=True):
206
  st.write(st.session_state.story)
@@ -211,7 +196,7 @@ def main():
211
 
212
  # Email section
213
  st.markdown("---")
214
- st.markdown("## πŸ“§ Get Story via Email")
215
  email = st.text_input(
216
  "Enter your email address:",
217
  help="We'll send you the story text and audio file"
 
143
 
144
  with col2:
145
  if uploaded_file is not None:
 
 
 
 
146
  # Display uploaded image
147
  st.markdown("## πŸ–ΌοΈ Your Image")
148
  bytes_data = uploaded_file.read()
 
150
  file.write(bytes_data)
151
  st.image(uploaded_file, use_column_width=True)
152
 
153
+ # Process image and generate story
154
+ if st.button("🎨 Generate Story"):
155
+ with st.spinner("πŸ€– AI is working its magic..."):
156
+ try:
157
+ # Get image description
158
+ scenario = img2txt("uploaded_image.jpg")
159
+ st.session_state.caption = scenario # Store caption in session state
160
+
161
+ # Create story prompt
162
+ prompt = f"""Based on the image description: '{scenario}',
163
+ create a {preferences['genre']} story set in {preferences['setting']}
164
+ in {preferences['continent']}. The story should have a {preferences['tone']}
165
+ tone and explore the theme of {preferences['theme']}. The main conflict
166
+ should be {preferences['conflict']}. The story should have a {preferences['twist']}
167
+ and end with a {preferences['ending']} ending."""
168
+
169
+ # Generate story
170
+ story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
171
+ st.session_state.story = story # Store story in session state
172
+
173
+ # Convert to audio
174
+ txt2speech(story)
175
+ st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state
176
+
177
+ except Exception as e:
178
+ st.error(f"An error occurred: {str(e)}")
179
+ st.warning("Please try again or contact support if the problem persists.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  # Display results if story exists in session state
182
  if st.session_state.story:
183
  st.markdown("---")
184
 
185
+ # Image caption
186
+ with st.expander("πŸ“œ Image Caption", expanded=True):
187
+ st.write(st.session_state.caption)
188
+
189
  # Story text
190
  with st.expander("πŸ“– Generated Story", expanded=True):
191
  st.write(st.session_state.story)
 
196
 
197
  # Email section
198
  st.markdown("---")
199
+ st.markdown("## πŸ“§ Get Story via Email - RUN LOCALLY")
200
  email = st.text_input(
201
  "Enter your email address:",
202
  help="We'll send you the story text and audio file"