Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -63,6 +63,20 @@ def get_base64_download_link(file_path, file_name):
|
|
| 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:
|
|
@@ -155,203 +169,101 @@ def archive_all_data(client):
|
|
| 155 |
# Streamlit app
|
| 156 |
st.title("π Cosmos DB and GitHub Integration")
|
| 157 |
|
| 158 |
-
# Initialize session state
|
| 159 |
-
if 'logged_in' not in st.session_state:
|
| 160 |
-
st.session_state.logged_in = False
|
| 161 |
-
if 'selected_records' not in st.session_state:
|
| 162 |
-
st.session_state.selected_records = []
|
| 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("Please ensure GitHub token is set in environment variables and source repository URL is provided.")
|
| 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 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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")
|
|
|
|
| 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 |
+
|
| 67 |
+
# New functions for dynamic sidebar
|
| 68 |
+
def get_databases(client):
|
| 69 |
+
return [db['id'] for db in client.list_databases()]
|
| 70 |
+
|
| 71 |
+
def get_containers(database):
|
| 72 |
+
return [container['id'] for container in database.list_containers()]
|
| 73 |
+
|
| 74 |
+
def get_documents(container, limit=1000):
|
| 75 |
+
query = "SELECT * FROM c"
|
| 76 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
|
| 77 |
+
return items
|
| 78 |
+
|
| 79 |
+
|
| 80 |
# Cosmos DB functions
|
| 81 |
def insert_record(record):
|
| 82 |
try:
|
|
|
|
| 169 |
# Streamlit app
|
| 170 |
st.title("π Cosmos DB and GitHub Integration")
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
# Modify the main app
|
| 175 |
+
def main():
|
| 176 |
+
st.title("π Cosmos DB and GitHub Integration")
|
| 177 |
|
| 178 |
+
# Initialize session state
|
| 179 |
+
if 'logged_in' not in st.session_state:
|
| 180 |
+
st.session_state.logged_in = False
|
| 181 |
+
if 'selected_records' not in st.session_state:
|
| 182 |
+
st.session_state.selected_records = []
|
| 183 |
+
if 'client' not in st.session_state:
|
| 184 |
+
st.session_state.client = None
|
| 185 |
+
if 'selected_database' not in st.session_state:
|
| 186 |
+
st.session_state.selected_database = None
|
| 187 |
+
if 'selected_container' not in st.session_state:
|
| 188 |
+
st.session_state.selected_container = None
|
| 189 |
+
|
| 190 |
+
# Login section
|
| 191 |
+
if not st.session_state.logged_in:
|
| 192 |
+
st.subheader("π Login")
|
| 193 |
+
input_key = st.text_input("Enter your Cosmos DB key", type="password")
|
| 194 |
+
if st.button("π Login"):
|
| 195 |
+
if input_key:
|
| 196 |
+
st.session_state.primary_key = input_key
|
| 197 |
+
st.session_state.logged_in = True
|
| 198 |
+
st.rerun()
|
| 199 |
+
else:
|
| 200 |
+
st.error("Invalid key. Please check your input.")
|
| 201 |
else:
|
| 202 |
+
# Initialize Cosmos DB client
|
| 203 |
+
try:
|
| 204 |
+
if st.session_state.client is None:
|
| 205 |
+
st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
| 206 |
+
|
| 207 |
+
# Sidebar for database, container, and document selection
|
| 208 |
+
st.sidebar.title("ποΈ Cosmos DB Navigator")
|
| 209 |
+
|
| 210 |
+
databases = get_databases(st.session_state.client)
|
| 211 |
+
selected_db = st.sidebar.selectbox("ποΈ Select Database", databases)
|
| 212 |
+
|
| 213 |
+
if selected_db != st.session_state.selected_database:
|
| 214 |
+
st.session_state.selected_database = selected_db
|
| 215 |
+
st.session_state.selected_container = None
|
| 216 |
+
st.rerun()
|
| 217 |
+
|
| 218 |
+
if st.session_state.selected_database:
|
| 219 |
+
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
| 220 |
+
containers = get_containers(database)
|
| 221 |
+
selected_container = st.sidebar.selectbox("π Select Container", containers)
|
| 222 |
+
|
| 223 |
+
if selected_container != st.session_state.selected_container:
|
| 224 |
+
st.session_state.selected_container = selected_container
|
| 225 |
+
st.rerun()
|
| 226 |
+
|
| 227 |
+
if st.session_state.selected_container:
|
| 228 |
+
container = database.get_container_client(st.session_state.selected_container)
|
| 229 |
+
|
| 230 |
+
limit_to_1000 = st.sidebar.checkbox("π’ Limit to top 1000 documents", value=True)
|
| 231 |
+
documents = get_documents(container, limit=1000 if limit_to_1000 else None)
|
| 232 |
+
|
| 233 |
+
if documents:
|
| 234 |
+
document_ids = [doc.get('id', 'Unknown') for doc in documents]
|
| 235 |
+
selected_document = st.sidebar.selectbox("π Select Document", document_ids)
|
| 236 |
+
|
| 237 |
+
if selected_document:
|
| 238 |
+
st.subheader(f"π Document Details: {selected_document}")
|
| 239 |
+
selected_doc = next((doc for doc in documents if doc.get('id') == selected_document), None)
|
| 240 |
+
if selected_doc:
|
| 241 |
+
st.json(selected_doc)
|
| 242 |
else:
|
| 243 |
+
st.sidebar.info("No documents found in this container.")
|
| 244 |
+
|
| 245 |
+
# Main content area
|
| 246 |
+
st.subheader(f"π Container: {st.session_state.selected_container}")
|
| 247 |
+
if st.session_state.selected_container:
|
| 248 |
+
df = pd.DataFrame(documents)
|
| 249 |
+
st.dataframe(df)
|
| 250 |
+
|
| 251 |
+
# ... (keep the rest of your existing code for GitHub operations, etc.)
|
| 252 |
+
|
| 253 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 254 |
+
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}. Status code: {e.status_code}")
|
| 255 |
+
except Exception as e:
|
| 256 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
# Logout button
|
| 259 |
+
if st.session_state.logged_in and st.sidebar.button("πͺ Logout"):
|
| 260 |
st.session_state.logged_in = False
|
| 261 |
st.session_state.selected_records.clear()
|
| 262 |
+
st.session_state.client = None
|
| 263 |
+
st.session_state.selected_database = None
|
| 264 |
+
st.session_state.selected_container = None
|
| 265 |
st.rerun()
|
| 266 |
|
| 267 |
+
if __name__ == "__main__":
|
| 268 |
+
main()
|
| 269 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|