DrishtiSharma commited on
Commit
9da3128
·
verified ·
1 Parent(s): 184d5d6

Update app.py

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