Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
return False, str(e)
|
30 |
+
|
31 |
+
# Streamlit app
|
32 |
+
st.title("π Cosmos DB Record Insertion and Procedure Execution")
|
33 |
+
|
34 |
+
# Login section
|
35 |
+
if 'logged_in' not in st.session_state:
|
36 |
+
st.session_state.logged_in = False
|
37 |
+
|
38 |
+
if not st.session_state.logged_in:
|
39 |
+
st.subheader("π Login")
|
40 |
+
input_key = Key
|
41 |
+
if st.button("π Login"):
|
42 |
+
if input_key:
|
43 |
+
st.session_state.primary_key = input_key
|
44 |
+
st.session_state.logged_in = True
|
45 |
+
st.rerun()
|
46 |
+
else:
|
47 |
+
st.error("Please enter a valid key")
|
48 |
+
else:
|
49 |
+
# Initialize Cosmos DB client
|
50 |
+
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
51 |
+
database = client.get_database_client(DATABASE_NAME)
|
52 |
+
container = database.get_container_client(CONTAINER_NAME)
|
53 |
+
|
54 |
+
# Input fields
|
55 |
+
st.subheader("π Enter Record Details")
|
56 |
+
id = st.text_input("ID")
|
57 |
+
name = st.text_input("Name")
|
58 |
+
document = st.text_area("Document")
|
59 |
+
evaluation_text = st.text_area("Evaluation Text")
|
60 |
+
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
61 |
+
|
62 |
+
col1, col2 = st.columns(2)
|
63 |
+
|
64 |
+
# Insert Record button
|
65 |
+
with col1:
|
66 |
+
if st.button("πΎ Insert Record"):
|
67 |
+
record = {
|
68 |
+
"id": id,
|
69 |
+
"name": name,
|
70 |
+
"document": document,
|
71 |
+
"evaluationText": evaluation_text,
|
72 |
+
"evaluationScore": evaluation_score
|
73 |
+
}
|
74 |
+
|
75 |
+
success, response = insert_record(record)
|
76 |
+
if success:
|
77 |
+
st.success("β
Record inserted successfully!")
|
78 |
+
st.json(response)
|
79 |
+
else:
|
80 |
+
st.error(f"β Failed to insert record: {response}")
|
81 |
+
|
82 |
+
# Call Procedure button
|
83 |
+
with col2:
|
84 |
+
if st.button("π§ Call Procedure"):
|
85 |
+
record = {
|
86 |
+
"id": id,
|
87 |
+
"name": name,
|
88 |
+
"document": document,
|
89 |
+
"evaluationText": evaluation_text,
|
90 |
+
"evaluationScore": evaluation_score
|
91 |
+
}
|
92 |
+
|
93 |
+
success, response = call_stored_procedure(record)
|
94 |
+
if success:
|
95 |
+
st.success("β
Stored procedure executed successfully!")
|
96 |
+
st.json(response)
|
97 |
+
else:
|
98 |
+
st.error(f"β Failed to execute stored procedure: {response}")
|
99 |
+
|
100 |
+
# Logout button
|
101 |
+
if st.button("πͺ Logout"):
|
102 |
+
st.session_state.logged_in = False
|
103 |
+
st.rerun()
|
104 |
+
|
105 |
+
# Display connection info
|
106 |
+
st.sidebar.subheader("π Connection Information")
|
107 |
+
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
108 |
+
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
109 |
+
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
110 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|