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

Update app.py

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