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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
API_URL = "http://127.0.0.1:8848" # 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 |
+
# ---------------------- BRAINSTORM ----------------------
|
| 26 |
+
st.subheader("Step 4: Story Boarding with brainstorming")
|
| 27 |
+
|
| 28 |
+
defaults = {
|
| 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, # -1 means no history yet
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
for key, val in defaults.items():
|
| 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 |
+
for file in uploaded_files:
|
| 48 |
+
base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
|
| 49 |
+
st.session_state.base64_images = base64_images
|
| 50 |
+
|
| 51 |
+
def call_brainstorming_api():
|
| 52 |
+
print('Selected topics:', st.session_state.topics_to_pass)
|
| 53 |
+
response = requests.post(
|
| 54 |
+
"http://127.0.0.1:8848/brainstorm", # Replace with your actual endpoint
|
| 55 |
+
params={"thread_id": "my-session"},
|
| 56 |
+
json={
|
| 57 |
+
"preferred_topics": st.session_state.topics_to_pass,
|
| 58 |
+
"image_base64_list": st.session_state.base64_images
|
| 59 |
+
}
|
| 60 |
+
)
|
| 61 |
+
if response.ok:
|
| 62 |
+
result_json = response.json()
|
| 63 |
+
data = result_json.get("response", {})
|
| 64 |
+
st.session_state.story = data.get("stories", [""])[-1]
|
| 65 |
+
st.session_state.brainstorming_topics = data.get("brainstroming_topics", [])
|
| 66 |
+
else:
|
| 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)
|
| 93 |
+
updated_topics = {}
|
| 94 |
+
|
| 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}")
|
| 102 |
+
|
| 103 |
+
if checked:
|
| 104 |
+
selected.add(edited_topic)
|
| 105 |
+
else:
|
| 106 |
+
selected.discard(edited_topic)
|
| 107 |
+
|
| 108 |
+
updated_topics[label] = edited_topic
|
| 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 |
+
# Truncate future if we're not at the end
|
| 118 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 119 |
+
st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
|
| 120 |
+
|
| 121 |
+
# Save current state to history
|
| 122 |
+
current_state = {
|
| 123 |
+
"selected_topics": st.session_state.selected_topics.copy(),
|
| 124 |
+
"selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
|
| 125 |
+
"story": st.session_state.story,
|
| 126 |
+
"brainstorming_topics": st.session_state.brainstorming_topics.copy()
|
| 127 |
+
}
|
| 128 |
+
st.session_state.history.append(current_state)
|
| 129 |
+
st.session_state.history_index += 1
|
| 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 |
+
if st.button("π Back"):
|
| 139 |
+
if st.session_state.history_index > 0:
|
| 140 |
+
st.session_state.history_index -= 1
|
| 141 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 142 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 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 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 152 |
+
st.session_state.history_index += 1
|
| 153 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 154 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 155 |
+
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 156 |
+
st.session_state.story = state["story"]
|
| 157 |
+
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 158 |
+
st.rerun()
|
| 159 |
+
else:
|
| 160 |
+
st.warning("You're at the most recent step.")
|
| 161 |
+
|
| 162 |
+
st.markdown("---")
|
| 163 |
+
|
| 164 |
+
|