DrishtiSharma commited on
Commit
0770d1b
·
verified ·
1 Parent(s): 617cdaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -5
app.py CHANGED
@@ -153,8 +153,6 @@ tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])
153
 
154
  # 📜 Tab 1: Essay Generation
155
  with tab1:
156
- #st.subheader("📝 Generate an Essay")
157
-
158
  # Display chat messages from the session
159
  if "messages" not in st.session_state:
160
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
@@ -177,7 +175,7 @@ with tab1:
177
 
178
  # Handle the assistant's response
179
  with st.chat_message("assistant"):
180
- if response and "essay" in response: # Display essay preview and download link
181
  essay = response["essay"]
182
  st.markdown(f"### 📝 Essay Preview ({essay_length} words)")
183
  st.markdown(f"#### {essay['header']}")
@@ -190,12 +188,51 @@ with tab1:
190
  st.markdown("**🖊️ Conclusion:**")
191
  st.markdown(essay["conclusion"])
192
 
193
- # Provide download link for the PDF (only if available)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  pdf_name = response.get("pdf_name")
195
  if pdf_name and os.path.exists(pdf_name):
196
  with open(pdf_name, "rb") as pdf_file:
197
  b64 = base64.b64encode(pdf_file.read()).decode()
198
- href = f"<a href='data:application/octet-stream;base64,{b64}' download='{pdf_name}'>📄 Click here to download the PDF</a>"
199
  st.markdown(href, unsafe_allow_html=True)
200
 
201
  # Save response in session state
@@ -209,6 +246,7 @@ with tab1:
209
  st.error("⚠️ No response received. Please try again.")
210
 
211
 
 
212
  # 📊 Tab 2: Workflow Visualization
213
  with tab2:
214
  #st.subheader("📊 Multi-Agent Essay Writer Workflow Viz")
 
153
 
154
  # 📜 Tab 1: Essay Generation
155
  with tab1:
 
 
156
  # Display chat messages from the session
157
  if "messages" not in st.session_state:
158
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
 
175
 
176
  # Handle the assistant's response
177
  with st.chat_message("assistant"):
178
+ if response and "essay" in response: # Display essay preview and allow editing
179
  essay = response["essay"]
180
  st.markdown(f"### 📝 Essay Preview ({essay_length} words)")
181
  st.markdown(f"#### {essay['header']}")
 
188
  st.markdown("**🖊️ Conclusion:**")
189
  st.markdown(essay["conclusion"])
190
 
191
+ # Editable Section
192
+ st.markdown("## ✍️ Edit Your Essay Below:")
193
+
194
+ # Combine all parts of the essay into one editable text field
195
+ full_essay_text = f"## {essay['header']}\n\n{essay['entry']}\n\n"
196
+ for para in essay["paragraphs"]:
197
+ full_essay_text += f"### {para['sub_header']}\n{para['paragraph']}\n\n"
198
+ full_essay_text += f"**Conclusion:**\n{essay['conclusion']}"
199
+
200
+ # Editable text area for the user
201
+ edited_essay = st.text_area("Edit Your Essay:", value=full_essay_text, height=400)
202
+
203
+ # Save and Download buttons
204
+ col1, col2 = st.columns(2)
205
+
206
+ with col1:
207
+ if st.button("💾 Save as TXT"):
208
+ with open("edited_essay.txt", "w", encoding="utf-8") as file:
209
+ file.write(edited_essay)
210
+ with open("edited_essay.txt", "rb") as file:
211
+ st.download_button(label="⬇️ Download TXT", data=file, file_name="edited_essay.txt", mime="text/plain")
212
+
213
+ with col2:
214
+ if st.button("📄 Save as PDF"):
215
+ from fpdf import FPDF
216
+
217
+ pdf = FPDF()
218
+ pdf.set_auto_page_break(auto=True, margin=15)
219
+ pdf.add_page()
220
+ pdf.set_font("Arial", size=12)
221
+
222
+ for line in edited_essay.split("\n"):
223
+ pdf.cell(200, 10, txt=line, ln=True, align='L')
224
+
225
+ pdf.output("edited_essay.pdf")
226
+
227
+ with open("edited_essay.pdf", "rb") as file:
228
+ st.download_button(label="⬇️ Download PDF", data=file, file_name="edited_essay.pdf", mime="application/pdf")
229
+
230
+ # Provide download link for the original PDF (only if available)
231
  pdf_name = response.get("pdf_name")
232
  if pdf_name and os.path.exists(pdf_name):
233
  with open(pdf_name, "rb") as pdf_file:
234
  b64 = base64.b64encode(pdf_file.read()).decode()
235
+ href = f"<a href='data:application/octet-stream;base64,{b64}' download='{pdf_name}'>📄 Click here to download the original PDF</a>"
236
  st.markdown(href, unsafe_allow_html=True)
237
 
238
  # Save response in session state
 
246
  st.error("⚠️ No response received. Please try again.")
247
 
248
 
249
+
250
  # 📊 Tab 2: Workflow Visualization
251
  with tab2:
252
  #st.subheader("📊 Multi-Agent Essay Writer Workflow Viz")