awacke1 commited on
Commit
ba1fe9f
β€’
1 Parent(s): 12b09fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -45
app.py CHANGED
@@ -1,6 +1,8 @@
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/"
@@ -10,26 +12,40 @@ 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(container, long_text):
21
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  result = container.scripts.execute_stored_procedure(
23
- sproc='processPrompt', # Replace with your actual stored procedure name
24
  params=[long_text],
25
- partition_key=None # You may need to adjust this based on your container's partition key
26
  )
27
  return True, result
28
  except Exception as e:
29
- return False, str(e)
 
30
 
31
  # Streamlit app
32
- st.title("🌟 Cosmos DB Record Insertion and Stored Procedure")
33
 
34
  # Login section
35
  if 'logged_in' not in st.session_state:
@@ -37,8 +53,7 @@ if 'logged_in' not in st.session_state:
37
 
38
  if not st.session_state.logged_in:
39
  st.subheader("πŸ” Login")
40
- #input_key = st.text_input("Enter your Cosmos DB Primary Key", type="password")
41
- input_key = Key
42
  if st.button("πŸš€ Login"):
43
  if input_key:
44
  st.session_state.primary_key = input_key
@@ -52,47 +67,52 @@ else:
52
  database = client.get_database_client(DATABASE_NAME)
53
  container = database.get_container_client(CONTAINER_NAME)
54
 
55
- # Input fields for record insertion
56
- st.subheader("πŸ“ Enter Record Details")
57
- id = st.text_input("ID")
58
- name = st.text_input("Name")
59
- age = st.number_input("Age", min_value=0, max_value=150)
60
- city = st.text_input("City")
61
 
62
- # Submit button for record insertion
63
- if st.button("πŸ’Ύ Insert Record"):
64
- record = {
65
- "id": id,
66
- "name": name,
67
- "age": age,
68
- "city": city
69
- }
70
-
71
- success, response = insert_record(record)
72
- if success:
73
- st.success("βœ… Record inserted successfully!")
74
- st.json(response)
75
- else:
76
- st.error(f"❌ Failed to insert record: {response}")
 
 
 
 
 
 
77
 
78
- # Separator
79
- st.markdown("---")
 
80
 
81
- # Input field for long text
82
- st.subheader("πŸ“ Enter Long Text for Stored Procedure")
83
- long_text = st.text_area("Long Text", height=200)
 
84
 
85
- # Button to call stored procedure
86
- if st.button("πŸš€ Call Stored Procedure"):
87
- if long_text:
88
- success, result = call_stored_procedure(container, long_text)
89
- if success:
90
- st.success("βœ… Stored procedure executed successfully!")
91
- st.json(result)
 
 
 
92
  else:
93
- st.error(f"❌ Failed to execute stored procedure: {result}")
94
- else:
95
- st.warning("Please enter some text before calling the stored procedure.")
96
 
97
  # Logout button
98
  if st.button("πŸšͺ Logout"):
@@ -104,4 +124,8 @@ else:
104
  st.sidebar.text(f"Endpoint: {ENDPOINT}")
105
  st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
106
  st.sidebar.text(f"Database: {DATABASE_NAME}")
107
- st.sidebar.text(f"Container: {CONTAINER_NAME}")
 
 
 
 
 
1
  import streamlit as st
2
  from azure.cosmos import CosmosClient
3
  import os
4
+ import traceback
5
+ import json
6
 
7
  # Cosmos DB configuration
8
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
 
12
  CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
13
  Key = os.environ.get("Key")
14
 
15
+ def insert_record(container, record):
16
  try:
17
  response = container.create_item(body=record)
18
  return True, response
19
  except Exception as e:
20
  return False, str(e)
21
 
22
+ def call_stored_procedure(container, long_text, partition_key_type):
23
  try:
24
+ if partition_key_type == "first_word":
25
+ partition_key = long_text.split()[0]
26
+ elif partition_key_type == "first_two_words":
27
+ partition_key = " ".join(long_text.split()[:2])
28
+ elif partition_key_type == "first_line":
29
+ partition_key = long_text.split('\n')[0].strip()
30
+ elif partition_key_type == "json":
31
+ # Assume the text is a JSON string and extract a specific field
32
+ json_data = json.loads(long_text)
33
+ partition_key = json_data.get("partitionKey", "default")
34
+ else:
35
+ partition_key = "default"
36
+
37
  result = container.scripts.execute_stored_procedure(
38
+ sproc='processQTPrompts', # Make sure this matches your stored procedure name
39
  params=[long_text],
40
+ partition_key=partition_key
41
  )
42
  return True, result
43
  except Exception as e:
44
+ error_msg = f"Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
45
+ return False, error_msg
46
 
47
  # Streamlit app
48
+ st.title("🌟 Cosmos DB Record Insertion and Stored Procedure Caller")
49
 
50
  # Login section
51
  if 'logged_in' not in st.session_state:
 
53
 
54
  if not st.session_state.logged_in:
55
  st.subheader("πŸ” Login")
56
+ input_key = st.text_input("Enter your Cosmos DB Primary Key", type="password", value=Key)
 
57
  if st.button("πŸš€ Login"):
58
  if input_key:
59
  st.session_state.primary_key = input_key
 
67
  database = client.get_database_client(DATABASE_NAME)
68
  container = database.get_container_client(CONTAINER_NAME)
69
 
70
+ # Create tabs for different operations
71
+ tab1, tab2 = st.tabs(["Insert Record", "Call Stored Procedure"])
 
 
 
 
72
 
73
+ with tab1:
74
+ st.subheader("πŸ“ Enter Record Details")
75
+ id = st.text_input("ID")
76
+ name = st.text_input("Name")
77
+ age = st.number_input("Age", min_value=0, max_value=150)
78
+ city = st.text_input("City")
79
+
80
+ if st.button("πŸ’Ύ Insert Record"):
81
+ record = {
82
+ "id": id,
83
+ "name": name,
84
+ "age": age,
85
+ "city": city
86
+ }
87
+
88
+ success, response = insert_record(container, record)
89
+ if success:
90
+ st.success("βœ… Record inserted successfully!")
91
+ st.json(response)
92
+ else:
93
+ st.error(f"❌ Failed to insert record: {response}")
94
 
95
+ with tab2:
96
+ st.subheader("πŸ“ Enter Long Text for Stored Procedure")
97
+ long_text = st.text_area("Long Text", height=200)
98
 
99
+ partition_key_type = st.selectbox(
100
+ "Select Partition Key Type",
101
+ ["first_word", "first_two_words", "first_line", "json", "default"]
102
+ )
103
 
104
+ if st.button("πŸš€ Call Stored Procedure"):
105
+ if long_text:
106
+ with st.spinner('Calling stored procedure...'):
107
+ success, result = call_stored_procedure(container, long_text, partition_key_type)
108
+ if success:
109
+ st.success("βœ… Stored procedure executed successfully!")
110
+ st.json(result)
111
+ else:
112
+ st.error("❌ Failed to execute stored procedure")
113
+ st.text(result) # This will show the detailed error message
114
  else:
115
+ st.warning("Please enter some text before calling the stored procedure.")
 
 
116
 
117
  # Logout button
118
  if st.button("πŸšͺ Logout"):
 
124
  st.sidebar.text(f"Endpoint: {ENDPOINT}")
125
  st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
126
  st.sidebar.text(f"Database: {DATABASE_NAME}")
127
+ st.sidebar.text(f"Container: {CONTAINER_NAME}")
128
+
129
+
130
+
131
+