Spaces:
Sleeping
Sleeping
Update src/pages/brainstorm.py
Browse files- src/pages/brainstorm.py +76 -93
src/pages/brainstorm.py
CHANGED
|
@@ -1,63 +1,55 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import base64
|
| 4 |
-
API_URL = "https://subashdvorak-brainstroming-fast-api.hf.space" # Replace with your FastAPI URL
|
| 5 |
-
|
| 6 |
-
st.set_page_config(page_title="AI Business Ideation Platform", layout="wide")
|
| 7 |
-
|
| 8 |
-
# Store responses across user interactions
|
| 9 |
-
if 'business_details' not in st.session_state:
|
| 10 |
-
st.session_state.business_details = {}
|
| 11 |
-
if 'final_ideation' not in st.session_state:
|
| 12 |
-
st.session_state.final_ideation = []
|
| 13 |
-
if 'human_interactions' not in st.session_state:
|
| 14 |
-
st.session_state.human_interactions = []
|
| 15 |
-
if 'brainstorm_response' not in st.session_state:
|
| 16 |
-
st.session_state.brainstorm_response = {}
|
| 17 |
-
if 'final_story' not in st.session_state:
|
| 18 |
-
st.session_state.final_story = ""
|
| 19 |
-
if 'generated_image' not in st.session_state:
|
| 20 |
-
st.session_state.generated_image = ""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
st.subheader("Step 4: Story Boarding with brainstorming")
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"selected_topics": [],
|
| 30 |
"selected_from_brainstorm": [],
|
| 31 |
"story": "",
|
| 32 |
"brainstorming_topics": [],
|
| 33 |
-
"topics_to_pass":[],
|
| 34 |
"base64_images": [],
|
| 35 |
"history": [],
|
| 36 |
-
"history_index": -1
|
| 37 |
}
|
| 38 |
|
| 39 |
-
for key, val in
|
| 40 |
if key not in st.session_state:
|
| 41 |
st.session_state[key] = val
|
| 42 |
|
|
|
|
| 43 |
uploaded_files = st.file_uploader("π Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True)
|
| 44 |
|
| 45 |
if uploaded_files:
|
| 46 |
base64_images = []
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
|
|
|
| 51 |
def call_brainstorming_api():
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
if response.ok:
|
| 62 |
result_json = response.json()
|
| 63 |
data = result_json.get("response", {})
|
|
@@ -67,26 +59,15 @@ def call_brainstorming_api():
|
|
| 67 |
st.error("β API call failed.")
|
| 68 |
st.write(response.text)
|
| 69 |
|
|
|
|
| 70 |
col1, col2 = st.columns(2)
|
| 71 |
|
| 72 |
with col1:
|
| 73 |
st.subheader("π Story")
|
| 74 |
st.text_area("Generated Story", st.session_state.story, height=300)
|
| 75 |
|
| 76 |
-
# with col2:
|
| 77 |
-
# st.subheader("Brainstorming Topics (Click to Select)")
|
| 78 |
-
# if st.session_state.brainstorming_topics:
|
| 79 |
-
# topics_dict = st.session_state.brainstorming_topics[-1]
|
| 80 |
-
# selected = set(st.session_state.selected_from_brainstorm)
|
| 81 |
-
# for label, topic in topics_dict.items():
|
| 82 |
-
# if st.checkbox(topic, key=topic, value=topic in selected):
|
| 83 |
-
# selected.add(topic)
|
| 84 |
-
# else:
|
| 85 |
-
# selected.discard(topic)
|
| 86 |
-
# st.session_state.topics_to_pass = list(selected)
|
| 87 |
-
# st.session_state.selected_from_brainstorm = list(selected)
|
| 88 |
with col2:
|
| 89 |
-
st.subheader("Brainstorming Topics (Click to Select / Edit)")
|
| 90 |
if st.session_state.brainstorming_topics:
|
| 91 |
topics_dict = st.session_state.brainstorming_topics[-1]
|
| 92 |
selected = set(st.session_state.selected_from_brainstorm)
|
|
@@ -95,7 +76,6 @@ with col2:
|
|
| 95 |
for label, topic in topics_dict.items():
|
| 96 |
col_a, col_b = st.columns([0.1, 0.9])
|
| 97 |
with col_a:
|
| 98 |
-
# Use a blank label and keep checkbox aligned
|
| 99 |
checked = st.checkbox("", key=f"check_{label}", value=topic in selected)
|
| 100 |
with col_b:
|
| 101 |
edited_topic = st.text_input("", value=topic, key=f"edit_{label}")
|
|
@@ -109,56 +89,59 @@ with col2:
|
|
| 109 |
|
| 110 |
st.session_state.topics_to_pass = list(selected)
|
| 111 |
st.session_state.selected_from_brainstorm = list(selected)
|
| 112 |
-
|
| 113 |
else:
|
| 114 |
st.info("No brainstorming topics yet. Click 'Generate Brainstorm' to start.")
|
| 115 |
|
|
|
|
| 116 |
if st.button("π Brainstorm"):
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
st.session_state.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# Update current state
|
| 132 |
-
st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
|
| 133 |
-
st.session_state.selected_from_brainstorm = []
|
| 134 |
-
|
| 135 |
-
call_brainstorming_api()
|
| 136 |
-
st.rerun()
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
st.session_state.
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 144 |
-
st.session_state.story = state["story"]
|
| 145 |
-
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 146 |
st.rerun()
|
| 147 |
-
else:
|
| 148 |
-
st.warning("You're at the first step.")
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if st.button("π Forward"):
|
| 151 |
-
|
| 152 |
-
st.session_state.history_index
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
st.markdown("---")
|
| 163 |
-
|
| 164 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
API_URL = "https://subashdvorak-brainstroming-fast-api.hf.space"
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="AI Business Ideation Platform", layout="wide")
|
| 8 |
st.subheader("Step 4: Story Boarding with brainstorming")
|
| 9 |
|
| 10 |
+
# Store persistent session state
|
| 11 |
+
session_defaults = {
|
| 12 |
+
'business_details': {},
|
| 13 |
+
'final_ideation': [],
|
| 14 |
+
'human_interactions': [],
|
| 15 |
+
'brainstorm_response': {},
|
| 16 |
+
'final_story': "",
|
| 17 |
+
'generated_image': "",
|
| 18 |
"selected_topics": [],
|
| 19 |
"selected_from_brainstorm": [],
|
| 20 |
"story": "",
|
| 21 |
"brainstorming_topics": [],
|
| 22 |
+
"topics_to_pass": [],
|
| 23 |
"base64_images": [],
|
| 24 |
"history": [],
|
| 25 |
+
"history_index": -1
|
| 26 |
}
|
| 27 |
|
| 28 |
+
for key, val in session_defaults.items():
|
| 29 |
if key not in st.session_state:
|
| 30 |
st.session_state[key] = val
|
| 31 |
|
| 32 |
+
# Upload images
|
| 33 |
uploaded_files = st.file_uploader("π Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True)
|
| 34 |
|
| 35 |
if uploaded_files:
|
| 36 |
base64_images = []
|
| 37 |
+
with st.spinner("Encoding uploaded images..."):
|
| 38 |
+
for file in uploaded_files:
|
| 39 |
+
base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
|
| 40 |
+
st.session_state.base64_images = base64_images
|
| 41 |
|
| 42 |
+
# Brainstorming API call
|
| 43 |
def call_brainstorming_api():
|
| 44 |
+
with st.spinner("Generating brainstorming topics and story..."):
|
| 45 |
+
response = requests.post(
|
| 46 |
+
f"{API_URL}/brainstorm",
|
| 47 |
+
params={"thread_id": "my-session"},
|
| 48 |
+
json={
|
| 49 |
+
"preferred_topics": st.session_state.topics_to_pass,
|
| 50 |
+
"image_base64_list": st.session_state.base64_images
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
if response.ok:
|
| 54 |
result_json = response.json()
|
| 55 |
data = result_json.get("response", {})
|
|
|
|
| 59 |
st.error("β API call failed.")
|
| 60 |
st.write(response.text)
|
| 61 |
|
| 62 |
+
# Layout
|
| 63 |
col1, col2 = st.columns(2)
|
| 64 |
|
| 65 |
with col1:
|
| 66 |
st.subheader("π Story")
|
| 67 |
st.text_area("Generated Story", st.session_state.story, height=300)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
with col2:
|
| 70 |
+
st.subheader("π§ Brainstorming Topics (Click to Select / Edit)")
|
| 71 |
if st.session_state.brainstorming_topics:
|
| 72 |
topics_dict = st.session_state.brainstorming_topics[-1]
|
| 73 |
selected = set(st.session_state.selected_from_brainstorm)
|
|
|
|
| 76 |
for label, topic in topics_dict.items():
|
| 77 |
col_a, col_b = st.columns([0.1, 0.9])
|
| 78 |
with col_a:
|
|
|
|
| 79 |
checked = st.checkbox("", key=f"check_{label}", value=topic in selected)
|
| 80 |
with col_b:
|
| 81 |
edited_topic = st.text_input("", value=topic, key=f"edit_{label}")
|
|
|
|
| 89 |
|
| 90 |
st.session_state.topics_to_pass = list(selected)
|
| 91 |
st.session_state.selected_from_brainstorm = list(selected)
|
|
|
|
| 92 |
else:
|
| 93 |
st.info("No brainstorming topics yet. Click 'Generate Brainstorm' to start.")
|
| 94 |
|
| 95 |
+
# Brainstorm Button
|
| 96 |
if st.button("π Brainstorm"):
|
| 97 |
+
with st.spinner("Saving current state and generating new ideas..."):
|
| 98 |
+
# Truncate forward history
|
| 99 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 100 |
+
st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
|
| 101 |
+
|
| 102 |
+
# Save current state to history
|
| 103 |
+
current_state = {
|
| 104 |
+
"selected_topics": st.session_state.selected_topics.copy(),
|
| 105 |
+
"selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
|
| 106 |
+
"story": st.session_state.story,
|
| 107 |
+
"brainstorming_topics": st.session_state.brainstorming_topics.copy()
|
| 108 |
+
}
|
| 109 |
+
st.session_state.history.append(current_state)
|
| 110 |
+
st.session_state.history_index += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# Update current session state
|
| 113 |
+
st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
|
| 114 |
+
st.session_state.selected_from_brainstorm = []
|
| 115 |
+
|
| 116 |
+
call_brainstorming_api()
|
|
|
|
|
|
|
|
|
|
| 117 |
st.rerun()
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# Back Button
|
| 120 |
+
if st.button("π Back"):
|
| 121 |
+
with st.spinner("Loading previous brainstorm..."):
|
| 122 |
+
if st.session_state.history_index > 0:
|
| 123 |
+
st.session_state.history_index -= 1
|
| 124 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 125 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 126 |
+
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 127 |
+
st.session_state.story = state["story"]
|
| 128 |
+
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 129 |
+
st.rerun()
|
| 130 |
+
else:
|
| 131 |
+
st.warning("You're at the first step.")
|
| 132 |
+
|
| 133 |
+
# Forward Button
|
| 134 |
if st.button("π Forward"):
|
| 135 |
+
with st.spinner("Restoring next brainstorm..."):
|
| 136 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 137 |
+
st.session_state.history_index += 1
|
| 138 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 139 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 140 |
+
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 141 |
+
st.session_state.story = state["story"]
|
| 142 |
+
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 143 |
+
st.rerun()
|
| 144 |
+
else:
|
| 145 |
+
st.warning("You're at the most recent step.")
|
| 146 |
|
| 147 |
st.markdown("---")
|
|
|
|
|
|