import os
import streamlit as st
from PIL import Image
import os
from models.imageCaptioning import generateCaption
from models.storyGeneration import generateStory
st.set_page_config(page_title="Image2Story Generator", layout="centered")
st.title("Image2Story Generator")
st.write("Upload your Image and let us generate a Story for you")
upload_dir = "/tmp/uploads"
os.makedirs(upload_dir, exist_ok=True)
#Upload your file
uploaded_file = st.sidebar.file_uploader("Upload Your Image", type= ["jpg" , "jpeg" , "png" , "webp"])
if uploaded_file:
image = Image.open(uploaded_file).convert("RGB")
image_path = os.path.join(upload_dir, uploaded_file.name)
image.save(image_path)
# st.image(image, caption="uploaded image" , use_container_width=True)
# with st.spinner("Generating Captions..."):
# caption = generateCaption(image_path)
# st.success(f"The image is of : {caption}")
with st.spinner("Generating Captions..."):
caption = generateCaption(image_path)
st.success("Caption generated successfully!")
st.markdown(f"""
🖼️ Scenario is :
{caption}
""", unsafe_allow_html=True)
story_style = st.sidebar.selectbox("Choose Story Style" , ["William Shakespeare" , "Leo Tolstoy", "Charles Dickens" , "Haruki Murakami" , "J.K. Rowling" , "Stephen King"])
prompt = f"""
You are a professional story writer with a deep understanding of surreal and introspective storytelling, inspired by {story_style}.
Write a short story based on the theme: {caption.strip()}.
The story should be in between 500 and 800 words, and must include:
- A clear beginning and ending.
- Subtle emotional depth and a dreamlike atmosphere.
- Elements typical of {story_style}'s writing style.
Do not include a title. Return only the story.
"""
# with st.spinner("Generating Story..."):
# story = generateStory(captions)
# st.markdown("Generated Story")
# st.write(story)
with st.spinner("Generating Story..."):
story = generateStory(caption)
st.markdown("""
📖 Generated Story
""", unsafe_allow_html=True)
formatted_story = story.replace('\n', '
')
st.markdown(f"""
{formatted_story}
""", unsafe_allow_html=True)