adi-123 commited on
Commit
f366ef4
·
verified ·
1 Parent(s): c4fd83b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -148
app.py CHANGED
@@ -10,95 +10,50 @@ from email.mime.multipart import MIMEMultipart
10
  from email.mime.text import MIMEText
11
  from email.mime.audio import MIMEAudio
12
 
13
- #from dotenv import load_dotenv
14
- # Load environment variables
15
- #load_dotenv()
16
-
17
-
18
- # Image-to-text function
19
  def img2txt(url: str) -> str:
20
- print("Initializing captioning model...")
21
  captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
22
-
23
- print("Generating text from the image...")
24
  text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
25
-
26
- print(text)
27
  return text
28
 
29
- # Text-to-story generation function
30
  def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
31
  client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
32
-
33
  story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}"
34
-
35
  stream = client.chat.completions.create(
36
  model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
37
- messages=[
38
- {"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
39
- Ensure the story does not exceed 250 words.'''},
40
- {"role": "user", "content": story_prompt}
41
- ],
42
  top_k=top_k,
43
  top_p=top_p,
44
  temperature=temperature,
45
  stream=True
46
  )
47
-
48
- story = ''
49
- for chunk in stream:
50
- story += chunk.choices[0].delta.content
51
-
52
  return story
53
 
54
- # Text-to-speech function
55
  def txt2speech(text: str) -> None:
56
- print("Converting text to speech using gTTS...")
57
  tts = gTTS(text=text, lang='en')
58
  tts.save("audio_story.mp3")
59
 
60
- # Get user preferences function
61
- def get_user_preferences() -> Dict[str, str]:
62
- preferences = {
63
- 'continent': st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]),
64
- 'genre': st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]),
65
- 'setting': st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]),
66
- 'plot': st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]),
67
- 'tone': st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]),
68
- 'theme': st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]),
69
- 'conflict': st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]),
70
- 'twist': st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]),
71
- 'ending': st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
72
- }
73
- return preferences
74
-
75
- # Function to send the story via email using smtplib
76
  def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
77
  try:
78
- # Email configuration
79
- smtp_server = os.environ.get("SMTP_SERVER") # e.g., "smtp.gmail.com"
80
- smtp_port = int(os.environ.get("SMTP_PORT", 587)) # e.g., 587 for TLS
81
- sender_email = os.environ.get("SENDER_EMAIL") # Your email
82
- sender_password = os.environ.get("SENDER_PASSWORD") # Your email password
83
-
84
- # Create the email
85
  msg = MIMEMultipart()
86
  msg['From'] = sender_email
87
  msg['To'] = recipient_email
88
  msg['Subject'] = "Your Generated Story"
89
-
90
- # Attach the story text
91
  msg.attach(MIMEText(f"Here's your generated story:\n\n{story_text}\n\nEnjoy!", 'plain'))
92
 
93
- # Attach the audio file
94
  with open(audio_file_path, 'rb') as audio_file:
95
  audio_part = MIMEAudio(audio_file.read(), _subtype='mp3')
96
  audio_part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(audio_file_path))
97
  msg.attach(audio_part)
98
 
99
- # Send the email
100
  with smtplib.SMTP(smtp_server, smtp_port) as server:
101
- server.starttls() # Upgrade to a secure connection
102
  server.login(sender_email, sender_password)
103
  server.send_message(msg)
104
 
@@ -108,113 +63,39 @@ def send_story_email(recipient_email: str, story_text: str, audio_file_path: str
108
  print(f"Error sending email: {str(e)}")
109
  return False
110
 
111
- # Basic email validation function
112
  def validate_email(email: str) -> bool:
113
  pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
114
  return re.match(pattern, email) is not None
115
 
116
- # Main Streamlit application
117
  def main():
118
- st.set_page_config(
119
- page_title="🎨 Image-to-Audio Story 🎧",
120
- page_icon="🖼️",
121
- layout="wide"
122
- )
123
  st.title("Turn the Image into Audio Story")
124
 
125
- # Initialize session state variables
126
- if "story" not in st.session_state:
127
- st.session_state.story = ""
128
- if "audio_file_path" not in st.session_state:
129
- st.session_state.audio_file_path = ""
130
- if "caption" not in st.session_state:
131
- st.session_state.caption = ""
132
-
133
- # Main content area
134
- col1, col2 = st.columns([2, 3])
135
-
136
- with col1:
137
- # Image upload section
138
- st.markdown("## 📷 Upload Image")
139
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
140
-
141
- # Story preferences section
142
- st.markdown("## 🎭 Story Preferences")
143
- preferences = get_user_preferences()
144
-
145
- with col2:
146
- if uploaded_file is not None:
147
- # Display uploaded image
148
- st.markdown("## 🖼️ Your Image")
149
- bytes_data = uploaded_file.read()
150
- with open("uploaded_image.jpg", "wb") as file:
151
- file.write(bytes_data)
152
- st.image(uploaded_file, use_column_width=True)
153
-
154
- # Process image and generate story
155
- if st.button("🎨 Generate Story"):
156
- with st.spinner("🤖 AI is working its magic..."):
157
- try:
158
- # Get image description
159
- scenario = img2txt("uploaded_image.jpg")
160
- st.session_state.caption = scenario # Store caption in session state
161
-
162
- # Create story prompt
163
- prompt = f"""Based on the image description: '{scenario}',
164
- create a {preferences['genre']} story set in {preferences['setting']}
165
- in {preferences['continent']}. The story should have a {preferences['tone']}
166
- tone and explore the theme of {preferences['theme']}. The main conflict
167
- should be {preferences['conflict']}. The story should have a {preferences['twist']}
168
- and end with a {preferences['ending']} ending."""
169
-
170
- # Generate story
171
- story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
172
- st.session_state.story = story # Store story in session state
173
-
174
- # Convert to audio
175
- txt2speech(story)
176
- st.session_state.audio_file_path = "audio_story.mp3" # Store audio path in session state
177
 
178
- except Exception as e:
179
- st.error(f"An error occurred: {str(e)}")
180
- st.warning("Please try again or contact support if the problem persists.")
 
 
 
 
 
181
 
182
- # Display results if story exists in session state
183
- if st.session_state.story:
184
- st.markdown("---")
185
-
186
- # Image caption
187
- with st.expander("📜 Image Caption", expanded=True):
188
- st.write(st.session_state.caption)
189
-
190
- # Story text
191
- with st.expander("📖 Generated Story", expanded=True):
192
- st.write(st.session_state.story)
193
 
194
- # Audio player
195
- with st.expander("🎧 Audio Version", expanded=True):
196
- st.audio(st.session_state.audio_file_path)
197
-
198
- # Email section
199
- st.markdown("---")
200
- st.markdown("## 📧 Get Story via Email")
201
- email = st.text_input(
202
- "Enter your email address:",
203
- help="We'll send you the story text and audio file"
204
- )
205
 
 
206
  if st.button("📤 Send to Email"):
207
- if not email:
208
- st.warning("Please enter an email address.")
209
- elif not validate_email(email):
210
- st.error("Please enter a valid email address.")
 
211
  else:
212
- print('The process is ongoing________________________')
213
- with st.spinner("📨 Sending email..."):
214
- if send_story_email(email, st.session_state.story, st.session_state.audio_file_path):
215
- st.success(f"Email sent to: {email}")
216
- else:
217
- st.error("❌ Failed to send email. Please try again.")
218
 
219
  if __name__ == '__main__':
220
  main()
 
10
  from email.mime.text import MIMEText
11
  from email.mime.audio import MIMEAudio
12
 
 
 
 
 
 
 
13
  def img2txt(url: str) -> str:
 
14
  captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
 
 
15
  text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
 
 
16
  return text
17
 
 
18
  def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
19
  client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
 
20
  story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}"
 
21
  stream = client.chat.completions.create(
22
  model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
23
+ messages=[{"role": "user", "content": story_prompt}],
 
 
 
 
24
  top_k=top_k,
25
  top_p=top_p,
26
  temperature=temperature,
27
  stream=True
28
  )
29
+ story = ''.join(chunk.choices[0].delta.content for chunk in stream)
 
 
 
 
30
  return story
31
 
 
32
  def txt2speech(text: str) -> None:
 
33
  tts = gTTS(text=text, lang='en')
34
  tts.save("audio_story.mp3")
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
37
  try:
38
+ smtp_server = os.environ.get("SMTP_SERVER")
39
+ smtp_port = int(os.environ.get("SMTP_PORT", 587))
40
+ sender_email = os.environ.get("SENDER_EMAIL")
41
+ sender_password = os.environ.get("SENDER_PASSWORD")
42
+
 
 
43
  msg = MIMEMultipart()
44
  msg['From'] = sender_email
45
  msg['To'] = recipient_email
46
  msg['Subject'] = "Your Generated Story"
47
+
 
48
  msg.attach(MIMEText(f"Here's your generated story:\n\n{story_text}\n\nEnjoy!", 'plain'))
49
 
 
50
  with open(audio_file_path, 'rb') as audio_file:
51
  audio_part = MIMEAudio(audio_file.read(), _subtype='mp3')
52
  audio_part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(audio_file_path))
53
  msg.attach(audio_part)
54
 
 
55
  with smtplib.SMTP(smtp_server, smtp_port) as server:
56
+ server.starttls()
57
  server.login(sender_email, sender_password)
58
  server.send_message(msg)
59
 
 
63
  print(f"Error sending email: {str(e)}")
64
  return False
65
 
 
66
  def validate_email(email: str) -> bool:
67
  pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
68
  return re.match(pattern, email) is not None
69
 
 
70
  def main():
71
+ st.set_page_config(page_title="🎨 Image-to-Audio Story 🎧", layout="wide")
72
+
 
 
 
73
  st.title("Turn the Image into Audio Story")
74
 
75
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
+ if uploaded_file is not None:
78
+ st.image(uploaded_file)
79
+
80
+ if st.button("🎨 Generate Story"):
81
+ scenario = img2txt(uploaded_file) # Process the uploaded image
82
+ prompt = f"Create a story based on: {scenario}"
83
+ story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5)
84
+ txt2speech(story)
85
 
86
+ st.session_state.story = story
 
 
 
 
 
 
 
 
 
 
87
 
88
+ st.audio("audio_story.mp3") # Use Streamlit's audio player
 
 
 
 
 
 
 
 
 
 
89
 
90
+ email = st.text_input("Enter your email address:")
91
  if st.button("📤 Send to Email"):
92
+ if validate_email(email):
93
+ if send_story_email(email, story, "audio_story.mp3"):
94
+ st.success(f"Email sent to: {email}")
95
+ else:
96
+ st.error("❌ Failed to send email.")
97
  else:
98
+ st.error("Please enter a valid email address.")
 
 
 
 
 
99
 
100
  if __name__ == '__main__':
101
  main()