Spaces:
Running
Running
Delete backup3rocks.app.py
Browse files- backup3rocks.app.py +0 -116
backup3rocks.app.py
DELETED
@@ -1,116 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from azure.cosmos import CosmosClient
|
3 |
-
import os
|
4 |
-
|
5 |
-
# Cosmos DB configuration
|
6 |
-
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
7 |
-
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
8 |
-
# You'll need to set these environment variables or use Azure Key Vault
|
9 |
-
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
10 |
-
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
11 |
-
Key = os.environ.get("Key")
|
12 |
-
|
13 |
-
def insert_record(record):
|
14 |
-
try:
|
15 |
-
response = container.create_item(body=record)
|
16 |
-
return True, response
|
17 |
-
except Exception as e:
|
18 |
-
return False, str(e)
|
19 |
-
|
20 |
-
def call_stored_procedure(record):
|
21 |
-
try:
|
22 |
-
response = container.scripts.execute_stored_procedure(
|
23 |
-
sproc="processPrompt",
|
24 |
-
params=[record],
|
25 |
-
partition_key=record['id']
|
26 |
-
)
|
27 |
-
return True, response
|
28 |
-
except Exception as e:
|
29 |
-
error_message = f"Error type: {type(e).__name__}\nError message: {str(e)}"
|
30 |
-
if hasattr(e, 'sub_status'):
|
31 |
-
error_message += f"\nSub-status: {e.sub_status}"
|
32 |
-
if hasattr(e, 'response'):
|
33 |
-
error_message += f"\nResponse: {e.response}"
|
34 |
-
return False, error_message
|
35 |
-
|
36 |
-
|
37 |
-
# Streamlit app
|
38 |
-
st.title("๐ Cosmos DB Record Insertion and Procedure Execution")
|
39 |
-
|
40 |
-
# Login section
|
41 |
-
if 'logged_in' not in st.session_state:
|
42 |
-
st.session_state.logged_in = False
|
43 |
-
|
44 |
-
if not st.session_state.logged_in:
|
45 |
-
st.subheader("๐ Login")
|
46 |
-
input_key = Key
|
47 |
-
if st.button("๐ Login"):
|
48 |
-
if input_key:
|
49 |
-
st.session_state.primary_key = input_key
|
50 |
-
st.session_state.logged_in = True
|
51 |
-
st.rerun()
|
52 |
-
else:
|
53 |
-
st.error("Please enter a valid key")
|
54 |
-
else:
|
55 |
-
# Initialize Cosmos DB client
|
56 |
-
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
57 |
-
database = client.get_database_client(DATABASE_NAME)
|
58 |
-
container = database.get_container_client(CONTAINER_NAME)
|
59 |
-
|
60 |
-
# Input fields
|
61 |
-
st.subheader("๐ Enter Record Details")
|
62 |
-
id = st.text_input("ID")
|
63 |
-
name = st.text_input("Name")
|
64 |
-
document = st.text_area("Document")
|
65 |
-
evaluation_text = st.text_area("Evaluation Text")
|
66 |
-
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
67 |
-
|
68 |
-
col1, col2 = st.columns(2)
|
69 |
-
|
70 |
-
# Insert Record button
|
71 |
-
with col1:
|
72 |
-
if st.button("๐พ Insert Record"):
|
73 |
-
record = {
|
74 |
-
"id": id,
|
75 |
-
"name": name,
|
76 |
-
"document": document,
|
77 |
-
"evaluationText": evaluation_text,
|
78 |
-
"evaluationScore": evaluation_score
|
79 |
-
}
|
80 |
-
|
81 |
-
success, response = insert_record(record)
|
82 |
-
if success:
|
83 |
-
st.success("โ
Record inserted successfully!")
|
84 |
-
st.json(response)
|
85 |
-
else:
|
86 |
-
st.error(f"โ Failed to insert record: {response}")
|
87 |
-
|
88 |
-
# Call Procedure button
|
89 |
-
with col2:
|
90 |
-
if st.button("๐ง Call Procedure"):
|
91 |
-
record = {
|
92 |
-
"id": id,
|
93 |
-
"name": name,
|
94 |
-
"document": document,
|
95 |
-
"evaluationText": evaluation_text,
|
96 |
-
"evaluationScore": evaluation_score
|
97 |
-
}
|
98 |
-
|
99 |
-
success, response = call_stored_procedure(record)
|
100 |
-
if success:
|
101 |
-
st.success("โ
Stored procedure executed successfully!")
|
102 |
-
st.json(response)
|
103 |
-
else:
|
104 |
-
st.error(f"โ Failed to execute stored procedure: {response}")
|
105 |
-
|
106 |
-
# Logout button
|
107 |
-
if st.button("๐ช Logout"):
|
108 |
-
st.session_state.logged_in = False
|
109 |
-
st.rerun()
|
110 |
-
|
111 |
-
# Display connection info
|
112 |
-
st.sidebar.subheader("๐ Connection Information")
|
113 |
-
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
114 |
-
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
115 |
-
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
116 |
-
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|