DrishtiSharma commited on
Commit
6393614
·
verified ·
1 Parent(s): 44f493d

Update lab/layout2_v3_conversational_style.py

Browse files
lab/layout2_v3_conversational_style.py CHANGED
@@ -155,101 +155,104 @@ with tab1:
155
  with st.chat_message(message["role"]):
156
  st.markdown(message["content"], unsafe_allow_html=True)
157
 
158
- # Use text_input instead of chat_input to allow a default value
159
  topic = st.text_input("📝 Provide an essay topic:", value="Write an essay on the cultural diversity of India")
160
 
161
- if topic:
162
- # Store user message
163
- st.chat_message("user").markdown(topic)
164
- st.session_state["messages"].append({"role": "user", "content": topic})
165
-
166
- with st.spinner(" Generating your essay..."):
167
- response = None
168
- if app:
169
- response = app.write_essay({"topic": topic})
170
- else:
171
- st.error("⚠️ Agents are not initialized. Please check the system or restart the app.")
172
-
173
- # Store assistant response and display it
174
- with st.chat_message("assistant"):
175
- if response and "essay" in response: # Display essay preview and allow editing
176
- essay = response["essay"]
177
-
178
- # Store response in session state
179
- assistant_response = f"Here is your {essay_length}-word essay preview and the download link."
180
- st.session_state["messages"].append({"role": "assistant", "content": assistant_response})
181
-
182
- # Create Two-Column Layout
183
- col1, col2 = st.columns(2)
184
-
185
- with col1:
186
- st.markdown(f"### 📝 Essay Preview ({essay_length} words)")
187
- st.markdown(f"#### {essay['header']}")
188
- st.markdown(essay["entry"])
189
-
190
- for para in essay["paragraphs"]:
191
- st.markdown(f"**{para['sub_header']}**")
192
- st.markdown(para["paragraph"])
193
-
194
- st.markdown("**🖊️ Conclusion:**")
195
- st.markdown(essay["conclusion"])
196
-
197
- with col2:
198
- st.markdown("### ✍️ Edit Your Essay:")
199
-
200
- # Combine all parts of the essay into one editable text field
201
- full_essay_text = f"## {essay['header']}\n\n{essay['entry']}\n\n"
202
- for para in essay["paragraphs"]:
203
- full_essay_text += f"### {para['sub_header']}\n{para['paragraph']}\n\n"
204
- full_essay_text += f"**Conclusion:**\n{essay['conclusion']}"
205
-
206
- # Editable text area for the user
207
- edited_essay = st.text_area("Edit Here:", value=full_essay_text, height=300)
208
-
209
- # Save and Download buttons
210
- save_col1, save_col2 = st.columns(2)
211
-
212
- with save_col1:
213
- if st.button("💾 Save as TXT"):
214
- with open("edited_essay.txt", "w", encoding="utf-8") as file:
215
- file.write(edited_essay)
216
- with open("edited_essay.txt", "rb") as file:
217
- st.download_button(label="⬇️ Download TXT", data=file, file_name="edited_essay.txt", mime="text/plain")
218
-
219
- with save_col2:
220
- if st.button("📄 Save as PDF"):
221
- from fpdf import FPDF
222
-
223
- pdf = FPDF()
224
- pdf.set_auto_page_break(auto=True, margin=15)
225
- pdf.add_page()
226
- pdf.set_font("Arial", size=12)
227
-
228
- for line in edited_essay.split("\n"):
229
- pdf.cell(200, 10, txt=line, ln=True, align='L')
230
-
231
- pdf.output("edited_essay.pdf")
232
-
233
- with open("edited_essay.pdf", "rb") as file:
234
- st.download_button(label="⬇️ Download PDF", data=file, file_name="edited_essay.pdf", mime="application/pdf")
235
-
236
- # Provide download link for the original PDF
237
- pdf_name = response.get("pdf_name")
238
- if pdf_name and os.path.exists(pdf_name):
239
- with open(pdf_name, "rb") as pdf_file:
240
- b64 = base64.b64encode(pdf_file.read()).decode()
241
- href = f"<a href='data:application/octet-stream;base64,{b64}' download='{pdf_name}'>📄 Click here to download the original PDF</a>"
242
- st.markdown(href, unsafe_allow_html=True)
243
-
244
- # Save response in session state
245
- st.session_state["messages"].append(
246
- {"role": "assistant", "content": f"Here is your {essay_length}-word essay preview and the download link."}
247
- )
248
- elif response:
249
- st.markdown(response["response"])
250
- st.session_state["messages"].append({"role": "assistant", "content": response["response"]})
251
- else:
252
- st.error("⚠️ No response received. Please try again.")
 
 
 
253
 
254
 
255
  # 📊 Tab 2: Workflow Visualization
@@ -280,4 +283,4 @@ st.markdown(
280
  </div>
281
  """,
282
  unsafe_allow_html=True,
283
- )
 
155
  with st.chat_message(message["role"]):
156
  st.markdown(message["content"], unsafe_allow_html=True)
157
 
158
+ # Use text_input to allow a default value, but do not trigger generation immediately
159
  topic = st.text_input("📝 Provide an essay topic:", value="Write an essay on the cultural diversity of India")
160
 
161
+ # Add a button to trigger essay generation
162
+ if st.button("Generate Essay"):
163
+ if topic:
164
+ # Store user message in the chat
165
+ st.chat_message("user").markdown(topic)
166
+ st.session_state["messages"].append({"role": "user", "content": topic})
167
+
168
+ with st.spinner("⏳ Generating your essay..."):
169
+ response = None
170
+ if app:
171
+ response = app.write_essay({"topic": topic})
172
+ else:
173
+ st.error("⚠️ Agents are not initialized. Please check the system or restart the app.")
174
+
175
+ # Store assistant response and display it
176
+ with st.chat_message("assistant"):
177
+ if response and "essay" in response: # Display essay preview and allow editing
178
+ essay = response["essay"]
179
+
180
+ # Store response in session state
181
+ assistant_response = f"Here is your {essay_length}-word essay preview and the download link."
182
+ st.session_state["messages"].append({"role": "assistant", "content": assistant_response})
183
+
184
+ # Create Two-Column Layout
185
+ col1, col2 = st.columns(2)
186
+
187
+ with col1:
188
+ st.markdown(f"### 📝 Essay Preview ({essay_length} words)")
189
+ st.markdown(f"#### {essay['header']}")
190
+ st.markdown(essay["entry"])
191
+
192
+ for para in essay["paragraphs"]:
193
+ st.markdown(f"**{para['sub_header']}**")
194
+ st.markdown(para["paragraph"])
195
+
196
+ st.markdown("**🖊️ Conclusion:**")
197
+ st.markdown(essay["conclusion"])
198
+
199
+ with col2:
200
+ st.markdown("### ✍️ Edit Your Essay:")
201
+
202
+ # Combine all parts of the essay into one editable text field
203
+ full_essay_text = f"## {essay['header']}\n\n{essay['entry']}\n\n"
204
+ for para in essay["paragraphs"]:
205
+ full_essay_text += f"### {para['sub_header']}\n{para['paragraph']}\n\n"
206
+ full_essay_text += f"**Conclusion:**\n{essay['conclusion']}"
207
+
208
+ # Editable text area for the user
209
+ edited_essay = st.text_area("Edit Here:", value=full_essay_text, height=300)
210
+
211
+ # Save and Download buttons
212
+ save_col1, save_col2 = st.columns(2)
213
+
214
+ with save_col1:
215
+ if st.button("💾 Save as TXT"):
216
+ with open("edited_essay.txt", "w", encoding="utf-8") as file:
217
+ file.write(edited_essay)
218
+ with open("edited_essay.txt", "rb") as file:
219
+ st.download_button(label="⬇️ Download TXT", data=file, file_name="edited_essay.txt", mime="text/plain")
220
+
221
+ with save_col2:
222
+ if st.button("📄 Save as PDF"):
223
+ from fpdf import FPDF
224
+
225
+ pdf = FPDF()
226
+ pdf.set_auto_page_break(auto=True, margin=15)
227
+ pdf.add_page()
228
+ pdf.set_font("Arial", size=12)
229
+
230
+ for line in edited_essay.split("\n"):
231
+ pdf.cell(200, 10, txt=line, ln=True, align='L')
232
+
233
+ pdf.output("edited_essay.pdf")
234
+
235
+ with open("edited_essay.pdf", "rb") as file:
236
+ st.download_button(label="⬇️ Download PDF", data=file, file_name="edited_essay.pdf", mime="application/pdf")
237
+
238
+ # Provide download link for the original PDF
239
+ pdf_name = response.get("pdf_name")
240
+ if pdf_name and os.path.exists(pdf_name):
241
+ with open(pdf_name, "rb") as pdf_file:
242
+ b64 = base64.b64encode(pdf_file.read()).decode()
243
+ href = f"<a href='data:application/octet-stream;base64,{b64}' download='{pdf_name}'>📄 Click here to download the original PDF</a>"
244
+ st.markdown(href, unsafe_allow_html=True)
245
+
246
+ # Save response in session state
247
+ st.session_state["messages"].append(
248
+ {"role": "assistant", "content": f"Here is your {essay_length}-word essay preview and the download link."}
249
+ )
250
+ elif response:
251
+ st.markdown(response["response"])
252
+ st.session_state["messages"].append({"role": "assistant", "content": response["response"]})
253
+ else:
254
+ st.error("⚠️ No response received. Please try again.")
255
+
256
 
257
 
258
  # 📊 Tab 2: Workflow Visualization
 
283
  </div>
284
  """,
285
  unsafe_allow_html=True,
286
+ )