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B configuration
|
| 7 |
+
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
| 8 |
+
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
| 9 |
+
|
| 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 |
+
|
| 14 |
+
def create_stored_procedure(container):
|
| 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.prompt = @prompt",
|
| 31 |
+
parameters: [{ name: "@prompt", 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 |
+
prompt: 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 |
+
return container.scripts.execute_stored_procedure(
|
| 89 |
+
sproc='processQTPrompts',
|
| 90 |
+
params=[prompt_text],
|
| 91 |
+
partition_key=None
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Streamlit app
|
| 95 |
+
st.title("๐ QT Prompt Processor")
|
| 96 |
+
|
| 97 |
+
# Login section
|
| 98 |
+
if 'logged_in' not in st.session_state:
|
| 99 |
+
st.session_state.logged_in = False
|
| 100 |
+
|
| 101 |
+
if not st.session_state.logged_in:
|
| 102 |
+
st.subheader("๐ Login")
|
| 103 |
+
input_key = st.text_input("Enter your Cosmos DB Primary Key", type="password")
|
| 104 |
+
if st.button("๐ Login"):
|
| 105 |
+
if input_key:
|
| 106 |
+
st.session_state.primary_key = input_key
|
| 107 |
+
st.session_state.logged_in = True
|
| 108 |
+
st.experimental_rerun()
|
| 109 |
+
else:
|
| 110 |
+
st.error("Please enter a valid key")
|
| 111 |
+
else:
|
| 112 |
+
# Initialize Cosmos DB client
|
| 113 |
+
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
| 114 |
+
database = client.get_database_client(DATABASE_NAME)
|
| 115 |
+
container = database.get_container_client(CONTAINER_NAME)
|
| 116 |
+
|
| 117 |
+
# Ensure the stored procedure exists
|
| 118 |
+
ensure_stored_procedure_exists(container)
|
| 119 |
+
|
| 120 |
+
# Input field for QT prompts
|
| 121 |
+
st.subheader("๐ Enter QT Prompts")
|
| 122 |
+
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"
|
| 123 |
+
qt_prompts = st.text_area("QT Prompts", value=default_text, height=300)
|
| 124 |
+
|
| 125 |
+
# Submit button
|
| 126 |
+
if st.button("๐ Process QT Prompts"):
|
| 127 |
+
results = process_qt_prompts(container, qt_prompts)
|
| 128 |
+
|
| 129 |
+
# Display results in a dataframe
|
| 130 |
+
df_data = [{"Prompt": item['prompt'], "Occurrence Count": item['occurrenceCount']} for item in results]
|
| 131 |
+
st.dataframe(df_data)
|
| 132 |
+
|
| 133 |
+
# Logout button
|
| 134 |
+
if st.button("๐ช Logout"):
|
| 135 |
+
st.session_state.logged_in = False
|
| 136 |
+
st.experimental_rerun()
|
| 137 |
+
|
| 138 |
+
# Display connection info
|
| 139 |
+
st.sidebar.subheader("๐ Connection Information")
|
| 140 |
+
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 141 |
+
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 142 |
+
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 143 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|