subashdvorak commited on
Commit
9eb7123
·
verified ·
1 Parent(s): d072e61

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +32 -116
src/streamlit_app.py CHANGED
@@ -3,7 +3,7 @@ import requests
3
  import base64
4
  from streamlit import switch_page
5
 
6
- API_URL = "https://subashdvorak-brainstroming-fast-api.hf.space" # 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")
@@ -35,24 +35,23 @@ for key, val in session_defaults.items():
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'], 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
  else:
49
  print('File not uploaded')
50
 
51
-
52
  if st.button("Submit"):
53
  if not st.session_state.details_completed:
54
- # if st.session_state.details_completed == True:
55
- resp = requests.post(f"{API_URL}/context-analysis", json={"message": msg})
56
  if resp.ok:
57
  response = resp.json()
58
  st.markdown(response.get('response'))
@@ -63,7 +62,9 @@ if st.button("Submit"):
63
  else:
64
  st.error("Context analysis failed.")
65
  else:
66
- resp = requests.post(f"{API_URL}/orchestration", json={"message": msg,"image_base64": st.session_state.base64_images})
 
 
67
  if resp.ok:
68
  response = resp.json()
69
  tool = response.get("tool_response")
@@ -73,136 +74,51 @@ if st.button("Submit"):
73
 
74
  # ========== Tool Handling ==========
75
  if "ideation" in tool:
76
- resp = requests.post(f"{API_URL}/ideation")
 
77
  if resp.ok:
78
  st.session_state.final_ideation = resp.json()["response"]["improver_response"][-1]
79
-
80
- if len(st.session_state.final_ideation)>0:
81
- # st.subheader("Final Ideations")
82
- st.markdown(f"*Idea:1* - {eval(st.session_state.final_ideation)[0]}")
83
- st.markdown(f"*Idea:2* - {eval(st.session_state.final_ideation)[1]}")
84
- st.markdown(f"*Idea:3* - {eval(st.session_state.final_ideation)[2]}")
85
- st.markdown(f"*Idea:4* - {eval(st.session_state.final_ideation)[3]}")
86
  st.success("Ideas generated successfully.")
 
 
87
 
88
  elif "human-idea-refining" in tool:
89
- resp = requests.post(f"{API_URL}/human-idea-refining", json={"query": message})
 
90
  if resp.ok:
91
  refined = resp.json()['response']
92
  st.session_state.human_interactions.append(refined)
93
  st.markdown(st.session_state.human_interactions[-1])
94
  st.success("Ideas refined.")
 
 
95
 
96
  elif "generate-story" in tool:
 
97
  switch_page("pages/brainstorm.py")
98
- # def call_brainstorming_api():
99
- # response = requests.post(
100
- # f"{API_URL}/brainstorm",
101
- # params={"thread_id": "my-session"},
102
- # json={
103
- # "preferred_topics": st.session_state.topics_to_pass,
104
- # "image_base64_list": st.session_state.base64_images
105
- # }
106
- # )
107
- # if response.ok:
108
- # result_json = response.json()
109
- # data = result_json.get("response", {})
110
- # st.session_state.story = data.get("stories", [""])[-1]
111
- # st.session_state.brainstorming_topics = data.get("brainstroming_topics", [])
112
- # else:
113
- # st.error("❌ API call failed.")
114
- # st.write(response.text)
115
-
116
- # call_brainstorming_api()
117
-
118
- # uploaded_files = st.file_uploader("📂 Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True, key="brainstorm_image_uploader")
119
- # if uploaded_files:
120
- # base64_images = [base64.b64encode(f.read()).decode('utf-8') for f in uploaded_files]
121
- # st.session_state.base64_images = base64_images
122
-
123
-
124
-
125
- # # Triggered Brainstorming
126
- # if st.session_state.trigger_brainstorm:
127
- # st.session_state.trigger_brainstorm = False # reset flag
128
- # # Save history
129
- # if st.session_state.history_index < len(st.session_state.history) - 1:
130
- # st.session_state.history = st.session_state.history[:st.session_state.history_index + 1]
131
- # st.session_state.history.append({
132
- # "selected_topics": st.session_state.selected_topics.copy(),
133
- # "selected_from_brainstorm": st.session_state.selected_from_brainstorm.copy(),
134
- # "story": st.session_state.story,
135
- # "brainstorming_topics": st.session_state.brainstorming_topics.copy()
136
- # })
137
- # st.session_state.history_index += 1
138
-
139
- # # Update selection
140
- # st.session_state.selected_topics.extend(st.session_state.selected_from_brainstorm)
141
- # st.session_state.selected_from_brainstorm = []
142
-
143
- # st.rerun()
144
-
145
- # col1, col2 = st.columns(2)
146
- # with col1:
147
- # st.subheader("📖 Story")
148
- # st.text_area("Generated Story", st.session_state.story, height=300)
149
-
150
- # with col2:
151
- # st.subheader("Brainstorming Topics (Click to Select)")
152
- # if st.session_state.brainstorming_topics:
153
- # topics_dict = st.session_state.brainstorming_topics[-1]
154
- # selected = set(st.session_state.selected_from_brainstorm)
155
- # for label, topic in topics_dict.items():
156
- # if st.checkbox(topic, key=topic, value=topic in selected):
157
- # selected.add(topic)
158
- # else:
159
- # selected.discard(topic)
160
- # st.session_state.topics_to_pass = list(selected)
161
- # st.session_state.selected_from_brainstorm = list(selected)
162
- # else:
163
- # st.info("No brainstorming topics yet. Click 'Brainstorm' to start.")
164
-
165
- # if st.button("🚀 Brainstorm"):
166
- # st.session_state.trigger_brainstorm = True
167
- # st.rerun()
168
-
169
- # if st.button("🔙 Back"):
170
- # if st.session_state.history_index > 0:
171
- # st.session_state.history_index -= 1
172
- # state = st.session_state.history[st.session_state.history_index]
173
- # st.session_state.selected_topics = state["selected_topics"]
174
- # st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
175
- # st.session_state.story = state["story"]
176
- # st.session_state.brainstorming_topics = state["brainstorming_topics"]
177
- # st.rerun()
178
- # else:
179
- # st.warning("You're at the first step.")
180
-
181
- # if st.button("🔜 Forward"):
182
- # if st.session_state.history_index < len(st.session_state.history) - 1:
183
- # st.session_state.history_index += 1
184
- # state = st.session_state.history[st.session_state.history_index]
185
- # st.session_state.selected_topics = state["selected_topics"]
186
- # st.session_state.selected_from_brainstorm = state["selected_from_brainstorm"]
187
- # st.session_state.story = state["story"]
188
- # st.session_state.brainstorming_topics = state["brainstorming_topics"]
189
- # st.rerun()
190
- # else:
191
- # st.warning("You're at the most recent step.")
192
 
193
  elif "generate-ultimate-story" in tool:
194
- resp = requests.post(f"{API_URL}/generate-final-story")
 
195
  if resp.ok:
196
  st.session_state.final_story = resp.json()["response"]
197
  st.markdown(st.session_state.final_story)
198
  st.success("Final story generated.")
 
 
199
 
200
  elif "generate-image" in tool:
201
- resp = requests.post(f"{API_URL}/generate-image")
 
202
  if resp.ok:
203
  st.session_state.generated_image = resp.json()["response"]
204
  st.image(f"data:image/png;base64,{st.session_state.generated_image}", use_column_width=True)
205
  st.success("Image generated.")
 
 
206
 
207
  else:
208
  st.markdown(f"🤖 {message}")
 
3
  import base64
4
  from streamlit import switch_page
5
 
6
+ API_URL = "https://subashdvorak-brainstroming-fast-api.hf.space"
7
 
8
  st.set_page_config(page_title="AI Business Ideation Platform", layout="wide")
9
  st.header("Welcome to RT-GENAI")
 
35
  st.subheader("How can I help you?")
36
  msg = st.text_input("")
37
 
38
+ # Image uploader
39
  uploaded_files = st.file_uploader("📂 Upload reference images (optional)", type=['png', 'jpg', 'jpeg'], key="initial_image_uploader")
40
 
41
  if uploaded_files:
42
  base64_images = []
43
+ with st.spinner("Encoding uploaded images..."):
44
+ for file in uploaded_files:
45
+ base64_images.append(base64.b64encode(file.read()).decode('utf-8'))
46
+ st.session_state.base64_images = base64_images
 
47
  else:
48
  print('File not uploaded')
49
 
50
+ # Submit button
51
  if st.button("Submit"):
52
  if not st.session_state.details_completed:
53
+ with st.spinner("Analyzing your business context..."):
54
+ resp = requests.post(f"{API_URL}/context-analysis", json={"message": msg})
55
  if resp.ok:
56
  response = resp.json()
57
  st.markdown(response.get('response'))
 
62
  else:
63
  st.error("Context analysis failed.")
64
  else:
65
+ with st.spinner("Processing orchestration..."):
66
+ resp = requests.post(f"{API_URL}/orchestration", json={"message": msg, "image_base64": st.session_state.base64_images})
67
+
68
  if resp.ok:
69
  response = resp.json()
70
  tool = response.get("tool_response")
 
74
 
75
  # ========== Tool Handling ==========
76
  if "ideation" in tool:
77
+ with st.spinner("Generating ideas..."):
78
+ resp = requests.post(f"{API_URL}/ideation")
79
  if resp.ok:
80
  st.session_state.final_ideation = resp.json()["response"]["improver_response"][-1]
81
+ ideas = eval(st.session_state.final_ideation)
82
+ for i, idea in enumerate(ideas, 1):
83
+ st.markdown(f"*Idea:{i}* - {idea}")
 
 
 
 
84
  st.success("Ideas generated successfully.")
85
+ else:
86
+ st.error("Failed to generate ideas.")
87
 
88
  elif "human-idea-refining" in tool:
89
+ with st.spinner("Refining ideas with your input..."):
90
+ resp = requests.post(f"{API_URL}/human-idea-refining", json={"query": message})
91
  if resp.ok:
92
  refined = resp.json()['response']
93
  st.session_state.human_interactions.append(refined)
94
  st.markdown(st.session_state.human_interactions[-1])
95
  st.success("Ideas refined.")
96
+ else:
97
+ st.error("Idea refinement failed.")
98
 
99
  elif "generate-story" in tool:
100
+ st.info("Redirecting to Brainstorm page...")
101
  switch_page("pages/brainstorm.py")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  elif "generate-ultimate-story" in tool:
104
+ with st.spinner("Generating final story..."):
105
+ resp = requests.post(f"{API_URL}/generate-final-story")
106
  if resp.ok:
107
  st.session_state.final_story = resp.json()["response"]
108
  st.markdown(st.session_state.final_story)
109
  st.success("Final story generated.")
110
+ else:
111
+ st.error("Failed to generate story.")
112
 
113
  elif "generate-image" in tool:
114
+ with st.spinner("Generating image from the idea..."):
115
+ resp = requests.post(f"{API_URL}/generate-image")
116
  if resp.ok:
117
  st.session_state.generated_image = resp.json()["response"]
118
  st.image(f"data:image/png;base64,{st.session_state.generated_image}", use_column_width=True)
119
  st.success("Image generated.")
120
+ else:
121
+ st.error("Image generation failed.")
122
 
123
  else:
124
  st.markdown(f"🤖 {message}")