awacke1 commited on
Commit
e9445df
·
verified ·
1 Parent(s): 325f079

Delete backup4.notdeleteing.app.py

Browse files
Files changed (1) hide show
  1. backup4.notdeleteing.app.py +0 -153
backup4.notdeleteing.app.py DELETED
@@ -1,153 +0,0 @@
1
- import streamlit as st
2
- from azure.cosmos import CosmosClient
3
- import os
4
- import pandas as pd
5
-
6
- # Cosmos DB configuration
7
- ENDPOINT = "https://acae-afd.documents.azure.com:443/"
8
- SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
9
- # You'll need to set these environment variables or use Azure Key Vault
10
- DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
11
- CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
12
- Key = os.environ.get("Key")
13
-
14
- def insert_record(record):
15
- try:
16
- response = container.create_item(body=record)
17
- return True, response
18
- except Exception as e:
19
- return False, str(e)
20
-
21
- def call_stored_procedure(record):
22
- try:
23
- response = container.scripts.execute_stored_procedure(
24
- sproc="processPrompt",
25
- params=[record],
26
- partition_key=record['id']
27
- )
28
- return True, response
29
- except Exception as e:
30
- error_message = f"Error type: {type(e).__name__}\nError message: {str(e)}"
31
- if hasattr(e, 'sub_status'):
32
- error_message += f"\nSub-status: {e.sub_status}"
33
- if hasattr(e, 'response'):
34
- error_message += f"\nResponse: {e.response}"
35
- return False, error_message
36
-
37
- def fetch_all_records():
38
- query = "SELECT * FROM c"
39
- items = list(container.query_items(query=query, enable_cross_partition_query=True))
40
- return pd.DataFrame(items)
41
-
42
- def delete_record(id):
43
- try:
44
- container.delete_item(id, partition_key=id)
45
- return True
46
- except Exception as e:
47
- return False
48
-
49
- # Streamlit app
50
- st.title("🌟 Cosmos DB Record Management")
51
-
52
- # Login section
53
- if 'logged_in' not in st.session_state:
54
- st.session_state.logged_in = False
55
-
56
- if not st.session_state.logged_in:
57
- st.subheader("🔐 Login")
58
- #input_key = st.text_input("Enter your key", type="password")
59
- input_key = Key
60
- if st.button("🚀 Login"):
61
- if input_key:
62
- st.session_state.primary_key = input_key
63
- st.session_state.logged_in = True
64
- st.rerun()
65
- else:
66
- st.error("Please enter a valid key")
67
- else:
68
- # Initialize Cosmos DB client
69
- client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
70
- database = client.get_database_client(DATABASE_NAME)
71
- container = database.get_container_client(CONTAINER_NAME)
72
-
73
- # Fetch and display all records
74
- st.subheader("📊 All Records")
75
- df = fetch_all_records()
76
-
77
- # Use Streamlit's data editor
78
- edited_df = st.data_editor(df, num_rows="dynamic", key="data_editor")
79
-
80
- # Add delete and download buttons
81
- col1, col2 = st.columns(2)
82
- with col1:
83
- if st.button("🗑️ Delete Selected"):
84
- for index, row in edited_df.iterrows():
85
- if row.get('_selected', False):
86
- if delete_record(row['id']):
87
- st.success(f"Deleted record with ID: {row['id']}")
88
- else:
89
- st.error(f"Failed to delete record with ID: {row['id']}")
90
- st.rerun()
91
-
92
- with col2:
93
- if st.download_button("📥 Download Data", edited_df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
94
- st.success("Data downloaded successfully!")
95
-
96
- # Input fields for new record
97
- st.subheader("📝 Enter New Record Details")
98
- new_id = st.text_input("ID")
99
- new_name = st.text_input("Name")
100
- new_document = st.text_area("Document")
101
- new_evaluation_text = st.text_area("Evaluation Text")
102
- new_evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
103
-
104
- col1, col2 = st.columns(2)
105
-
106
- # Insert Record button
107
- with col1:
108
- if st.button("💾 Insert Record"):
109
- record = {
110
- "id": new_id,
111
- "name": new_name,
112
- "document": new_document,
113
- "evaluationText": new_evaluation_text,
114
- "evaluationScore": new_evaluation_score
115
- }
116
-
117
- success, response = insert_record(record)
118
- if success:
119
- st.success("✅ Record inserted successfully!")
120
- st.json(response)
121
- else:
122
- st.error(f"❌ Failed to insert record: {response}")
123
- st.rerun()
124
-
125
- # Call Procedure button
126
- with col2:
127
- if st.button("🔧 Call Procedure"):
128
- record = {
129
- "id": new_id,
130
- "name": new_name,
131
- "document": new_document,
132
- "evaluationText": new_evaluation_text,
133
- "evaluationScore": new_evaluation_score
134
- }
135
-
136
- success, response = call_stored_procedure(record)
137
- if success:
138
- st.success("✅ Stored procedure executed successfully!")
139
- st.json(response)
140
- else:
141
- st.error(f"❌ Failed to execute stored procedure: {response}")
142
-
143
- # Logout button
144
- if st.button("🚪 Logout"):
145
- st.session_state.logged_in = False
146
- st.rerun()
147
-
148
- # Display connection info
149
- st.sidebar.subheader("🔗 Connection Information")
150
- st.sidebar.text(f"Endpoint: {ENDPOINT}")
151
- st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
152
- st.sidebar.text(f"Database: {DATABASE_NAME}")
153
- st.sidebar.text(f"Container: {CONTAINER_NAME}")