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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 |
+
st.header("Business Ideation & Story Generation Platform")
|
| 8 |
+
|
| 9 |
+
# Store responses across user interactions
|
| 10 |
+
if 'business_details' not in st.session_state:
|
| 11 |
+
st.session_state.business_details = {}
|
| 12 |
+
if 'final_ideation' not in st.session_state:
|
| 13 |
+
st.session_state.final_ideation = []
|
| 14 |
+
if 'human_interactions' not in st.session_state:
|
| 15 |
+
st.session_state.human_interactions = []
|
| 16 |
+
if 'brainstorm_response' not in st.session_state:
|
| 17 |
+
st.session_state.brainstorm_response = {}
|
| 18 |
+
if 'final_story' not in st.session_state:
|
| 19 |
+
st.session_state.final_story = ""
|
| 20 |
+
if 'generated_image' not in st.session_state:
|
| 21 |
+
st.session_state.generated_image = ""
|
| 22 |
+
|
| 23 |
+
# ---------------------- CONTEXT ANALYSIS ----------------------
|
| 24 |
+
|
| 25 |
+
st.subheader("Step 1: Context Analysis")
|
| 26 |
+
msg = st.text_input("Describe your business here until i say to move forward:")
|
| 27 |
+
if st.button("Submit"):
|
| 28 |
+
resp = requests.post(f"{API_URL}/context-analysis", json={"message": msg})
|
| 29 |
+
if resp.ok:
|
| 30 |
+
response = resp.json()
|
| 31 |
+
st.markdown(response.get('response'))
|
| 32 |
+
if response.get('complete') == True:
|
| 33 |
+
st.session_state.business_details = response.get("business_details", {})
|
| 34 |
+
st.success('Context analysis completed. Scroll Down to step 2 now.')
|
| 35 |
+
else:
|
| 36 |
+
st.error("Failed to analyze context.")
|
| 37 |
+
|
| 38 |
+
if st.session_state.business_details != {}:
|
| 39 |
+
st.json(st.session_state.business_details)
|
| 40 |
+
st.markdown("---")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------------- IDEATION ----------------------
|
| 44 |
+
st.subheader("Step 2: Generate Ideas")
|
| 45 |
+
if st.button("Generate Creative Ideas"):
|
| 46 |
+
resp = requests.post(f"{API_URL}/ideation")
|
| 47 |
+
if resp.ok:
|
| 48 |
+
result = resp.json()
|
| 49 |
+
st.session_state.final_ideation = result["response"]["improver_response"][-1]
|
| 50 |
+
# st.write("Final Ideas:")
|
| 51 |
+
# for idea in eval(st.session_state.final_ideation):
|
| 52 |
+
# st.markdown(f"- {idea}")
|
| 53 |
+
else:
|
| 54 |
+
st.error("Failed to generate ideas.")
|
| 55 |
+
|
| 56 |
+
st.markdown("---")
|
| 57 |
+
if len(st.session_state.final_ideation)>0:
|
| 58 |
+
# st.subheader("Final Ideations")
|
| 59 |
+
st.markdown(f"*Idea:1* - {eval(st.session_state.final_ideation)[0]}")
|
| 60 |
+
st.markdown(f"*Idea:2* - {eval(st.session_state.final_ideation)[1]}")
|
| 61 |
+
st.markdown(f"*Idea:3* - {eval(st.session_state.final_ideation)[2]}")
|
| 62 |
+
st.markdown(f"*Idea:4* - {eval(st.session_state.final_ideation)[3]}")
|
| 63 |
+
st.success('Ideas are generated. Go down to refine your ideas.')
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ---------------------- HUMAN REFINEMENT ----------------------
|
| 67 |
+
st.subheader("Step 3: Human Feedback Loop")
|
| 68 |
+
query = st.text_input("Suggest a change or improvement to the ideas:")
|
| 69 |
+
if st.button("Refine with Feedback"):
|
| 70 |
+
resp = requests.post(f"{API_URL}/human-idea-refining", json={"query": query})
|
| 71 |
+
if resp.ok:
|
| 72 |
+
refined = resp.json()['response']
|
| 73 |
+
st.session_state.human_interactions.append(refined)
|
| 74 |
+
# st.markdown(refined)
|
| 75 |
+
else:
|
| 76 |
+
st.error("Failed to refine ideas.")
|
| 77 |
+
|
| 78 |
+
if len(st.session_state.human_interactions)>0:
|
| 79 |
+
st.markdown(st.session_state.human_interactions[-1])
|
| 80 |
+
|
| 81 |
+
st.markdown("---")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ---------------------- BRAINSTORM ----------------------
|
| 85 |
+
st.subheader("Step 4: Story Boarding with brainstorming")
|
| 86 |
+
|
| 87 |
+
defaults = {
|
| 88 |
+
"selected_topics": [],
|
| 89 |
+
"selected_from_brainstorm": [],
|
| 90 |
+
"story": "",
|
| 91 |
+
"brainstorming_topics": [],
|
| 92 |
+
"topics_to_pass":[],
|
| 93 |
+
"base64_images": [],
|
| 94 |
+
"history": [],
|
| 95 |
+
"history_index": -1, # -1 means no history yet
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
for key, val in defaults.items():
|
| 99 |
+
if key not in st.session_state:
|
| 100 |
+
st.session_state[key] = val
|
| 101 |
+
|
| 102 |
+
uploaded_files = st.file_uploader("π Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True)
|
| 103 |
+
|
| 104 |
+
if uploaded_files:
|
| 105 |
+
base64_images = []
|
| 106 |
+
for file in uploaded_files:
|
| 107 |
+
base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
|
| 108 |
+
st.session_state.base64_images = base64_images
|
| 109 |
+
|
| 110 |
+
def call_brainstorming_api():
|
| 111 |
+
print('Selected topics:', st.session_state.topics_to_pass)
|
| 112 |
+
response = requests.post(
|
| 113 |
+
"http://127.0.0.1:8848/brainstrom", # Replace with your actual endpoint
|
| 114 |
+
params={"thread_id": "my-session"},
|
| 115 |
+
json={
|
| 116 |
+
"preferred_topics": st.session_state.topics_to_pass,
|
| 117 |
+
"image_base64_list": st.session_state.base64_images
|
| 118 |
+
}
|
| 119 |
+
)
|
| 120 |
+
if response.ok:
|
| 121 |
+
result_json = response.json()
|
| 122 |
+
data = result_json.get("response", {})
|
| 123 |
+
st.session_state.story = data.get("stories", [""])[-1]
|
| 124 |
+
st.session_state.brainstorming_topics = data.get("brainstroming_topics", [])
|
| 125 |
+
else:
|
| 126 |
+
st.error("β API call failed.")
|
| 127 |
+
st.write(response.text)
|
| 128 |
+
|
| 129 |
+
col1, col2 = st.columns(2)
|
| 130 |
+
|
| 131 |
+
with col1:
|
| 132 |
+
st.subheader("π Story")
|
| 133 |
+
st.text_area("Generated Story", st.session_state.story, height=300)
|
| 134 |
+
|
| 135 |
+
with col2:
|
| 136 |
+
st.subheader("Brainstorming Topics (Click to Select)")
|
| 137 |
+
if st.session_state.brainstorming_topics:
|
| 138 |
+
topics_dict = st.session_state.brainstorming_topics[-1]
|
| 139 |
+
selected = set(st.session_state.selected_from_brainstorm)
|
| 140 |
+
for label, topic in topics_dict.items():
|
| 141 |
+
if st.checkbox(topic, key=topic, value=topic in selected):
|
| 142 |
+
selected.add(topic)
|
| 143 |
+
else:
|
| 144 |
+
selected.discard(topic)
|
| 145 |
+
st.session_state.topics_to_pass = list(selected)
|
| 146 |
+
st.session_state.selected_from_brainstorm = list(selected)
|
| 147 |
+
else:
|
| 148 |
+
st.info("No brainstorming topics yet. Click 'Generate Brainstorm' to start.")
|
| 149 |
+
|
| 150 |
+
if st.button("π Brainstorm"):
|
| 151 |
+
# Truncate future if we're not at the end
|
| 152 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 153 |
+
st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
|
| 154 |
+
|
| 155 |
+
# Save current state to history
|
| 156 |
+
current_state = {
|
| 157 |
+
"selected_topics": st.session_state.selected_topics.copy(),
|
| 158 |
+
"selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
|
| 159 |
+
"story": st.session_state.story,
|
| 160 |
+
"brainstorming_topics": st.session_state.brainstorming_topics.copy()
|
| 161 |
+
}
|
| 162 |
+
st.session_state.history.append(current_state)
|
| 163 |
+
st.session_state.history_index += 1
|
| 164 |
+
|
| 165 |
+
# Update current state
|
| 166 |
+
st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
|
| 167 |
+
st.session_state.selected_from_brainstorm = []
|
| 168 |
+
|
| 169 |
+
call_brainstorming_api()
|
| 170 |
+
st.rerun()
|
| 171 |
+
|
| 172 |
+
if st.button("π Back"):
|
| 173 |
+
if st.session_state.history_index > 0:
|
| 174 |
+
st.session_state.history_index -= 1
|
| 175 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 176 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 177 |
+
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 178 |
+
st.session_state.story = state["story"]
|
| 179 |
+
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 180 |
+
st.rerun()
|
| 181 |
+
else:
|
| 182 |
+
st.warning("You're at the first step.")
|
| 183 |
+
|
| 184 |
+
if st.button("π Forward"):
|
| 185 |
+
if st.session_state.history_index < len(st.session_state.history) - 1:
|
| 186 |
+
st.session_state.history_index += 1
|
| 187 |
+
state = st.session_state.history[st.session_state.history_index]
|
| 188 |
+
st.session_state.selected_topics = state["selected_topics"]
|
| 189 |
+
st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
|
| 190 |
+
st.session_state.story = state["story"]
|
| 191 |
+
st.session_state.brainstorming_topics = state["brainstorming_topics"]
|
| 192 |
+
st.rerun()
|
| 193 |
+
else:
|
| 194 |
+
st.warning("You're at the most recent step.")
|
| 195 |
+
|
| 196 |
+
st.markdown("---")
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# ---------------------- FINAL STORY ----------------------
|
| 200 |
+
st.subheader("π Step 5: Final Story")
|
| 201 |
+
if st.button("Generate Final Story"):
|
| 202 |
+
resp = requests.post(f"{API_URL}/generate-final-story")
|
| 203 |
+
if resp.ok:
|
| 204 |
+
st.session_state.final_story = resp.json()['response']
|
| 205 |
+
# st.markdown(st.session_state.final_story)
|
| 206 |
+
else:
|
| 207 |
+
st.error("Failed to generate final story.")
|
| 208 |
+
st.markdown(st.session_state.final_story)
|
| 209 |
+
st.markdown("---")
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
# ---------------------- GENERATE IMAGE ----------------------
|
| 213 |
+
st.subheader("πΌοΈ Step 6: Generate Image")
|
| 214 |
+
if st.button("Generate Image from Story"):
|
| 215 |
+
resp = requests.post(f"{API_URL}/generate-image")
|
| 216 |
+
if resp.ok:
|
| 217 |
+
img_url = resp.json()['response']
|
| 218 |
+
# st.image("image.png", caption="Generated Image from Story")
|
| 219 |
+
st.session_state.generated_image = "image.png"
|
| 220 |
+
else:
|
| 221 |
+
st.error("Failed to generate image.")
|
| 222 |
+
st.image("image.png",caption="Generated Image from Story")
|