subashdvorak commited on
Commit
cb5a15d
Β·
verified Β·
1 Parent(s): 2d8c0f7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +188 -221
src/streamlit_app.py CHANGED
@@ -1,242 +1,209 @@
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
- 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
- with st.spinner("Analyzing your context...."):
29
- resp = requests.post(f"{API_URL}/context-analysis", json={"message": msg})
30
- if resp.ok:
31
- response = resp.json()
32
- st.markdown(response.get('response'))
33
- if response.get('complete') == True:
34
- st.session_state.business_details = response.get("business_details", {})
35
- st.success('Context analysis completed. Scroll Down to step 2 now.')
36
- else:
37
- st.error("Failed to analyze context.")
38
-
39
- if st.session_state.business_details != {}:
40
- st.json(st.session_state.business_details)
41
- st.markdown("---")
42
 
43
-
44
- # ---------------------- IDEATION ----------------------
45
- st.subheader("Step 2: Generate Ideas")
46
- if st.button("Generate Creative Ideas"):
47
- with st.spinner("Implementing the ideation. Please wait...."):
48
- resp = requests.post(f"{API_URL}/ideation")
49
- if resp.ok:
50
- result = resp.json()
51
- st.session_state.final_ideation = result["response"]["improver_response"][-1]
52
- # st.write("Final Ideas:")
53
- # for idea in eval(st.session_state.final_ideation):
54
- # st.markdown(f"- {idea}")
55
- else:
56
- st.error("Failed to generate ideas.")
57
-
58
- if len(st.session_state.final_ideation)>0:
59
- # st.subheader("Final Ideations")
60
- st.markdown(f"*Idea:1* - {eval(st.session_state.final_ideation)[0]}")
61
- st.markdown(f"*Idea:2* - {eval(st.session_state.final_ideation)[1]}")
62
- st.markdown(f"*Idea:3* - {eval(st.session_state.final_ideation)[2]}")
63
- st.markdown(f"*Idea:4* - {eval(st.session_state.final_ideation)[3]}")
64
- st.success('Ideas are generated. Go down to refine your ideas.')
65
- st.markdown("---")
66
-
67
-
68
- # ---------------------- HUMAN REFINEMENT ----------------------
69
- st.subheader("Step 3: Human Feedback Loop")
70
- query = st.text_input("Suggest a change or improvement to the ideas:")
71
- if st.button("Refine with Feedback"):
72
- with st.spinner("Making changes. Please wait...."):
73
- resp = requests.post(f"{API_URL}/human-idea-refining", json={"query": query})
74
- if resp.ok:
75
- refined = resp.json()['response']
76
- st.session_state.human_interactions.append(refined)
77
- # st.markdown(refined)
78
- else:
79
- st.error("Failed to refine ideas.")
80
-
81
- if len(st.session_state.human_interactions)>0:
82
- st.markdown(st.session_state.human_interactions[-1])
83
-
84
- st.markdown("---")
85
-
86
-
87
- # ---------------------- BRAINSTORM ----------------------
88
- st.subheader("Step 4: Story Boarding with brainstorming")
89
-
90
- defaults = {
91
- "selected_topics": [],
92
- "selected_from_brainstorm": [],
93
- "story": "",
94
- "brainstorming_topics": [],
95
- "topics_to_pass":[],
96
- "base64_images": [],
97
- "history": [],
98
- "history_index": -1, # -1 means no history yet
99
  }
100
-
101
- for key, val in defaults.items():
102
  if key not in st.session_state:
103
  st.session_state[key] = val
104
 
105
- uploaded_files = st.file_uploader("πŸ“‚ Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True)
 
 
 
 
 
106
 
107
  if uploaded_files:
108
  base64_images = []
109
  for file in uploaded_files:
110
  base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
111
  st.session_state.base64_images = base64_images
 
112
 
113
- def call_brainstorming_api():
114
- print('Selected topics:', st.session_state.topics_to_pass)
115
- response = requests.post(
116
- f"{API_URL}/brainstrom", # Replace with your actual endpoint
117
- params={"thread_id": "my-session"},
118
- json={
119
- "preferred_topics": st.session_state.topics_to_pass,
120
- "image_base64_list": st.session_state.base64_images
121
- }
122
- )
123
- if response.ok:
124
- result_json = response.json()
125
- data = result_json.get("response", {})
126
- st.session_state.story = data.get("stories", [""])[-1]
127
- st.session_state.brainstorming_topics = data.get("brainstroming_topics", [])
128
- else:
129
- st.error("❌ API call failed.")
130
- st.write(response.text)
131
-
132
- col1, col2 = st.columns(2)
133
-
134
- with col1:
135
- st.subheader("πŸ“– Story")
136
- st.text_area("Generated Story", st.session_state.story, height=300)
137
-
138
- with col2:
139
- st.subheader("Brainstorming Topics (Click to Select)")
140
- if st.session_state.brainstorming_topics:
141
- topics_dict = st.session_state.brainstorming_topics[-1]
142
- selected = set(st.session_state.selected_from_brainstorm)
143
- for label, topic in topics_dict.items():
144
- if st.checkbox(topic, key=topic, value=topic in selected):
145
- selected.add(topic)
146
- else:
147
- selected.discard(topic)
148
- st.session_state.topics_to_pass = list(selected)
149
- st.session_state.selected_from_brainstorm = list(selected)
150
- else:
151
- st.info("No brainstorming topics yet. Click 'Generate Brainstorm' to start.")
152
-
153
- if st.button("πŸš€ Brainstorm"):
154
- with st.spinner("Brainstorming. Please wait..."):
155
-
156
- # Truncate future if we're not at the end
157
- if st.session_state.history_index < len(st.session_state.history) - 1:
158
- st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
159
-
160
- # Save current state to history
161
- current_state = {
162
- "selected_topics": st.session_state.selected_topics.copy(),
163
- "selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
164
- "story": st.session_state.story,
165
- "brainstorming_topics": st.session_state.brainstorming_topics.copy()
166
- }
167
- st.session_state.history.append(current_state)
168
- st.session_state.history_index += 1
169
-
170
- # Update current state
171
- st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
172
- st.session_state.selected_from_brainstorm = []
173
-
174
- call_brainstorming_api()
175
- st.rerun()
176
-
177
- if st.button("πŸ”™ Back"):
178
- with st.spinner("Going back...."):
179
- if st.session_state.history_index > 0:
180
- st.session_state.history_index -= 1
181
- state = st.session_state.history[st.session_state.history_index]
182
- st.session_state.selected_topics = state["selected_topics"]
183
- st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
184
- st.session_state.story = state["story"]
185
- st.session_state.brainstorming_topics = state["brainstorming_topics"]
186
- st.rerun()
187
- else:
188
- st.warning("You're at the first step.")
189
-
190
- if st.button("πŸ”œ Forward"):
191
- with st.spinner("Going Forward...."):
192
- if st.session_state.history_index < len(st.session_state.history) - 1:
193
- st.session_state.history_index += 1
194
- state = st.session_state.history[st.session_state.history_index]
195
- st.session_state.selected_topics = state["selected_topics"]
196
- st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
197
- st.session_state.story = state["story"]
198
- st.session_state.brainstorming_topics = state["brainstorming_topics"]
199
- st.rerun()
200
- else:
201
- st.warning("You're at the most recent step.")
202
-
203
- st.markdown("---")
204
 
205
-
206
- # ---------------------- FINAL STORY ----------------------
207
- st.subheader("πŸ“– Step 5: Final Story")
208
- if st.button("Generate Final Story"):
209
- with st.spinner("Generating final story. Please wait..."):
210
- resp = requests.post(f"{API_URL}/generate-final-story")
211
  if resp.ok:
212
- st.session_state.final_story = resp.json()['response']
213
- # st.markdown(st.session_state.final_story)
 
 
 
 
214
  else:
215
- st.error("Failed to generate final story.")
216
- st.markdown(st.session_state.final_story)
217
- st.markdown("---")
218
-
219
-
220
- # ---------------------- GENERATE IMAGE ----------------------
221
- st.subheader("πŸ–ΌοΈ Step 6: Generate Image")
222
-
223
- if "generated_image" not in st.session_state:
224
- st.session_state.generated_image = None
225
-
226
- if st.button("Generate Image from Story"):
227
- with st.spinner("Generating image. Please wait..."):
228
- resp = requests.post(f"{API_URL}/generate-image")
229
  if resp.ok:
230
- img_base64 = resp.json()['response']
231
- st.session_state.generated_image=img_base64
232
- st.success("Image generated successfully.")
233
- else:
234
- st.error("Failed to generate image.")
235
-
236
- # Display image if it exists
237
- if st.session_state.generated_image != "":
238
- st.markdown(
239
- f'<img src="data:image/png;base64,{st.session_state.generated_image}" width="500"/>',
240
- unsafe_allow_html=True
241
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
  import base64
4
+ from streamlit import switch_page
5
 
6
+ API_URL = "http://127.0.0.1:8848" # Replace with your FastAPI URL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ st.set_page_config(page_title="AI Business Ideation Platform", layout="wide")
9
+ st.header("Welcome to RT-GENAI")
10
+
11
+ # Initialize session state
12
+ session_defaults = {
13
+ 'business_details': {},
14
+ 'details_completed': False,
15
+ 'final_ideation': [],
16
+ 'human_interactions': [],
17
+ 'brainstorm_response': {},
18
+ 'final_story': "",
19
+ 'generated_image': "",
20
+ 'selected_topics': [],
21
+ 'selected_from_brainstorm': [],
22
+ 'story': "",
23
+ 'brainstorming_topics': [],
24
+ 'topics_to_pass': [],
25
+ 'base64_images': [],
26
+ 'history': [],
27
+ 'history_index': -1,
28
+ 'trigger_brainstorm': False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  }
30
+ for key, val in session_defaults.items():
 
31
  if key not in st.session_state:
32
  st.session_state[key] = val
33
 
34
+ # Step 1: Context Analysis or Orchestration
35
+ st.subheader("How can I help you?")
36
+ msg = st.text_input("")
37
+
38
+
39
+ uploaded_files = st.file_uploader("πŸ“‚ Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True, key="initial_image_uploader")
40
 
41
  if uploaded_files:
42
  base64_images = []
43
  for file in uploaded_files:
44
  base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
45
  st.session_state.base64_images = base64_images
46
+ print('Base 64 image:',base64_images)
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+ if st.button("Submit"):
50
+ # if not st.session_state.details_completed:
51
+ if st.session_state.details_completed == True:
52
+ resp = requests.post(f"{API_URL}/context-analysis", json={"message": msg})
 
 
53
  if resp.ok:
54
+ response = resp.json()
55
+ st.markdown(response.get('response'))
56
+ if response.get('complete'):
57
+ st.session_state.details_completed = True
58
+ st.session_state.business_details = response.get("business_details", {})
59
+ st.success("Context analysis completed. Now you can interact freely.")
60
  else:
61
+ st.error("Context analysis failed.")
62
+ else:
63
+ resp = requests.post(f"{API_URL}/orchestration", json={"message": msg,"image_base64": st.session_state.base64_images})
 
 
 
 
 
 
 
 
 
 
 
64
  if resp.ok:
65
+ response = resp.json()
66
+ tool = response.get("tool_response")
67
+ message = response.get("message_response")
68
+ image_caption = response.get("image_caption")
69
+ st.write('Tool response:', tool)
70
+
71
+ # ========== Tool Handling ==========
72
+ if "ideation" in tool:
73
+ resp = requests.post(f"{API_URL}/ideation")
74
+ if resp.ok:
75
+ st.session_state.final_ideation = resp.json()["response"]["improver_response"][-1]
76
+
77
+ if len(st.session_state.final_ideation)>0:
78
+ # st.subheader("Final Ideations")
79
+ st.markdown(f"*Idea:1* - {eval(st.session_state.final_ideation)[0]}")
80
+ st.markdown(f"*Idea:2* - {eval(st.session_state.final_ideation)[1]}")
81
+ st.markdown(f"*Idea:3* - {eval(st.session_state.final_ideation)[2]}")
82
+ st.markdown(f"*Idea:4* - {eval(st.session_state.final_ideation)[3]}")
83
+ st.success("Ideas generated successfully.")
84
+
85
+ elif "human-idea-refining" in tool:
86
+ resp = requests.post(f"{API_URL}/human-idea-refining", json={"query": message})
87
+ if resp.ok:
88
+ refined = resp.json()['response']
89
+ st.session_state.human_interactions.append(refined)
90
+ st.markdown(st.session_state.human_interactions[-1])
91
+ st.success("Ideas refined.")
92
+
93
+ elif "generate-story" in tool:
94
+ switch_page("pages/all_in_one.py")
95
+ # def call_brainstorming_api():
96
+ # response = requests.post(
97
+ # f"{API_URL}/brainstorm",
98
+ # params={"thread_id": "my-session"},
99
+ # json={
100
+ # "preferred_topics": st.session_state.topics_to_pass,
101
+ # "image_base64_list": st.session_state.base64_images
102
+ # }
103
+ # )
104
+ # if response.ok:
105
+ # result_json = response.json()
106
+ # data = result_json.get("response", {})
107
+ # st.session_state.story = data.get("stories", [""])[-1]
108
+ # st.session_state.brainstorming_topics = data.get("brainstroming_topics", [])
109
+ # else:
110
+ # st.error("❌ API call failed.")
111
+ # st.write(response.text)
112
+
113
+ # call_brainstorming_api()
114
+
115
+ # uploaded_files = st.file_uploader("πŸ“‚ Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True, key="brainstorm_image_uploader")
116
+ # if uploaded_files:
117
+ # base64_images = [base64.b64encode(f.read()).decode('utf-8') for f in uploaded_files]
118
+ # st.session_state.base64_images = base64_images
119
+
120
+
121
+
122
+ # # Triggered Brainstorming
123
+ # if st.session_state.trigger_brainstorm:
124
+ # st.session_state.trigger_brainstorm = False # reset flag
125
+ # # Save history
126
+ # if st.session_state.history_index < len(st.session_state.history) - 1:
127
+ # st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
128
+ # st.session_state.history.append({
129
+ # "selected_topics": st.session_state.selected_topics.copy(),
130
+ # "selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
131
+ # "story": st.session_state.story,
132
+ # "brainstorming_topics": st.session_state.brainstorming_topics.copy()
133
+ # })
134
+ # st.session_state.history_index += 1
135
+
136
+ # # Update selection
137
+ # st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
138
+ # st.session_state.selected_from_brainstorm = []
139
+
140
+ # st.rerun()
141
+
142
+ # col1, col2 = st.columns(2)
143
+ # with col1:
144
+ # st.subheader("πŸ“– Story")
145
+ # st.text_area("Generated Story", st.session_state.story, height=300)
146
+
147
+ # with col2:
148
+ # st.subheader("Brainstorming Topics (Click to Select)")
149
+ # if st.session_state.brainstorming_topics:
150
+ # topics_dict = st.session_state.brainstorming_topics[-1]
151
+ # selected = set(st.session_state.selected_from_brainstorm)
152
+ # for label, topic in topics_dict.items():
153
+ # if st.checkbox(topic, key=topic, value=topic in selected):
154
+ # selected.add(topic)
155
+ # else:
156
+ # selected.discard(topic)
157
+ # st.session_state.topics_to_pass = list(selected)
158
+ # st.session_state.selected_from_brainstorm = list(selected)
159
+ # else:
160
+ # st.info("No brainstorming topics yet. Click 'Brainstorm' to start.")
161
+
162
+ # if st.button("πŸš€ Brainstorm"):
163
+ # st.session_state.trigger_brainstorm = True
164
+ # st.rerun()
165
+
166
+ # if st.button("πŸ”™ Back"):
167
+ # if st.session_state.history_index > 0:
168
+ # st.session_state.history_index -= 1
169
+ # state = st.session_state.history[st.session_state.history_index]
170
+ # st.session_state.selected_topics = state["selected_topics"]
171
+ # st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
172
+ # st.session_state.story = state["story"]
173
+ # st.session_state.brainstorming_topics = state["brainstorming_topics"]
174
+ # st.rerun()
175
+ # else:
176
+ # st.warning("You're at the first step.")
177
+
178
+ # if st.button("πŸ”œ Forward"):
179
+ # if st.session_state.history_index < len(st.session_state.history) - 1:
180
+ # st.session_state.history_index += 1
181
+ # state = st.session_state.history[st.session_state.history_index]
182
+ # st.session_state.selected_topics = state["selected_topics"]
183
+ # st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
184
+ # st.session_state.story = state["story"]
185
+ # st.session_state.brainstorming_topics = state["brainstorming_topics"]
186
+ # st.rerun()
187
+ # else:
188
+ # st.warning("You're at the most recent step.")
189
+
190
+ elif "generate-ultimate-story" in tool:
191
+ resp = requests.post(f"{API_URL}/generate-final-story")
192
+ if resp.ok:
193
+ st.session_state.final_story = resp.json()["response"]
194
+ st.markdown(st.session_state.final_story)
195
+ st.success("Final story generated.")
196
+
197
+ elif "generate-image" in tool:
198
+ resp = requests.post(f"{API_URL}/generate-image")
199
+ if resp.ok:
200
+ st.session_state.generated_image = resp.json()["response"]
201
+ st.image(f"data:image/png;base64,{st.session_state.generated_image}", use_column_width=True)
202
+ st.success("Image generated.")
203
 
204
+ else:
205
+ st.markdown(f"πŸ€– {message}")
206
+ if "no any information" not in image_caption.lower():
207
+ st.markdown(f"πŸ€– {image_caption}")
208
+ else:
209
+ st.error("Orchestration failed.")