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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -29
app.py CHANGED
@@ -9,11 +9,20 @@ from together import Together
9
  from email.mime.multipart import MIMEMultipart
10
  from email.mime.text import MIMEText
11
  from email.mime.audio import MIMEAudio
 
 
 
 
12
 
13
  # Image-to-text function
14
  def img2txt(url: str) -> str:
 
15
  captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
 
16
  text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
 
 
17
  return text
18
 
19
  # Text-to-story generation function
@@ -43,6 +52,7 @@ def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
43
 
44
  # Text-to-speech function
45
  def txt2speech(text: str) -> None:
 
46
  tts = gTTS(text=text, lang='en')
47
  tts.save("audio_story.mp3")
48
 
@@ -61,6 +71,47 @@ def get_user_preferences() -> Dict[str, str]:
61
  }
62
  return preferences
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # Main Streamlit application
65
  def main():
66
  st.set_page_config(
@@ -92,6 +143,10 @@ def main():
92
 
93
  with col2:
94
  if uploaded_file is not None:
 
 
 
 
95
  # Display uploaded image
96
  st.markdown("## πŸ–ΌοΈ Your Image")
97
  bytes_data = uploaded_file.read()
@@ -99,43 +154,53 @@ def main():
99
  file.write(bytes_data)
100
  st.image(uploaded_file, use_column_width=True)
101
 
102
- # Process image and generate story
103
- if st.button("🎨 Generate Story"):
104
  try:
105
- # Step 1: Generate Image Caption
106
- with st.spinner("πŸ” Generating Image Caption..."):
107
- scenario = img2txt("uploaded_image.jpg")
108
- st.session_state.caption = scenario # Store caption in session state
109
-
110
- # Step 2: Generate Story
111
- with st.spinner("πŸ“ Generating Story..."):
112
- prompt = f"""Based on the image description: '{scenario}',
113
- create a {preferences['genre']} story set in {preferences['setting']}
114
- in {preferences['continent']}. The story should have a {preferences['tone']}
115
- tone and explore the theme of {preferences['theme']}. The main conflict
116
- should be {preferences['conflict']}. The story should have a {preferences['twist']}
117
- and end with a {preferences['ending']} ending."""
118
-
119
- story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
120
- st.session_state.story = story # Store story in session state
121
-
122
- # Step 3: Generate Audio Version
123
- with st.spinner("🎧 Generating Audio Version..."):
124
- txt2speech(story)
125
- st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state
126
-
127
  except Exception as e:
128
- st.error(f"An error occurred: {str(e)}")
129
  st.warning("Please try again or contact support if the problem persists.")
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  # Display results if story exists in session state
132
  if st.session_state.story:
133
  st.markdown("---")
134
 
135
- # Image caption
136
- with st.expander("πŸ“œ Image Caption", expanded=True):
137
- st.write(st.session_state.caption)
138
-
139
  # Story text
140
  with st.expander("πŸ“– Generated Story", expanded=True):
141
  st.write(st.session_state.story)
@@ -144,5 +209,25 @@ def main():
144
  with st.expander("🎧 Audio Version", expanded=True):
145
  st.audio(st.session_state.audio_file_path)
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  if __name__ == '__main__':
148
  main()
 
9
  from email.mime.multipart import MIMEMultipart
10
  from email.mime.text import MIMEText
11
  from email.mime.audio import MIMEAudio
12
+ #from dotenv import load_dotenv
13
+
14
+ # Load environment variables
15
+ #load_dotenv()
16
 
17
  # Image-to-text function
18
  def img2txt(url: str) -> str:
19
+ print("Initializing captioning model...")
20
  captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
21
+
22
+ print("Generating text from the image...")
23
  text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
24
+
25
+ print(text)
26
  return text
27
 
28
  # Text-to-story generation function
 
52
 
53
  # Text-to-speech function
54
  def txt2speech(text: str) -> None:
55
+ print("Converting text to speech using gTTS...")
56
  tts = gTTS(text=text, lang='en')
57
  tts.save("audio_story.mp3")
58
 
 
71
  }
72
  return preferences
73
 
74
+ # Function to send the story via email using smtplib
75
+ def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
76
+ try:
77
+ # Email configuration
78
+ smtp_server = os.environ.get("SMTP_SERVER") # e.g., "smtp.gmail.com"
79
+ smtp_port = int(os.environ.get("SMTP_PORT", 587)) # e.g., 587 for TLS
80
+ sender_email = os.environ.get("SENDER_EMAIL") # Your email
81
+ sender_password = os.environ.get("SENDER_PASSWORD") # Your email password
82
+
83
+ # Create the email
84
+ msg = MIMEMultipart()
85
+ msg['From'] = sender_email
86
+ msg['To'] = recipient_email
87
+ msg['Subject'] = "Your Generated Story"
88
+
89
+ # Attach the story text
90
+ msg.attach(MIMEText(f"Here's your generated story:\n\n{story_text}\n\nEnjoy!", 'plain'))
91
+
92
+ # Attach the audio file
93
+ with open(audio_file_path, 'rb') as audio_file:
94
+ audio_part = MIMEAudio(audio_file.read(), _subtype='mp3')
95
+ audio_part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(audio_file_path))
96
+ msg.attach(audio_part)
97
+
98
+ # Send the email
99
+ with smtplib.SMTP(smtp_server, smtp_port) as server:
100
+ server.starttls() # Upgrade to a secure connection
101
+ server.login(sender_email, sender_password)
102
+ server.send_message(msg)
103
+
104
+ return True
105
+
106
+ except Exception as e:
107
+ print(f"Error sending email: {str(e)}")
108
+ return False
109
+
110
+ # Basic email validation function
111
+ def validate_email(email: str) -> bool:
112
+ pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
113
+ return re.match(pattern, email) is not None
114
+
115
  # Main Streamlit application
116
  def main():
117
  st.set_page_config(
 
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
  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)
 
209
  with st.expander("🎧 Audio Version", expanded=True):
210
  st.audio(st.session_state.audio_file_path)
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"
218
+ )
219
+
220
+ if st.button("πŸ“€ Send to Email"):
221
+ if not email:
222
+ st.warning("Please enter an email address.")
223
+ elif not validate_email(email):
224
+ st.error("Please enter a valid email address.")
225
+ else:
226
+ with st.spinner("πŸ“¨ Sending email..."):
227
+ if send_story_email(email, st.session_state.story, st.session_state.audio_file_path):
228
+ st.success(f"Email sent to: {email}")
229
+ else:
230
+ st.error("❌ Failed to send email. Please try again.")
231
+
232
  if __name__ == '__main__':
233
  main()