Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
from azure.cosmos import CosmosClient,
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
import traceback
|
@@ -14,344 +14,112 @@ import json
|
|
14 |
|
15 |
st.set_page_config(layout="wide")
|
16 |
|
17 |
-
#
|
18 |
-
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
19 |
-
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
20 |
-
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
21 |
-
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
22 |
-
Key = os.environ.get("Key")
|
23 |
|
24 |
-
#
|
25 |
-
def
|
26 |
-
|
27 |
-
shutil.rmtree(local_path)
|
28 |
-
Repo.clone_from(url, local_path)
|
29 |
|
30 |
-
def
|
31 |
-
|
32 |
|
33 |
-
def
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if 'origin' in [remote.name for remote in local_repo.remotes]:
|
42 |
-
origin = local_repo.remote('origin')
|
43 |
-
origin.set_url(repo_url)
|
44 |
-
else:
|
45 |
-
origin = local_repo.create_remote('origin', repo_url)
|
46 |
-
|
47 |
-
if not local_repo.heads:
|
48 |
-
local_repo.git.checkout('-b', 'main')
|
49 |
-
current_branch = 'main'
|
50 |
-
else:
|
51 |
-
current_branch = local_repo.active_branch.name
|
52 |
-
|
53 |
-
local_repo.git.add(A=True)
|
54 |
-
|
55 |
-
if local_repo.is_dirty():
|
56 |
-
local_repo.git.commit('-m', 'Initial commit')
|
57 |
-
|
58 |
-
origin.push(refspec=f'{current_branch}:{current_branch}')
|
59 |
-
|
60 |
-
def get_base64_download_link(file_path, file_name):
|
61 |
-
with open(file_path, "rb") as file:
|
62 |
-
contents = file.read()
|
63 |
-
base64_encoded = base64.b64encode(contents).decode()
|
64 |
-
return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">Download {file_name}</a>'
|
65 |
-
|
66 |
-
# Cosmos DB functions
|
67 |
-
def insert_record(record):
|
68 |
-
try:
|
69 |
-
response = container.create_item(body=record)
|
70 |
-
return True, response
|
71 |
-
except exceptions.CosmosHttpResponseError as e:
|
72 |
-
return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
73 |
-
except Exception as e:
|
74 |
-
return False, f"An unexpected error occurred: {str(e)}"
|
75 |
-
|
76 |
-
def call_stored_procedure(record):
|
77 |
-
try:
|
78 |
-
response = container.scripts.execute_stored_procedure(
|
79 |
-
sproc="processPrompt",
|
80 |
-
params=[record],
|
81 |
-
partition_key=record['id']
|
82 |
-
)
|
83 |
-
return True, response
|
84 |
-
except exceptions.CosmosHttpResponseError as e:
|
85 |
-
error_message = f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
86 |
-
return False, error_message
|
87 |
-
except Exception as e:
|
88 |
-
error_message = f"An unexpected error occurred: {str(e)}"
|
89 |
-
return False, error_message
|
90 |
-
|
91 |
-
def fetch_all_records():
|
92 |
-
try:
|
93 |
-
query = "SELECT * FROM c"
|
94 |
-
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
95 |
-
return pd.DataFrame(items)
|
96 |
-
except exceptions.CosmosHttpResponseError as e:
|
97 |
-
st.error(f"HTTP error occurred while fetching records: {str(e)}. Status code: {e.status_code}")
|
98 |
-
return pd.DataFrame()
|
99 |
-
except Exception as e:
|
100 |
-
st.error(f"An unexpected error occurred while fetching records: {str(e)}")
|
101 |
-
return pd.DataFrame()
|
102 |
-
|
103 |
-
def update_record(updated_record):
|
104 |
-
try:
|
105 |
-
container.upsert_item(body=updated_record)
|
106 |
-
return True, f"Record with id {updated_record['id']} successfully updated."
|
107 |
-
except exceptions.CosmosHttpResponseError as e:
|
108 |
-
return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
109 |
-
except Exception as e:
|
110 |
-
return False, f"An unexpected error occurred: {traceback.format_exc()}"
|
111 |
-
|
112 |
-
def delete_record(name, id):
|
113 |
-
try:
|
114 |
-
container.delete_item(item=id, partition_key=id)
|
115 |
-
return True, f"Successfully deleted record with name: {name} and id: {id}"
|
116 |
-
except exceptions.CosmosResourceNotFoundError:
|
117 |
-
return False, f"Record with id {id} not found. It may have been already deleted."
|
118 |
-
except exceptions.CosmosHttpResponseError as e:
|
119 |
-
return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
120 |
-
except Exception as e:
|
121 |
-
return False, f"An unexpected error occurred: {traceback.format_exc()}"
|
122 |
-
|
123 |
-
# New function to archive all databases and containers
|
124 |
-
def archive_all_data(client):
|
125 |
-
try:
|
126 |
-
base_dir = "./cosmos_archive"
|
127 |
-
if os.path.exists(base_dir):
|
128 |
-
shutil.rmtree(base_dir)
|
129 |
-
os.makedirs(base_dir)
|
130 |
-
|
131 |
-
for database in client.list_databases():
|
132 |
-
db_name = database['id']
|
133 |
-
db_dir = os.path.join(base_dir, db_name)
|
134 |
-
os.makedirs(db_dir)
|
135 |
-
|
136 |
-
db_client = client.get_database_client(db_name)
|
137 |
-
for container in db_client.list_containers():
|
138 |
-
container_name = container['id']
|
139 |
-
container_dir = os.path.join(db_dir, container_name)
|
140 |
-
os.makedirs(container_dir)
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
st.
|
157 |
-
|
158 |
-
|
159 |
-
if
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
# Login section
|
165 |
-
if not st.session_state.logged_in:
|
166 |
-
st.subheader("๐ Login")
|
167 |
-
input_key = Key
|
168 |
-
if st.button("๐ Login"):
|
169 |
-
if input_key:
|
170 |
-
st.session_state.primary_key = input_key
|
171 |
-
st.session_state.logged_in = True
|
172 |
-
st.rerun()
|
173 |
-
else:
|
174 |
-
st.error("Invalid key. Please check your environment variables.")
|
175 |
-
else:
|
176 |
-
# Initialize Cosmos DB client
|
177 |
-
try:
|
178 |
-
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
179 |
-
database = client.get_database_client(DATABASE_NAME)
|
180 |
-
container = database.get_container_client(CONTAINER_NAME)
|
181 |
-
except exceptions.CosmosHttpResponseError as e:
|
182 |
-
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}. Status code: {e.status_code}")
|
183 |
-
st.stop()
|
184 |
-
except Exception as e:
|
185 |
-
st.error(f"An unexpected error occurred while connecting to Cosmos DB: {str(e)}")
|
186 |
-
st.stop()
|
187 |
-
|
188 |
-
# GitHub section
|
189 |
-
st.subheader("๐ GitHub Operations")
|
190 |
-
github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
|
191 |
-
source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
|
192 |
-
new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
|
193 |
-
|
194 |
-
col1, col2 = st.columns(2)
|
195 |
-
with col1:
|
196 |
-
if st.button("๐ฅ Clone Repository"):
|
197 |
-
if github_token and source_repo:
|
198 |
-
try:
|
199 |
-
local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
200 |
-
download_github_repo(source_repo, local_path)
|
201 |
-
zip_filename = f"{new_repo_name}.zip"
|
202 |
-
create_zip_file(local_path, zip_filename[:-4])
|
203 |
-
st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
|
204 |
-
st.success("Repository cloned successfully!")
|
205 |
-
except Exception as e:
|
206 |
-
st.error(f"An error occurred: {str(e)}")
|
207 |
-
finally:
|
208 |
-
if os.path.exists(local_path):
|
209 |
-
shutil.rmtree(local_path)
|
210 |
-
if os.path.exists(zip_filename):
|
211 |
-
os.remove(zip_filename)
|
212 |
-
else:
|
213 |
-
st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided.")
|
214 |
-
|
215 |
-
with col2:
|
216 |
-
if st.button("๐ค Push to New Repository"):
|
217 |
-
if github_token and source_repo:
|
218 |
-
try:
|
219 |
-
g = Github(github_token)
|
220 |
-
new_repo = create_repo(g, new_repo_name)
|
221 |
-
local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
222 |
-
download_github_repo(source_repo, local_path)
|
223 |
-
push_to_github(local_path, new_repo, github_token)
|
224 |
-
st.success(f"Repository pushed successfully to {new_repo.html_url}")
|
225 |
-
except Exception as e:
|
226 |
-
st.error(f"An error occurred: {str(e)}")
|
227 |
-
finally:
|
228 |
-
if os.path.exists(local_path):
|
229 |
-
shutil.rmtree(local_path)
|
230 |
else:
|
231 |
-
st.error("
|
232 |
-
|
233 |
-
# Cosmos DB Operations
|
234 |
-
st.subheader("โ๏ธ Cosmos DB Operations")
|
235 |
-
|
236 |
-
# Archive all data
|
237 |
-
if st.button("๐ฆ Archive All Cosmos DB Data"):
|
238 |
-
download_link = archive_all_data(client)
|
239 |
-
st.markdown(download_link, unsafe_allow_html=True)
|
240 |
-
|
241 |
-
# Fetch and display all records
|
242 |
-
st.subheader("๐ All Records")
|
243 |
-
df = fetch_all_records()
|
244 |
-
|
245 |
-
if df.empty:
|
246 |
-
st.write("No records found in the database.")
|
247 |
else:
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
else:
|
268 |
-
st.
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
updated_record = {
|
283 |
-
"id": selected_record['id'],
|
284 |
-
"name": updated_name,
|
285 |
-
"document": updated_document,
|
286 |
-
"evaluationText": updated_evaluation_text,
|
287 |
-
"evaluationScore": updated_evaluation_score
|
288 |
-
}
|
289 |
-
|
290 |
-
success, message = update_record(updated_record)
|
291 |
-
if success:
|
292 |
-
st.success(message)
|
293 |
-
st.session_state.selected_record = updated_record
|
294 |
-
else:
|
295 |
-
st.error(message)
|
296 |
-
|
297 |
-
# Input fields for new record
|
298 |
-
st.subheader("๐ Enter New Record Details")
|
299 |
-
new_id = st.text_input("ID")
|
300 |
-
new_name = st.text_input("Name")
|
301 |
-
new_document = st.text_area("Document")
|
302 |
-
new_evaluation_text = st.text_area("Evaluation Text")
|
303 |
-
new_evaluation_score = st.text_input("Evaluation Score")
|
304 |
-
|
305 |
-
col1, col2 = st.columns(2)
|
306 |
-
|
307 |
-
with col1:
|
308 |
-
if st.button("๐พ Insert Record"):
|
309 |
-
record = {
|
310 |
-
"id": new_id,
|
311 |
-
"name": new_name,
|
312 |
-
"document": new_document,
|
313 |
-
"evaluationText": new_evaluation_text,
|
314 |
-
"evaluationScore": new_evaluation_score
|
315 |
-
}
|
316 |
-
|
317 |
-
success, response = insert_record(record)
|
318 |
-
if success:
|
319 |
-
st.success("โ
Record inserted successfully!")
|
320 |
-
st.json(response)
|
321 |
-
else:
|
322 |
-
st.error(f"โ Failed to insert record: {response}")
|
323 |
-
st.rerun()
|
324 |
-
|
325 |
-
with col2:
|
326 |
-
if st.button("๐ง Call Procedure"):
|
327 |
-
record = {
|
328 |
-
"id": new_id,
|
329 |
-
"name": new_name,
|
330 |
-
"document": new_document,
|
331 |
-
"evaluationText": new_evaluation_text,
|
332 |
-
"evaluationScore": new_evaluation_score
|
333 |
-
}
|
334 |
-
|
335 |
-
success, response = call_stored_procedure(record)
|
336 |
-
if success:
|
337 |
-
st.success("โ
Stored procedure executed successfully!")
|
338 |
-
st.json(response)
|
339 |
-
else:
|
340 |
-
st.error(f"โ Failed to execute stored procedure: {response}")
|
341 |
|
342 |
# Logout button
|
343 |
-
if st.button("๐ช Logout"):
|
344 |
st.session_state.logged_in = False
|
345 |
st.session_state.selected_records.clear()
|
346 |
-
st.session_state.
|
|
|
|
|
347 |
st.rerun()
|
348 |
|
349 |
-
|
350 |
-
|
351 |
-
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
352 |
-
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
353 |
-
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
354 |
-
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
355 |
-
|
356 |
-
#if __name__ == "__main__":
|
357 |
-
# st.write("Application is running")
|
|
|
1 |
import streamlit as st
|
2 |
+
from azure.cosmos import CosmosClient, exceptions
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
import traceback
|
|
|
14 |
|
15 |
st.set_page_config(layout="wide")
|
16 |
|
17 |
+
# ... (keep all the existing imports and configurations)
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# New functions for dynamic sidebar
|
20 |
+
def get_databases(client):
|
21 |
+
return [db['id'] for db in client.list_databases()]
|
|
|
|
|
22 |
|
23 |
+
def get_containers(database):
|
24 |
+
return [container['id'] for container in database.list_containers()]
|
25 |
|
26 |
+
def get_documents(container, limit=1000):
|
27 |
+
query = "SELECT * FROM c"
|
28 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
|
29 |
+
return items
|
30 |
|
31 |
+
# Modify the main app
|
32 |
+
def main():
|
33 |
+
st.title("๐ Cosmos DB and GitHub Integration")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Initialize session state
|
36 |
+
if 'logged_in' not in st.session_state:
|
37 |
+
st.session_state.logged_in = False
|
38 |
+
if 'selected_records' not in st.session_state:
|
39 |
+
st.session_state.selected_records = []
|
40 |
+
if 'client' not in st.session_state:
|
41 |
+
st.session_state.client = None
|
42 |
+
if 'selected_database' not in st.session_state:
|
43 |
+
st.session_state.selected_database = None
|
44 |
+
if 'selected_container' not in st.session_state:
|
45 |
+
st.session_state.selected_container = None
|
46 |
+
|
47 |
+
# Login section
|
48 |
+
if not st.session_state.logged_in:
|
49 |
+
st.subheader("๐ Login")
|
50 |
+
input_key = st.text_input("Enter your Cosmos DB key", type="password")
|
51 |
+
if st.button("๐ Login"):
|
52 |
+
if input_key:
|
53 |
+
st.session_state.primary_key = input_key
|
54 |
+
st.session_state.logged_in = True
|
55 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
else:
|
57 |
+
st.error("Invalid key. Please check your input.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
else:
|
59 |
+
# Initialize Cosmos DB client
|
60 |
+
try:
|
61 |
+
if st.session_state.client is None:
|
62 |
+
st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
63 |
+
|
64 |
+
# Sidebar for database, container, and document selection
|
65 |
+
st.sidebar.title("๐๏ธ Cosmos DB Navigator")
|
66 |
+
|
67 |
+
databases = get_databases(st.session_state.client)
|
68 |
+
selected_db = st.sidebar.selectbox("๐๏ธ Select Database", databases)
|
69 |
+
|
70 |
+
if selected_db != st.session_state.selected_database:
|
71 |
+
st.session_state.selected_database = selected_db
|
72 |
+
st.session_state.selected_container = None
|
73 |
+
st.rerun()
|
74 |
+
|
75 |
+
if st.session_state.selected_database:
|
76 |
+
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
77 |
+
containers = get_containers(database)
|
78 |
+
selected_container = st.sidebar.selectbox("๐ Select Container", containers)
|
79 |
+
|
80 |
+
if selected_container != st.session_state.selected_container:
|
81 |
+
st.session_state.selected_container = selected_container
|
82 |
+
st.rerun()
|
83 |
+
|
84 |
+
if st.session_state.selected_container:
|
85 |
+
container = database.get_container_client(st.session_state.selected_container)
|
86 |
+
|
87 |
+
limit_to_1000 = st.sidebar.checkbox("๐ข Limit to top 1000 documents", value=True)
|
88 |
+
documents = get_documents(container, limit=1000 if limit_to_1000 else None)
|
89 |
+
|
90 |
+
if documents:
|
91 |
+
document_ids = [doc.get('id', 'Unknown') for doc in documents]
|
92 |
+
selected_document = st.sidebar.selectbox("๐ Select Document", document_ids)
|
93 |
+
|
94 |
+
if selected_document:
|
95 |
+
st.subheader(f"๐ Document Details: {selected_document}")
|
96 |
+
selected_doc = next((doc for doc in documents if doc.get('id') == selected_document), None)
|
97 |
+
if selected_doc:
|
98 |
+
st.json(selected_doc)
|
99 |
else:
|
100 |
+
st.sidebar.info("No documents found in this container.")
|
101 |
+
|
102 |
+
# Main content area
|
103 |
+
st.subheader(f"๐ Container: {st.session_state.selected_container}")
|
104 |
+
if st.session_state.selected_container:
|
105 |
+
df = pd.DataFrame(documents)
|
106 |
+
st.dataframe(df)
|
107 |
+
|
108 |
+
# ... (keep the rest of your existing code for GitHub operations, etc.)
|
109 |
+
|
110 |
+
except exceptions.CosmosHttpResponseError as e:
|
111 |
+
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}. Status code: {e.status_code}")
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# Logout button
|
116 |
+
if st.session_state.logged_in and st.sidebar.button("๐ช Logout"):
|
117 |
st.session_state.logged_in = False
|
118 |
st.session_state.selected_records.clear()
|
119 |
+
st.session_state.client = None
|
120 |
+
st.session_state.selected_database = None
|
121 |
+
st.session_state.selected_container = None
|
122 |
st.rerun()
|
123 |
|
124 |
+
if __name__ == "__main__":
|
125 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|