adi-123 commited on
Commit
073629f
Β·
verified Β·
1 Parent(s): ce36df3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -35
app.py CHANGED
@@ -17,6 +17,14 @@ def main():
17
  )
18
  st.title("Turn the Image into Audio Story")
19
 
 
 
 
 
 
 
 
 
20
  # Main content area
21
  col1, col2 = st.columns([2, 3])
22
 
@@ -47,6 +55,7 @@ def main():
47
  try:
48
  # Get image description
49
  scenario = img2txt("uploaded_image.jpg")
 
50
 
51
  # Create story prompt
52
  prompt = f"""Based on the image description: '{scenario}',
@@ -58,48 +67,51 @@ def main():
58
 
59
  # Generate story
60
  story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
 
61
 
62
  # Convert to audio
63
  txt2speech(story)
64
-
65
- # Display results
66
- st.markdown("---")
67
-
68
- # Image caption
69
- with st.expander("πŸ“œ Image Caption", expanded=True):
70
- st.write(scenario)
71
-
72
- # Story text
73
- with st.expander("πŸ“– Generated Story", expanded=True):
74
- st.write(story)
75
-
76
- # Audio player
77
- with st.expander("🎧 Audio Version", expanded=True):
78
- st.audio("audio_story.mp3")
79
-
80
- # Email section
81
- st.markdown("---")
82
- st.markdown("## πŸ“§ Get Story via Email")
83
- email = st.text_input(
84
- "Enter your email address:",
85
- help="We'll send you the story text and audio file"
86
- )
87
-
88
- if st.button("πŸ“€ Send to Email"):
89
- if not email:
90
- st.warning("Please enter an email address.")
91
- elif not validate_email(email):
92
- st.error("Please enter a valid email address.")
93
- else:
94
- with st.spinner("πŸ“¨ Sending email..."):
95
- if send_story_email(email, story, "audio_story.mp3"):
96
- st.success("βœ‰οΈ Story sent successfully! Check your email.")
97
- else:
98
- st.error("❌ Failed to send email. Please try again.")
99
 
100
  except Exception as e:
101
  st.error(f"An error occurred: {str(e)}")
102
  st.warning("Please try again or contact support if the problem persists.")
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  if __name__ == '__main__':
105
  main()
 
17
  )
18
  st.title("Turn the Image into Audio Story")
19
 
20
+ # Initialize session state variables
21
+ if "story" not in st.session_state:
22
+ st.session_state.story = ""
23
+ if "audio_file_path" not in st.session_state:
24
+ st.session_state.audio_file_path = ""
25
+ if "caption" not in st.session_state:
26
+ st.session_state.caption = ""
27
+
28
  # Main content area
29
  col1, col2 = st.columns([2, 3])
30
 
 
55
  try:
56
  # Get image description
57
  scenario = img2txt("uploaded_image.jpg")
58
+ st.session_state.caption = scenario # Store caption in session state
59
 
60
  # Create story prompt
61
  prompt = f"""Based on the image description: '{scenario}',
 
67
 
68
  # Generate story
69
  story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
70
+ st.session_state.story = story # Store story in session state
71
 
72
  # Convert to audio
73
  txt2speech(story)
74
+ st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  except Exception as e:
77
  st.error(f"An error occurred: {str(e)}")
78
  st.warning("Please try again or contact support if the problem persists.")
79
 
80
+ # Display results if story exists in session state
81
+ if st.session_state.story:
82
+ st.markdown("---")
83
+
84
+ # Image caption
85
+ with st.expander("πŸ“œ Image Caption", expanded=True):
86
+ st.write(st.session_state.caption)
87
+
88
+ # Story text
89
+ with st.expander("πŸ“– Generated Story", expanded=True):
90
+ st.write(st.session_state.story)
91
+
92
+ # Audio player
93
+ with st.expander("🎧 Audio Version", expanded=True):
94
+ st.audio(st.session_state.audio_file_path)
95
+
96
+ # Email section
97
+ st.markdown("---")
98
+ st.markdown("## πŸ“§ Get Story via Email")
99
+ email = st.text_input(
100
+ "Enter your email address:",
101
+ help="We'll send you the story text and audio file"
102
+ )
103
+
104
+ if st.button("πŸ“€ Send to Email"):
105
+ if not email:
106
+ st.warning("Please enter an email address.")
107
+ elif not validate_email(email):
108
+ st.error("Please enter a valid email address.")
109
+ else:
110
+ with st.spinner("πŸ“¨ Sending email..."):
111
+ if send_story_email(email, st.session_state.story, st.session_state.audio_file_path):
112
+ st.success("βœ‰οΈ Story sent successfully! Check your email.")
113
+ else:
114
+ st.error("❌ Failed to send email. Please try again.")
115
+
116
  if __name__ == '__main__':
117
  main()