awacke1 commited on
Commit
7064bbe
β€’
1 Parent(s): 291583f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -75
app.py CHANGED
@@ -1,51 +1,25 @@
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/"
9
  SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
 
10
  # You'll need to set these environment variables or use Azure Key Vault
11
  DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
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,7 +27,8 @@ 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,52 +42,28 @@ else:
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"):
 
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
+
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
  # Streamlit app
22
+ st.title("🌟 Cosmos DB Record Insertion")
23
 
24
  # Login section
25
  if 'logged_in' not in st.session_state:
 
27
 
28
  if not st.session_state.logged_in:
29
  st.subheader("πŸ” Login")
30
+ #input_key = st.text_input("Enter your Cosmos DB Primary Key", type="password")
31
+ input_key = Key
32
  if st.button("πŸš€ Login"):
33
  if input_key:
34
  st.session_state.primary_key = input_key
 
42
  database = client.get_database_client(DATABASE_NAME)
43
  container = database.get_container_client(CONTAINER_NAME)
44
 
45
+ # Input fields
46
+ st.subheader("πŸ“ Enter Record Details")
47
+ id = st.text_input("ID")
48
+ name = st.text_input("Name")
49
+ age = st.number_input("Age", min_value=0, max_value=150)
50
+ city = st.text_input("City")
51
 
52
+ # Submit button
53
+ if st.button("πŸ’Ύ Insert Record"):
54
+ record = {
55
+ "id": id,
56
+ "name": name,
57
+ "age": age,
58
+ "city": city
59
+ }
60
+
61
+ success, response = insert_record(record)
62
+ if success:
63
+ st.success("βœ… Record inserted successfully!")
64
+ st.json(response)
65
+ else:
66
+ st.error(f"❌ Failed to insert record: {response}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Logout button
69
  if st.button("πŸšͺ Logout"):