awacke1 commited on
Commit
c222504
Β·
verified Β·
1 Parent(s): e39cc7e

Delete backup5.Arxiv.app.py

Browse files
Files changed (1) hide show
  1. backup5.Arxiv.app.py +0 -621
backup5.Arxiv.app.py DELETED
@@ -1,621 +0,0 @@
1
- import streamlit as st
2
- from azure.cosmos import CosmosClient, exceptions
3
- import os
4
- import pandas as pd
5
- import traceback
6
- import shutil
7
- from github import Github
8
- from git import Repo
9
- from datetime import datetime
10
- import base64
11
- import json
12
- import uuid # 🎲 For generating unique IDs
13
- from urllib.parse import quote # πŸ”— For encoding URLs
14
- from gradio_client import Client # 🌐 For connecting to Gradio apps
15
-
16
- # πŸŽ‰ Welcome to our fun-filled Cosmos DB and GitHub Integration app!
17
- st.set_page_config(layout="wide")
18
-
19
- # 🌌 Cosmos DB configuration
20
- ENDPOINT = "https://acae-afd.documents.azure.com:443/"
21
- DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
22
- CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
23
- Key = os.environ.get("Key") # πŸ”‘ Don't forget your key!
24
-
25
- # 🏠 Your local app URL (Change this to your app's URL)
26
- LOCAL_APP_URL = "https://huggingface.co/spaces/awacke1/AzureCosmosDBUI"
27
-
28
- # πŸ€– OpenAI configuration
29
- #openai.api_key = os.environ.get("OPENAI_API_KEY")
30
- #MODEL = "gpt-3.5-turbo" # Replace with your desired model
31
-
32
- # πŸ™ GitHub configuration
33
- def download_github_repo(url, local_path):
34
- # 🚚 Let's download that GitHub repo!
35
- if os.path.exists(local_path):
36
- shutil.rmtree(local_path)
37
- Repo.clone_from(url, local_path)
38
-
39
- def create_zip_file(source_dir, output_filename):
40
- # πŸ“¦ Zipping up files like a pro!
41
- shutil.make_archive(output_filename, 'zip', source_dir)
42
-
43
- def create_repo(g, repo_name):
44
- # πŸ› οΈ Creating a new GitHub repo. Magic!
45
- user = g.get_user()
46
- return user.create_repo(repo_name)
47
-
48
- def push_to_github(local_path, repo, github_token):
49
- # πŸš€ Pushing code to GitHub. Hold on tight!
50
- repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
51
- local_repo = Repo(local_path)
52
-
53
- if 'origin' in [remote.name for remote in local_repo.remotes]:
54
- origin = local_repo.remote('origin')
55
- origin.set_url(repo_url)
56
- else:
57
- origin = local_repo.create_remote('origin', repo_url)
58
-
59
- if not local_repo.heads:
60
- local_repo.git.checkout('-b', 'main')
61
- current_branch = 'main'
62
- else:
63
- current_branch = local_repo.active_branch.name
64
-
65
- local_repo.git.add(A=True)
66
-
67
- if local_repo.is_dirty():
68
- local_repo.git.commit('-m', 'Initial commit')
69
-
70
- origin.push(refspec=f'{current_branch}:{current_branch}')
71
-
72
- def get_base64_download_link(file_path, file_name):
73
- # πŸ§™β€β™‚οΈ Generating a magical download link!
74
- with open(file_path, "rb") as file:
75
- contents = file.read()
76
- base64_encoded = base64.b64encode(contents).decode()
77
- return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">⬇️ Download {file_name}</a>'
78
-
79
-
80
- # 🧭 New functions for dynamic sidebar navigation
81
- def get_databases(client):
82
- # πŸ“š Fetching list of databases. So many options!
83
- return [db['id'] for db in client.list_databases()]
84
-
85
- def get_containers(database):
86
- # πŸ“‚ Getting containers. Containers within containers!
87
- return [container['id'] for container in database.list_containers()]
88
-
89
- def get_documents(container, limit=None):
90
- # πŸ“ Retrieving documents. Shhh, don't tell anyone!
91
- query = "SELECT * FROM c ORDER BY c._ts DESC"
92
- items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
93
- return items
94
-
95
-
96
- # 🌟 Cosmos DB functions
97
- def insert_record(container, record):
98
- try:
99
- container.create_item(body=record)
100
- return True, "Record inserted successfully! πŸŽ‰"
101
- except exceptions.CosmosHttpResponseError as e:
102
- return False, f"HTTP error occurred: {str(e)} 🚨"
103
- except Exception as e:
104
- return False, f"An unexpected error occurred: {str(e)} 😱"
105
-
106
- def update_record(container, updated_record):
107
- try:
108
- container.upsert_item(body=updated_record)
109
- return True, f"Record with id {updated_record['id']} successfully updated. πŸ› οΈ"
110
- except exceptions.CosmosHttpResponseError as e:
111
- return False, f"HTTP error occurred: {str(e)} 🚨"
112
- except Exception as e:
113
- return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
114
-
115
- def delete_record(container, name, id):
116
- try:
117
- container.delete_item(item=id, partition_key=id)
118
- return True, f"Successfully deleted record with name: {name} and id: {id} πŸ—‘οΈ"
119
- except exceptions.CosmosResourceNotFoundError:
120
- return False, f"Record with id {id} not found. It may have been already deleted. πŸ•΅οΈβ€β™‚οΈ"
121
- except exceptions.CosmosHttpResponseError as e:
122
- return False, f"HTTP error occurred: {str(e)} 🚨"
123
- except Exception as e:
124
- return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
125
-
126
- # 🎲 Function to generate a unique UUID
127
- def generate_unique_id():
128
- # πŸ§™β€β™‚οΈ Generating a unique UUID!
129
- return str(uuid.uuid4())
130
-
131
- # πŸ“¦ Function to archive current container
132
- def archive_current_container(database_name, container_name, client):
133
- try:
134
- base_dir = "./cosmos_archive_current_container"
135
- if os.path.exists(base_dir):
136
- shutil.rmtree(base_dir)
137
- os.makedirs(base_dir)
138
-
139
- db_client = client.get_database_client(database_name)
140
- container_client = db_client.get_container_client(container_name)
141
- items = list(container_client.read_all_items())
142
-
143
- container_dir = os.path.join(base_dir, container_name)
144
- os.makedirs(container_dir)
145
-
146
- for item in items:
147
- item_id = item.get('id', f"unknown_{datetime.now().strftime('%Y%m%d%H%M%S')}")
148
- with open(os.path.join(container_dir, f"{item_id}.json"), 'w') as f:
149
- json.dump(item, f, indent=2)
150
-
151
- archive_name = f"{container_name}_archive_{datetime.now().strftime('%Y%m%d%H%M%S')}"
152
- shutil.make_archive(archive_name, 'zip', base_dir)
153
-
154
- return get_base64_download_link(f"{archive_name}.zip", f"{archive_name}.zip")
155
- except Exception as e:
156
- return f"An error occurred while archiving data: {str(e)} 😒"
157
-
158
- # πŸ” Search Glossary function
159
- def search_glossary(query):
160
- # πŸ•΅οΈβ€β™‚οΈ Searching the glossary for: query
161
- all_results = ""
162
- st.markdown(f"- {query}")
163
-
164
- #database_choice Literal['Semantic Search', 'Arxiv Search - Latest - (EXPERIMENTAL)'] Default: "Semantic Search"
165
- #llm_model_picked Literal['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None'] Default: "mistralai/Mistral-7B-Instruct-v0.2"
166
-
167
- # πŸ” Run 1 - ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM
168
- client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
169
- response2 = client.predict(
170
- message=query, # str in 'parameter_13' Textbox component
171
- llm_results_use=5,
172
- database_choice="Semantic Search",
173
- llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
174
- api_name="/update_with_rag_md"
175
- )
176
-
177
- #llm_model_picked Literal['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None'] Default: "mistralai/Mistral-7B-Instruct-v0.2"
178
-
179
-
180
- client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
181
- result = client.predict(
182
- prompt=query,
183
- llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
184
- stream_outputs=True,
185
- api_name="/ask_llm"
186
- )
187
-
188
- st.write('πŸ” Run of Multi-Agent System Paper Summary Spec is Complete')
189
- st.markdown(response2)
190
-
191
- # ArXiv searcher ~-<>-~ Paper References - Update with RAG
192
- client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
193
- response1 = client.predict(
194
- query,
195
- 10,
196
- "Semantic Search - up to 10 Mar 2024", # Search Source Dropdown component
197
- "mistralai/Mixtral-8x7B-Instruct-v0.1", # LLM Model Dropdown component
198
- api_name="/update_with_rag_md"
199
- )
200
- st.write('πŸ” Run of Multi-Agent System Paper References is Complete')
201
- responseall = response2 + response1[0] + response1[1]
202
- st.markdown(responseall)
203
- return responseall
204
-
205
- # πŸ“ Function to process text input
206
- def process_text(text_input):
207
- if text_input:
208
- if 'messages' not in st.session_state:
209
- st.session_state.messages = []
210
-
211
- st.session_state.messages.append({"role": "user", "content": text_input})
212
-
213
- with st.chat_message("user"):
214
- st.markdown(text_input)
215
-
216
- with st.chat_message("assistant"):
217
-
218
- search_glossary(text_input)
219
-
220
-
221
- useOpenAI=False
222
- if useOpenAI:
223
- completion = openai.ChatCompletion.create(
224
- model=MODEL,
225
- messages=[
226
- {"role": m["role"], "content": m["content"]}
227
- for m in st.session_state.messages
228
- ],
229
- stream=False
230
- )
231
- return_text = completion.choices[0].message.content
232
- st.write("Assistant: " + return_text)
233
-
234
-
235
- filename = generate_filename(text_input, "md")
236
- create_and_save_file(return_text, file_type="md", prompt=text_input, is_image=False, should_save=True)
237
- st.session_state.messages.append({"role": "assistant", "content": return_text})
238
-
239
- # πŸ“„ Function to generate a filename
240
- def generate_filename(text, file_type):
241
- # πŸ“ Generate a filename based on the text input
242
- safe_text = "".join(c if c.isalnum() or c in (' ', '.', '_') else '_' for c in text)
243
- safe_text = "_".join(safe_text.strip().split())
244
- filename = f"{safe_text}.{file_type}"
245
- return filename
246
-
247
- # 🏷️ Function to extract markdown title
248
- def extract_markdown_title(content):
249
- # πŸ” Extract the first markdown heading as the title
250
- lines = content.splitlines()
251
- for line in lines:
252
- if line.startswith('#'):
253
- return line.lstrip('#').strip()
254
- return None
255
-
256
- # πŸ’Ύ Function to create and save a file
257
- def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
258
- """
259
- Combines file name generation and file creation into one function.
260
- If the file is a markdown file, extracts the title from the content (if available) and uses it for the filename.
261
- """
262
- if not should_save:
263
- return None
264
-
265
- # Step 1: Generate filename based on the prompt or content
266
- filename = generate_filename(prompt if prompt else content, file_type)
267
-
268
- # Step 2: If it's a markdown file, check if it has a title (e.g., # Heading in markdown)
269
- if file_type == "md":
270
- title_from_content = extract_markdown_title(content)
271
- if title_from_content:
272
- filename = generate_filename(title_from_content, file_type)
273
-
274
- # Step 3: Save the file
275
- with open(filename, "w", encoding="utf-8") as f:
276
- if is_image:
277
- f.write(content)
278
- else:
279
- f.write(prompt + "\n\n" + content)
280
-
281
- return filename
282
-
283
- # 🎈 Let's modify the main app to be more fun!
284
- def main():
285
- st.title("πŸ™Git🌌CosmosπŸ’« - Azure Cosmos DB and Github Agent")
286
-
287
- # 🚦 Initialize session state
288
- if 'logged_in' not in st.session_state:
289
- st.session_state.logged_in = False
290
- if 'selected_records' not in st.session_state:
291
- st.session_state.selected_records = []
292
- if 'client' not in st.session_state:
293
- st.session_state.client = None
294
- if 'selected_database' not in st.session_state:
295
- st.session_state.selected_database = None
296
- if 'selected_container' not in st.session_state:
297
- st.session_state.selected_container = None
298
- if 'selected_document_id' not in st.session_state:
299
- st.session_state.selected_document_id = None
300
- if 'current_index' not in st.session_state:
301
- st.session_state.current_index = 0
302
- if 'cloned_doc' not in st.session_state:
303
- st.session_state.cloned_doc = None
304
-
305
- # βš™οΈ q= Run ArXiv search from query parameters
306
- try:
307
- query_params = st.query_params
308
- query = query_params.get('q') or query_params.get('query') or ''
309
- if query:
310
- # πŸ•΅οΈβ€β™‚οΈ We have a query! Let's process it!
311
- process_text(query)
312
- st.stop() # Stop further execution
313
- except Exception as e:
314
- st.markdown(' ')
315
-
316
- # πŸ” Automatic Login
317
- if Key:
318
- st.session_state.primary_key = Key
319
- st.session_state.logged_in = True
320
- else:
321
- st.error("Cosmos DB Key is not set in environment variables. πŸ”‘βŒ")
322
- return # Can't proceed without a key
323
-
324
- if st.session_state.logged_in:
325
- # 🌌 Initialize Cosmos DB client
326
- try:
327
- if st.session_state.client is None:
328
- st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
329
-
330
- # πŸ—„οΈ Sidebar for database, container, and document selection
331
- st.sidebar.title("πŸ™Git🌌CosmosπŸ’«πŸ—„οΈNavigator")
332
-
333
- databases = get_databases(st.session_state.client)
334
- selected_db = st.sidebar.selectbox("πŸ—ƒοΈ Select Database", databases)
335
-
336
- if selected_db != st.session_state.selected_database:
337
- st.session_state.selected_database = selected_db
338
- st.session_state.selected_container = None
339
- st.session_state.selected_document_id = None
340
- st.session_state.current_index = 0
341
- st.rerun()
342
-
343
- if st.session_state.selected_database:
344
- database = st.session_state.client.get_database_client(st.session_state.selected_database)
345
- containers = get_containers(database)
346
- selected_container = st.sidebar.selectbox("πŸ“ Select Container", containers)
347
-
348
- if selected_container != st.session_state.selected_container:
349
- st.session_state.selected_container = selected_container
350
- st.session_state.selected_document_id = None
351
- st.session_state.current_index = 0
352
- st.rerun()
353
-
354
- if st.session_state.selected_container:
355
- container = database.get_container_client(st.session_state.selected_container)
356
-
357
- # πŸ“¦ Add Export button
358
- if st.button("πŸ“¦ Export Container Data"):
359
- download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
360
- if download_link.startswith('<a'):
361
- st.markdown(download_link, unsafe_allow_html=True)
362
- else:
363
- st.error(download_link)
364
-
365
- # Fetch documents
366
- documents = get_documents(container)
367
- total_docs = len(documents)
368
-
369
- if total_docs > 5:
370
- documents_to_display = documents[:5]
371
- st.info("Showing top 5 most recent documents.")
372
- else:
373
- documents_to_display = documents
374
- st.info(f"Showing all {len(documents_to_display)} documents.")
375
-
376
- if documents_to_display:
377
- # 🎨 Add Viewer/Editor selection
378
- view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
379
- selected_view = st.selectbox("Select Viewer/Editor", view_options, index=2)
380
-
381
- if selected_view == 'Show as Markdown':
382
- # πŸ–ŒοΈ Show each record as Markdown with navigation
383
- total_docs = len(documents)
384
- doc = documents[st.session_state.current_index]
385
- st.markdown(f"#### Document ID: {doc.get('id', '')}")
386
-
387
- # πŸ•΅οΈβ€β™‚οΈ Let's extract values from the JSON that have at least one space
388
- values_with_space = []
389
- def extract_values(obj):
390
- if isinstance(obj, dict):
391
- for k, v in obj.items():
392
- extract_values(v)
393
- elif isinstance(obj, list):
394
- for item in obj:
395
- extract_values(item)
396
- elif isinstance(obj, str):
397
- if ' ' in obj:
398
- values_with_space.append(obj)
399
-
400
- extract_values(doc)
401
-
402
- # πŸ”— Let's create a list of links for these values
403
- search_urls = {
404
- "πŸš€πŸŒŒArXiv": lambda k: f"{LOCAL_APP_URL}/?q={quote(k)}",
405
- "πŸƒAnalyst": lambda k: f"{LOCAL_APP_URL}/?q={quote(k)}-{quote('PromptPrefix')}",
406
- "πŸ“šPyCoder": lambda k: f"{LOCAL_APP_URL}/?q={quote(k)}-{quote('PromptPrefix2')}",
407
- "πŸ”¬JSCoder": lambda k: f"{LOCAL_APP_URL}/?q={quote(k)}-{quote('PromptPrefix3')}",
408
- "🏠": lambda k: f"{LOCAL_APP_URL}/?q={quote(k)}",
409
- "πŸ“–": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
410
- "πŸ”": lambda k: f"https://www.google.com/search?q={quote(k)}",
411
- "▢️": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
412
- "πŸ”Ž": lambda k: f"https://www.bing.com/search?q={quote(k)}",
413
- "πŸŽ₯": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
414
- "🐦": lambda k: f"https://twitter.com/search?q={quote(k)}",
415
- }
416
-
417
- st.markdown("#### πŸ”— Links for Extracted Texts")
418
- for term in values_with_space:
419
- links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
420
- st.markdown(f"**{term}** <small>{links_md}</small>", unsafe_allow_html=True)
421
-
422
- # Show the document content as markdown
423
- content = json.dumps(doc, indent=2)
424
- st.markdown(f"```json\n{content}\n```")
425
-
426
- # Navigation buttons
427
- col_prev, col_next = st.columns([1, 1])
428
- with col_prev:
429
- if st.button("⬅️ Previous", key='prev_markdown'):
430
- if st.session_state.current_index > 0:
431
- st.session_state.current_index -= 1
432
- st.rerun()
433
- with col_next:
434
- if st.button("➑️ Next", key='next_markdown'):
435
- if st.session_state.current_index < total_docs - 1:
436
- st.session_state.current_index += 1
437
- st.rerun()
438
-
439
- elif selected_view == 'Show as Code Editor':
440
- # πŸ’» Show each record in a code editor with navigation
441
- total_docs = len(documents)
442
- doc = documents[st.session_state.current_index]
443
- st.markdown(f"#### Document ID: {doc.get('id', '')}")
444
- doc_str = st.text_area("Edit Document", value=json.dumps(doc, indent=2), height=300, key=f'code_editor_{st.session_state.current_index}')
445
- col_prev, col_next = st.columns([1, 1])
446
- with col_prev:
447
- if st.button("⬅️ Previous", key='prev_code'):
448
- if st.session_state.current_index > 0:
449
- st.session_state.current_index -= 1
450
- st.rerun()
451
- with col_next:
452
- if st.button("➑️ Next", key='next_code'):
453
- if st.session_state.current_index < total_docs - 1:
454
- st.session_state.current_index += 1
455
- st.rerun()
456
- if st.button("πŸ’Ύ Save Changes", key=f'save_button_{st.session_state.current_index}'):
457
- try:
458
- updated_doc = json.loads(doc_str)
459
- success, message = update_record(container, updated_doc)
460
- if success:
461
- st.success(f"Document {updated_doc['id']} saved successfully.")
462
- st.session_state.selected_document_id = updated_doc['id']
463
- st.rerun()
464
- else:
465
- st.error(message)
466
- except json.JSONDecodeError as e:
467
- st.error(f"Invalid JSON: {str(e)} 🚫")
468
- elif selected_view == 'Show as Edit and Save':
469
- # ✏️ Show as Edit and Save in columns
470
- st.markdown("#### Edit the document fields below:")
471
-
472
- # Create columns for each document
473
- num_cols = len(documents_to_display)
474
- cols = st.columns(num_cols)
475
-
476
- for idx, (col, doc) in enumerate(zip(cols, documents_to_display)):
477
- with col:
478
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
479
- editable_id = st.text_input("ID", value=doc.get('id', ''), key=f'edit_id_{idx}')
480
- # Remove 'id' from the document for editing other fields
481
- editable_doc = doc.copy()
482
- editable_doc.pop('id', None)
483
- doc_str = st.text_area("Document Content (in JSON format)", value=json.dumps(editable_doc, indent=2), height=300, key=f'doc_str_{idx}')
484
- if st.button("πŸ’Ύ Save Changes", key=f'save_button_{idx}'):
485
- try:
486
- updated_doc = json.loads(doc_str)
487
- updated_doc['id'] = editable_id # Include the possibly edited ID
488
- success, message = update_record(container, updated_doc)
489
- if success:
490
- st.success(f"Document {updated_doc['id']} saved successfully.")
491
- st.session_state.selected_document_id = updated_doc['id']
492
- st.rerun()
493
- else:
494
- st.error(message)
495
- except json.JSONDecodeError as e:
496
- st.error(f"Invalid JSON: {str(e)} 🚫")
497
- elif selected_view == 'Clone Document':
498
- # 🧬 Clone Document per record
499
- st.markdown("#### Clone a document:")
500
- for idx, doc in enumerate(documents_to_display):
501
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
502
- if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
503
- cloned_doc = doc.copy()
504
- # Generate a unique ID
505
- cloned_doc['id'] = generate_unique_id()
506
- st.session_state.cloned_doc = cloned_doc
507
- st.session_state.cloned_doc_str = json.dumps(cloned_doc, indent=2)
508
- st.session_state.clone_mode = True
509
- st.rerun()
510
- if st.session_state.get('clone_mode', False):
511
- st.markdown("#### Edit Cloned Document:")
512
- cloned_doc_str = st.text_area("Cloned Document Content (in JSON format)", value=st.session_state.cloned_doc_str, height=300)
513
- if st.button("πŸ’Ύ Save Cloned Document"):
514
- try:
515
- new_doc = json.loads(cloned_doc_str)
516
- success, message = insert_record(container, new_doc)
517
- if success:
518
- st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
519
- st.session_state.selected_document_id = new_doc['id']
520
- st.session_state.clone_mode = False
521
- st.session_state.cloned_doc = None
522
- st.session_state.cloned_doc_str = ''
523
- st.rerun()
524
- else:
525
- st.error(message)
526
- except json.JSONDecodeError as e:
527
- st.error(f"Invalid JSON: {str(e)} 🚫")
528
- elif selected_view == 'New Record':
529
- # πŸ†• New Record
530
- st.markdown("#### Create a new document:")
531
- new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
532
- new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
533
- if st.button("βž• Create New Document"):
534
- try:
535
- new_doc = json.loads(new_doc_str)
536
- new_doc['id'] = new_id # Use the provided ID
537
- success, message = insert_record(container, new_doc)
538
- if success:
539
- st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
540
- st.session_state.selected_document_id = new_doc['id']
541
- # Switch to 'Show as Edit and Save' mode
542
- st.rerun()
543
- else:
544
- st.error(message)
545
- except json.JSONDecodeError as e:
546
- st.error(f"Invalid JSON: {str(e)} 🚫")
547
- else:
548
- st.sidebar.info("No documents found in this container. πŸ“­")
549
-
550
- # πŸŽ‰ Main content area
551
- st.subheader(f"πŸ“Š Container: {st.session_state.selected_container}")
552
- if st.session_state.selected_container:
553
- if documents_to_display:
554
- df = pd.DataFrame(documents_to_display)
555
- st.dataframe(df)
556
- else:
557
- st.info("No documents to display. 🧐")
558
-
559
- # πŸ™ GitHub section
560
- st.subheader("πŸ™ GitHub Operations")
561
- github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
562
- source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
563
- new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
564
-
565
- col1, col2 = st.columns(2)
566
- with col1:
567
- if st.button("πŸ“₯ Clone Repository"):
568
- if github_token and source_repo:
569
- try:
570
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
571
- download_github_repo(source_repo, local_path)
572
- zip_filename = f"{new_repo_name}.zip"
573
- create_zip_file(local_path, zip_filename[:-4])
574
- st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
575
- st.success("Repository cloned successfully! πŸŽ‰")
576
- except Exception as e:
577
- st.error(f"An error occurred: {str(e)} 😒")
578
- finally:
579
- if os.path.exists(local_path):
580
- shutil.rmtree(local_path)
581
- if os.path.exists(zip_filename):
582
- os.remove(zip_filename)
583
- else:
584
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πŸ”‘β“")
585
-
586
- with col2:
587
- if st.button("πŸ“€ Push to New Repository"):
588
- if github_token and source_repo:
589
- try:
590
- g = Github(github_token)
591
- new_repo = create_repo(g, new_repo_name)
592
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
593
- download_github_repo(source_repo, local_path)
594
- push_to_github(local_path, new_repo, github_token)
595
- st.success(f"Repository pushed successfully to {new_repo.html_url} πŸš€")
596
- except Exception as e:
597
- st.error(f"An error occurred: {str(e)} 😒")
598
- finally:
599
- if os.path.exists(local_path):
600
- shutil.rmtree(local_path)
601
- else:
602
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πŸ”‘β“")
603
-
604
- except exceptions.CosmosHttpResponseError as e:
605
- st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} 🚨")
606
- except Exception as e:
607
- st.error(f"An unexpected error occurred: {str(e)} 😱")
608
-
609
- # πŸšͺ Logout button
610
- if st.session_state.logged_in and st.sidebar.button("πŸšͺ Logout"):
611
- st.session_state.logged_in = False
612
- st.session_state.selected_records.clear()
613
- st.session_state.client = None
614
- st.session_state.selected_database = None
615
- st.session_state.selected_container = None
616
- st.session_state.selected_document_id = None
617
- st.session_state.current_index = 0
618
- st.rerun()
619
-
620
- if __name__ == "__main__":
621
- main()