ChinarQ-AI Uzaiir commited on
Commit
89b53ac
·
verified ·
1 Parent(s): 0c31a3b

Update src/streamlit_app.py (#1)

Browse files

- Update src/streamlit_app.py (87d22c9187e93d39a8f92c49db412119b71f595f)


Co-authored-by: Khan <[email protected]>

Files changed (1) hide show
  1. src/streamlit_app.py +82 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,84 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)