Update app.py
Browse files
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 |
-
#
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
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"
|