|
import os |
|
import re |
|
import smtplib |
|
import streamlit as st |
|
from transformers import pipeline |
|
from typing import Dict |
|
from gtts import gTTS |
|
from together import Together |
|
from email.mime.multipart import MIMEMultipart |
|
from email.mime.text import MIMEText |
|
from email.mime.audio import MIMEAudio |
|
|
|
|
|
|
|
|
|
|
|
|
|
def img2txt(url: str) -> str: |
|
print("Initializing captioning model...") |
|
captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
|
|
|
print("Generating text from the image...") |
|
text = captioning_model(url, max_new_tokens=20)[0]["generated_text"] |
|
|
|
print(text) |
|
return text |
|
|
|
|
|
def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str: |
|
client = Together(api_key=os.environ.get("TOGETHER_API_KEY")) |
|
|
|
story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}" |
|
|
|
stream = client.chat.completions.create( |
|
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", |
|
messages=[ |
|
{"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt. |
|
Ensure the story does not exceed 250 words.'''}, |
|
{"role": "user", "content": story_prompt} |
|
], |
|
top_k=top_k, |
|
top_p=top_p, |
|
temperature=temperature, |
|
stream=True |
|
) |
|
|
|
story = '' |
|
for chunk in stream: |
|
story += chunk.choices[0].delta.content |
|
|
|
return story |
|
|
|
|
|
def txt2speech(text: str) -> None: |
|
print("Converting text to speech using gTTS...") |
|
tts = gTTS(text=text, lang='en') |
|
tts.save("audio_story.mp3") |
|
|
|
|
|
def get_user_preferences() -> Dict[str, str]: |
|
preferences = { |
|
'continent': st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]), |
|
'genre': st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]), |
|
'setting': st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]), |
|
'plot': st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]), |
|
'tone': st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]), |
|
'theme': st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]), |
|
'conflict': st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]), |
|
'twist': st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]), |
|
'ending': st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"]) |
|
} |
|
return preferences |
|
|
|
|
|
def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool: |
|
try: |
|
|
|
smtp_server = os.environ.get("SMTP_SERVER") |
|
smtp_port = int(os.environ.get("SMTP_PORT", 587)) |
|
sender_email = os.environ.get("SENDER_EMAIL") |
|
sender_password = os.environ.get("SENDER_PASSWORD") |
|
|
|
|
|
msg = MIMEMultipart() |
|
msg['From'] = sender_email |
|
msg['To'] = recipient_email |
|
msg['Subject'] = "Your Generated Story" |
|
|
|
|
|
msg.attach(MIMEText(f"Here's your generated story:\n\n{story_text}\n\nEnjoy!", 'plain')) |
|
|
|
|
|
with open(audio_file_path, 'rb') as audio_file: |
|
audio_part = MIMEAudio(audio_file.read(), _subtype='mp3') |
|
audio_part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(audio_file_path)) |
|
msg.attach(audio_part) |
|
|
|
|
|
with smtplib.SMTP(smtp_server, smtp_port) as server: |
|
server.starttls() |
|
server.login(sender_email, sender_password) |
|
server.send_message(msg) |
|
|
|
return True |
|
|
|
except Exception as e: |
|
print(f"Error sending email: {str(e)}") |
|
return False |
|
|
|
|
|
def validate_email(email: str) -> bool: |
|
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$' |
|
return re.match(pattern, email) is not None |
|
|
|
|
|
def main(): |
|
st.set_page_config( |
|
page_title="π¨ Image-to-Audio Story π§", |
|
page_icon="πΌοΈ", |
|
layout="wide" |
|
) |
|
st.title("Turn the Image into Audio Story") |
|
|
|
|
|
if "story" not in st.session_state: |
|
st.session_state.story = "" |
|
if "audio_file_path" not in st.session_state: |
|
st.session_state.audio_file_path = "" |
|
if "caption" not in st.session_state: |
|
st.session_state.caption = "" |
|
|
|
|
|
col1, col2 = st.columns([2, 3]) |
|
|
|
with col1: |
|
|
|
st.markdown("## π· Upload Image") |
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
st.markdown("## π Story Preferences") |
|
preferences = get_user_preferences() |
|
|
|
with col2: |
|
if uploaded_file is not None: |
|
|
|
st.session_state.story = "" |
|
st.session_state.audio_file_path = "" |
|
|
|
|
|
st.markdown("## πΌοΈ Your Image") |
|
bytes_data = uploaded_file.read() |
|
with open("uploaded_image.jpg", "wb") as file: |
|
file.write(bytes_data) |
|
st.image(uploaded_file, use_column_width=True) |
|
|
|
|
|
with st.spinner("π Generating image caption..."): |
|
try: |
|
scenario = img2txt("uploaded_image.jpg") |
|
st.session_state.caption = scenario |
|
st.success("Image caption generated.") |
|
except Exception as e: |
|
st.error(f"An error occurred while generating image caption: {str(e)}") |
|
st.warning("Please try again or contact support if the problem persists.") |
|
|
|
|
|
if st.session_state.caption: |
|
st.markdown("## π Image Caption") |
|
st.write(st.session_state.caption) |
|
|
|
|
|
if st.session_state.caption: |
|
if st.button("π¨ Generate Story"): |
|
with st.spinner("π Generating story..."): |
|
try: |
|
|
|
prompt = f"""Based on the image description: '{st.session_state.caption}', |
|
create a {preferences['genre']} story set in {preferences['setting']} |
|
in {preferences['continent']}. The story should have a {preferences['tone']} |
|
tone and explore the theme of {preferences['theme']}. The main conflict |
|
should be {preferences['conflict']}. The story should have a {preferences['twist']} |
|
and end with a {preferences['ending']} ending.""" |
|
|
|
|
|
story = txt2story(prompt, top_k=5, top_p=0.8, temperature=1.5) |
|
st.session_state.story = story |
|
st.success("Story generated.") |
|
|
|
|
|
with st.spinner("π Generating audio story..."): |
|
txt2speech(story) |
|
st.session_state.audio_file_path = "audio_story.mp3" |
|
st.success("Audio story generated.") |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred: {str(e)}") |
|
st.warning("Please try again or contact support if the problem persists.") |
|
|
|
|
|
if st.session_state.story: |
|
st.markdown("---") |
|
|
|
|
|
with st.expander("π Generated Story", expanded=True): |
|
st.write(st.session_state.story) |
|
|
|
|
|
with st.expander("π§ Audio Version", expanded=True): |
|
st.audio(st.session_state.audio_file_path) |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("## π§ Get Story via Email") |
|
email = st.text_input( |
|
"Enter your email address:", |
|
help="We'll send you the story text and audio file" |
|
) |
|
|
|
if st.button("π€ Send to Email"): |
|
if not email: |
|
st.warning("Please enter an email address.") |
|
elif not validate_email(email): |
|
st.error("Please enter a valid email address.") |
|
else: |
|
with st.spinner("π¨ Sending email..."): |
|
if send_story_email(email, st.session_state.story, st.session_state.audio_file_path): |
|
st.success(f"Email sent to: {email}") |
|
else: |
|
st.error("β Failed to send email. Please try again.") |
|
|
|
if __name__ == '__main__': |
|
main() |