|
import os |
|
import streamlit as st |
|
from utils import ( |
|
img2txt, |
|
txt2story, |
|
txt2speech, |
|
get_user_preferences, |
|
send_story_email, |
|
validate_email |
|
) |
|
|
|
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.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) |
|
|
|
|
|
if st.button("π¨ Generate Story"): |
|
with st.spinner("π€ AI is working its magic..."): |
|
try: |
|
|
|
scenario = img2txt("uploaded_image.jpg") |
|
st.session_state.caption = scenario |
|
|
|
|
|
prompt = f"""Based on the image description: '{scenario}', |
|
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 |
|
|
|
|
|
txt2speech(story) |
|
st.session_state.audio_file_path = "audio_story.mp3" |
|
|
|
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("π Image Caption", expanded=True): |
|
st.write(st.session_state.caption) |
|
|
|
|
|
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("βοΈ Story sent successfully! Check your email.") |
|
else: |
|
st.error("β Failed to send email. Please try again.") |
|
|
|
if __name__ == '__main__': |
|
main() |