Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,35 @@
|
|
| 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
|
| 15 |
-
stored_procedure_definition = {
|
| 16 |
-
'id': 'processQTPrompts',
|
| 17 |
-
'body': '''
|
| 18 |
-
function processQTPrompts(promptText) {
|
| 19 |
-
var context = getContext();
|
| 20 |
-
var container = context.getCollection();
|
| 21 |
-
var response = context.getResponse();
|
| 22 |
-
|
| 23 |
-
var prompts = promptText.split('\\n');
|
| 24 |
-
var results = [];
|
| 25 |
-
|
| 26 |
-
for (var i in prompts) {
|
| 27 |
-
var prompt = prompts[i].trim();
|
| 28 |
-
if (prompt.startsWith('QT ')) {
|
| 29 |
-
var querySpec = {
|
| 30 |
-
query: "SELECT * FROM c WHERE c.id = @id",
|
| 31 |
-
parameters: [{ name: "@id", value: prompt }]
|
| 32 |
-
};
|
| 33 |
-
|
| 34 |
-
var isAccepted = container.queryDocuments(
|
| 35 |
-
container.getSelfLink(),
|
| 36 |
-
querySpec,
|
| 37 |
-
function (err, items, responseOptions) {
|
| 38 |
-
if (err) throw err;
|
| 39 |
-
|
| 40 |
-
if (items.length > 0) {
|
| 41 |
-
// Update existing record
|
| 42 |
-
var item = items[0];
|
| 43 |
-
item.occurrenceCount++;
|
| 44 |
-
container.replaceDocument(
|
| 45 |
-
item._self,
|
| 46 |
-
item,
|
| 47 |
-
function (err, replacedItem) {
|
| 48 |
-
if (err) throw err;
|
| 49 |
-
results.push(replacedItem);
|
| 50 |
-
}
|
| 51 |
-
);
|
| 52 |
-
} else {
|
| 53 |
-
// Create new record
|
| 54 |
-
var newItem = {
|
| 55 |
-
id: prompt,
|
| 56 |
-
occurrenceCount: 1,
|
| 57 |
-
evaluation: ""
|
| 58 |
-
};
|
| 59 |
-
container.createDocument(
|
| 60 |
-
container.getSelfLink(),
|
| 61 |
-
newItem,
|
| 62 |
-
function (err, createdItem) {
|
| 63 |
-
if (err) throw err;
|
| 64 |
-
results.push(createdItem);
|
| 65 |
-
}
|
| 66 |
-
);
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
);
|
| 70 |
-
|
| 71 |
-
if (!isAccepted) throw new Error("The query was not accepted by the server.");
|
| 72 |
-
}
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
response.setBody(results);
|
| 76 |
-
}
|
| 77 |
-
'''
|
| 78 |
-
}
|
| 79 |
-
container.scripts.create_stored_procedure(body=stored_procedure_definition)
|
| 80 |
-
|
| 81 |
-
def ensure_stored_procedure_exists(container):
|
| 82 |
-
try:
|
| 83 |
-
container.scripts.get_stored_procedure('processQTPrompts')
|
| 84 |
-
except:
|
| 85 |
-
create_stored_procedure(container)
|
| 86 |
-
|
| 87 |
-
def process_qt_prompts(container, prompt_text):
|
| 88 |
-
# Use the first prompt as the partition key
|
| 89 |
-
first_prompt = prompt_text.split('\n')[0].strip()
|
| 90 |
-
return container.scripts.execute_stored_procedure(
|
| 91 |
-
sproc='processQTPrompts',
|
| 92 |
-
params=[prompt_text],
|
| 93 |
-
partition_key=first_prompt
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
def insert_record(container, record):
|
| 97 |
try:
|
| 98 |
response = container.create_item(body=record)
|
| 99 |
return True, response
|
| 100 |
except Exception as e:
|
| 101 |
return False, str(e)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# Streamlit app
|
| 104 |
-
st.title("๐ Cosmos DB Record Insertion and
|
| 105 |
|
| 106 |
# Login section
|
| 107 |
if 'logged_in' not in st.session_state:
|
|
@@ -124,48 +52,47 @@ else:
|
|
| 124 |
database = client.get_database_client(DATABASE_NAME)
|
| 125 |
container = database.get_container_client(CONTAINER_NAME)
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
name = st.text_input("Name")
|
| 137 |
-
age = st.number_input("Age", min_value=0, max_value=150)
|
| 138 |
-
city = st.text_input("City")
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
"age": age,
|
| 145 |
-
"city": city
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
success, response = insert_record(container, record)
|
| 149 |
if success:
|
| 150 |
-
st.success("โ
|
| 151 |
-
st.json(
|
| 152 |
else:
|
| 153 |
-
st.error(f"โ Failed to
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
st.subheader("๐ Enter QT Prompts")
|
| 157 |
-
default_text = "QT Crystal finders: Bioluminescent crystal caverns, quantum-powered explorers, prismatic hues, alien planet\nQT robot art: Cybernetic metropolis, sentient androids, rogue AI, neon-infused palette\nQT the Lava girl: Volcanic exoplanet, liquid metal rivers, heat-immune heroine, molten metallic palette"
|
| 158 |
-
qt_prompts = st.text_area("QT Prompts", value=default_text, height=300)
|
| 159 |
-
|
| 160 |
-
if st.button("๐ Process QT Prompts"):
|
| 161 |
-
try:
|
| 162 |
-
results = process_qt_prompts(container, qt_prompts)
|
| 163 |
-
|
| 164 |
-
# Display results in a dataframe
|
| 165 |
-
df_data = [{"Prompt": item['id'], "Occurrence Count": item['occurrenceCount']} for item in results]
|
| 166 |
-
st.dataframe(df_data)
|
| 167 |
-
except Exception as e:
|
| 168 |
-
st.error(f"An error occurred: {str(e)}")
|
| 169 |
|
| 170 |
# Logout button
|
| 171 |
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 |
# 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(container, long_text):
|
| 21 |
+
try:
|
| 22 |
+
result = container.scripts.execute_stored_procedure(
|
| 23 |
+
sproc='processQTPrompts', # 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:
|
|
|
|
| 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"):
|