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
|
159 |
topic = st.text_input("📝 Provide an essay topic:", value="Write an essay on the cultural diversity of India")
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
response =
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
full_essay_text
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
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 |
+
)
|