awacke1 commited on
Commit
5984e6e
1 Parent(s): d0dc9c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -188,6 +188,46 @@ def delete_record(container, record):
188
 
189
  # 💾 Save to Cosmos DB - Preserving data for future generations (or just until the next update)
190
  def save_to_cosmos_db(container, query, response1, response2):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  try:
192
  if container:
193
  record = {
@@ -555,7 +595,67 @@ def display_file_viewer(file_path):
555
  mime="text/markdown"
556
  )
557
 
 
 
558
  def display_file_editor(file_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559
  """Display file content in editor with save functionality"""
560
  # Initialize file content in session state if not already present
561
  if 'file_content' not in st.session_state:
 
188
 
189
  # 💾 Save to Cosmos DB - Preserving data for future generations (or just until the next update)
190
  def save_to_cosmos_db(container, query, response1, response2):
191
+ try:
192
+ if container:
193
+ # Generate a unique ID that includes a timestamp
194
+ timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
195
+ unique_uuid = str(uuid.uuid4())
196
+ new_id = f"{timestamp}-{unique_uuid}"
197
+
198
+ # Create new document with proper name field
199
+ record = {
200
+ "id": new_id,
201
+ "name": new_id, # Set name equal to ID to avoid null name error
202
+ "query": query,
203
+ "response1": response1,
204
+ "response2": response2,
205
+ "timestamp": datetime.utcnow().isoformat(),
206
+ "type": "ai_response", # Add document type for better organization
207
+ "version": "1.0"
208
+ }
209
+
210
+ try:
211
+ # Create the new document
212
+ container.create_item(body=record)
213
+ st.success(f"Record saved successfully with ID: {record['id']}")
214
+ # Refresh the documents display
215
+ st.session_state.documents = get_documents(container)
216
+ except exceptions.CosmosHttpResponseError as e:
217
+ st.error(f"Error saving record to Cosmos DB: {e}")
218
+ else:
219
+ st.error("Cosmos DB container is not initialized.")
220
+ except Exception as e:
221
+ st.error(f"An unexpected error occurred: {str(e)}")
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+ def save_to_cosmos_db_old(container, query, response1, response2):
231
  try:
232
  if container:
233
  record = {
 
595
  mime="text/markdown"
596
  )
597
 
598
+
599
+
600
  def display_file_editor(file_path):
601
+ """Display file content in both Markdown and Code Editor views"""
602
+ # Initialize file content in session state if not already present
603
+ if 'file_content' not in st.session_state:
604
+ st.session_state.file_content = {}
605
+
606
+ # Load content if not in session state or if it's a different file
607
+ if file_path not in st.session_state.file_content:
608
+ content = load_file_content(file_path)
609
+ if content is not None:
610
+ st.session_state.file_content[file_path] = content
611
+ else:
612
+ return
613
+
614
+ st.markdown("### ✏️ File Editor")
615
+ st.markdown(f"**Editing:** {file_path}")
616
+
617
+ # Create tabs for different views
618
+ markdown_tab, code_tab = st.tabs(["Markdown View", "Code Editor"])
619
+
620
+ with markdown_tab:
621
+ st.markdown("### 📄 Markdown Preview")
622
+ st.markdown(st.session_state.file_content[file_path])
623
+
624
+ with code_tab:
625
+ st.markdown("### 💻 Code Editor")
626
+ # Create a unique key for the text area
627
+ editor_key = f"editor_{hash(file_path)}"
628
+
629
+ # Editor with syntax highlighting for markdown
630
+ new_content = st.text_area(
631
+ "Edit content below:",
632
+ value=st.session_state.file_content[file_path],
633
+ height=400,
634
+ key=editor_key
635
+ )
636
+
637
+ # Add save and download buttons below both views
638
+ col1, col2 = st.columns([1, 5])
639
+ with col1:
640
+ if st.button("💾 Save Changes"):
641
+ if save_file_content(file_path, new_content):
642
+ st.session_state.file_content[file_path] = new_content
643
+ st.success("File saved successfully! 🎉")
644
+ time.sleep(1)
645
+ st.rerun()
646
+
647
+ with col2:
648
+ st.download_button(
649
+ label="⬇️ Download File",
650
+ data=new_content,
651
+ file_name=os.path.basename(file_path),
652
+ mime="text/markdown"
653
+ )
654
+
655
+
656
+
657
+
658
+ def display_file_editor_old(file_path):
659
  """Display file content in editor with save functionality"""
660
  # Initialize file content in session state if not already present
661
  if 'file_content' not in st.session_state: