Spaces:
Running
Running
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""" | |
<div style="background-color:#f0f2f6;padding:15px 20px;border-radius:10px;margin-top:20px;border-left:5px solid #4CAF50;"> | |
<h4 style="margin-bottom:10px;">πΌοΈ <strong>Scenario is :</strong></h4> | |
<p style="font-size:16px;color:#333;">{caption}</p> | |
</div> | |
""", 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(""" | |
<div style="background-color:#f9f9f9;padding:15px 20px;border-radius:10px;margin-top:20px;border-left:5px solid #2196F3;"> | |
<h4 style="margin-bottom:10px;">π <strong>Generated Story</strong></h4> | |
""", unsafe_allow_html=True) | |
formatted_story = story.replace('\n', '<br>') | |
st.markdown(f""" | |
<div style="font-size:16px;line-height:1.6;color:#444;"> | |
{formatted_story} | |
</div> | |
</div> | |
""", unsafe_allow_html=True) |