Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,118 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import streamlit as st
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def main():
|
13 |
st.set_page_config(
|
14 |
page_title="π¨ Image-to-Audio Story π§",
|
@@ -31,10 +135,7 @@ def main():
|
|
31 |
with col1:
|
32 |
# Image upload section
|
33 |
st.markdown("## π· Upload Image")
|
34 |
-
uploaded_file = st.file_uploader(
|
35 |
-
"Choose an image...",
|
36 |
-
type=["jpg", "jpeg", "png"]
|
37 |
-
)
|
38 |
|
39 |
# Story preferences section
|
40 |
st.markdown("## π Story Preferences")
|
@@ -107,9 +208,10 @@ def main():
|
|
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("
|
113 |
else:
|
114 |
st.error("β Failed to send email. Please try again.")
|
115 |
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
+
import smtplib
|
4 |
import streamlit as st
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from transformers import pipeline
|
7 |
+
from typing import Dict
|
8 |
+
from gtts import gTTS
|
9 |
+
from together import Together
|
10 |
+
from email.mime.multipart import MIMEMultipart
|
11 |
+
from email.mime.text import MIMEText
|
12 |
+
from email.mime.audio import MIMEAudio
|
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
|
29 |
+
def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
|
30 |
+
client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
|
31 |
+
|
32 |
+
story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}"
|
33 |
+
|
34 |
+
stream = client.chat.completions.create(
|
35 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
|
38 |
+
Ensure the story does not exceed 250 words.'''},
|
39 |
+
{"role": "user", "content": story_prompt}
|
40 |
+
],
|
41 |
+
top_k=top_k,
|
42 |
+
top_p=top_p,
|
43 |
+
temperature=temperature,
|
44 |
+
stream=True
|
45 |
+
)
|
46 |
+
|
47 |
+
story = ''
|
48 |
+
for chunk in stream:
|
49 |
+
story += chunk.choices[0].delta.content
|
50 |
+
|
51 |
+
return story
|
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 |
+
|
59 |
+
# Get user preferences function
|
60 |
+
def get_user_preferences() -> Dict[str, str]:
|
61 |
+
preferences = {
|
62 |
+
'continent': st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]),
|
63 |
+
'genre': st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]),
|
64 |
+
'setting': st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]),
|
65 |
+
'plot': st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]),
|
66 |
+
'tone': st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]),
|
67 |
+
'theme': st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]),
|
68 |
+
'conflict': st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]),
|
69 |
+
'twist': st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]),
|
70 |
+
'ending': st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
|
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(
|
118 |
page_title="π¨ Image-to-Audio Story π§",
|
|
|
135 |
with col1:
|
136 |
# Image upload section
|
137 |
st.markdown("## π· Upload Image")
|
138 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
139 |
|
140 |
# Story preferences section
|
141 |
st.markdown("## π Story Preferences")
|
|
|
208 |
elif not validate_email(email):
|
209 |
st.error("Please enter a valid email address.")
|
210 |
else:
|
211 |
+
print('The process is ongoing________________________')
|
212 |
with st.spinner("π¨ Sending email..."):
|
213 |
if send_story_email(email, st.session_state.story, st.session_state.audio_file_path):
|
214 |
+
st.success(f"Email sent to: {email}")
|
215 |
else:
|
216 |
st.error("β Failed to send email. Please try again.")
|
217 |
|