Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
#1
by
Uzaiir
- opened
- src/streamlit_app.py +82 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,84 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
from models.imageCaptioning import generateCaption
|
| 9 |
+
from models.storyGeneration import generateStory
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Image2Story Generator", layout="centered")
|
| 12 |
+
st.title("Image2Story Generator")
|
| 13 |
+
st.write("Upload your Image and let us generate a Story for you")
|
| 14 |
+
|
| 15 |
+
upload_dir = "/tmp/uploads"
|
| 16 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
#Upload your file
|
| 19 |
+
uploaded_file = st.sidebar.file_uploader("Upload Your Image", type= ["jpg" , "jpeg" , "png" , "webp"])
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
if uploaded_file:
|
| 24 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 25 |
+
image_path = os.path.join(upload_dir, uploaded_file.name)
|
| 26 |
+
image.save(image_path)
|
| 27 |
+
|
| 28 |
+
# st.image(image, caption="uploaded image" , use_container_width=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# with st.spinner("Generating Captions..."):
|
| 33 |
+
# caption = generateCaption(image_path)
|
| 34 |
+
# st.success(f"The image is of : {caption}")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
with st.spinner("Generating Captions..."):
|
| 39 |
+
caption = generateCaption(image_path)
|
| 40 |
+
st.success("Caption generated successfully!")
|
| 41 |
+
|
| 42 |
+
st.markdown(f"""
|
| 43 |
+
<div style="background-color:#f0f2f6;padding:15px 20px;border-radius:10px;margin-top:20px;border-left:5px solid #4CAF50;">
|
| 44 |
+
<h4 style="margin-bottom:10px;">πΌοΈ <strong>Scenario is :</strong></h4>
|
| 45 |
+
<p style="font-size:16px;color:#333;">{caption}</p>
|
| 46 |
+
</div>
|
| 47 |
+
""", unsafe_allow_html=True)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
story_style = st.sidebar.selectbox("Choose Story Style" , ["William Shakespeare" , "Leo Tolstoy", "Charles Dickens" , "Haruki Murakami" , "J.K. Rowling" , "Stephen King"])
|
| 51 |
+
|
| 52 |
+
prompt = f"""
|
| 53 |
+
You are a professional story writer with a deep understanding of surreal and introspective storytelling, inspired by {story_style}.
|
| 54 |
+
Write a short story based on the theme: {caption.strip()}.
|
| 55 |
+
The story should be in between 500 and 800 words, and must include:
|
| 56 |
+
- A clear beginning and ending.
|
| 57 |
+
- Subtle emotional depth and a dreamlike atmosphere.
|
| 58 |
+
- Elements typical of {story_style}'s writing style.
|
| 59 |
+
Do not include a title. Return only the story.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# with st.spinner("Generating Story..."):
|
| 64 |
+
# story = generateStory(captions)
|
| 65 |
+
# st.markdown("Generated Story")
|
| 66 |
+
# st.write(story)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
with st.spinner("Generating Story..."):
|
| 70 |
+
story = generateStory(caption)
|
| 71 |
+
|
| 72 |
+
st.markdown("""
|
| 73 |
+
<div style="background-color:#f9f9f9;padding:15px 20px;border-radius:10px;margin-top:20px;border-left:5px solid #2196F3;">
|
| 74 |
+
<h4 style="margin-bottom:10px;">π <strong>Generated Story</strong></h4>
|
| 75 |
+
""", unsafe_allow_html=True)
|
| 76 |
+
|
| 77 |
+
formatted_story = story.replace('\n', '<br>')
|
| 78 |
+
|
| 79 |
+
st.markdown(f"""
|
| 80 |
+
<div style="font-size:16px;line-height:1.6;color:#444;">
|
| 81 |
+
{formatted_story}
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
""", unsafe_allow_html=True)
|