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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from azure.cosmos import CosmosClient, PartitionKey
|
| 4 |
+
from azure.storage.blob import BlobServiceClient
|
| 5 |
+
import requests
|
| 6 |
+
import glob
|
| 7 |
+
|
| 8 |
+
# Environment Variables
|
| 9 |
+
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
| 10 |
+
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
| 11 |
+
|
| 12 |
+
# Initialize Azure Cosmos DB Client
|
| 13 |
+
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
| 14 |
+
|
| 15 |
+
# Initialize Azure Blob Storage Client
|
| 16 |
+
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
|
| 17 |
+
|
| 18 |
+
# Function to Retrieve and Display Cosmos DB Structure
|
| 19 |
+
def display_cosmos_db_structure():
|
| 20 |
+
st.subheader('Azure Cosmos DB Structure')
|
| 21 |
+
for db_properties in cosmos_client.list_databases():
|
| 22 |
+
db_name = db_properties['id']
|
| 23 |
+
st.markdown(f"#### Database: {db_name}")
|
| 24 |
+
database_client = cosmos_client.get_database_client(db_name)
|
| 25 |
+
|
| 26 |
+
for container_properties in database_client.list_containers():
|
| 27 |
+
container_name = container_properties['id']
|
| 28 |
+
st.markdown(f"- **Container**: {container_name}")
|
| 29 |
+
container_client = database_client.get_container_client(container_name)
|
| 30 |
+
|
| 31 |
+
items = list(container_client.read_all_items())
|
| 32 |
+
for item in items:
|
| 33 |
+
st.markdown(f" - Item: `{item['id']}`") # Replace 'id' with the appropriate key if different
|
| 34 |
+
|
| 35 |
+
# Button to Trigger Display of Cosmos DB Structure
|
| 36 |
+
if st.button('Show Cosmos DB Structure'):
|
| 37 |
+
display_cosmos_db_structure()
|
| 38 |
+
|
| 39 |
+
# Function to Add or Update an Item
|
| 40 |
+
def add_or_update_item(database_client, container_name, item_id, item_data):
|
| 41 |
+
container = database_client.get_container_client(container_name)
|
| 42 |
+
try:
|
| 43 |
+
existing_item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 44 |
+
existing_item.update(item_data)
|
| 45 |
+
container.replace_item(item_id, existing_item)
|
| 46 |
+
except:
|
| 47 |
+
container.create_item(item_data)
|
| 48 |
+
|
| 49 |
+
# Test Function to Insert PNG Images
|
| 50 |
+
def test_insert_png_images():
|
| 51 |
+
# Get the first database and container
|
| 52 |
+
db_properties = next(cosmos_client.list_databases(), None)
|
| 53 |
+
if db_properties:
|
| 54 |
+
db_name = db_properties['id']
|
| 55 |
+
database_client = cosmos_client.get_database_client(db_name)
|
| 56 |
+
container_properties = next(database_client.list_containers(), None)
|
| 57 |
+
if container_properties:
|
| 58 |
+
container_name = container_properties['id']
|
| 59 |
+
|
| 60 |
+
# Insert PNG files
|
| 61 |
+
png_files = glob.glob('*.png')
|
| 62 |
+
for file_name in png_files:
|
| 63 |
+
item_id = os.path.splitext(file_name)[0] # Use file name without extension as ID
|
| 64 |
+
item_data = {"id": item_id, "file_name": file_name}
|
| 65 |
+
add_or_update_item(database_client, container_name, item_id, item_data)
|
| 66 |
+
st.write(f"Inserted: {file_name}")
|
| 67 |
+
|
| 68 |
+
# Displaying Images
|
| 69 |
+
st.subheader("Displaying Images in Container")
|
| 70 |
+
items = list(database_client.get_container_client(container_name).read_all_items())
|
| 71 |
+
for item in items:
|
| 72 |
+
if 'file_name' in item and item['file_name'].endswith('.png'):
|
| 73 |
+
st.image(item['file_name'], caption=item['file_name'])
|
| 74 |
+
else:
|
| 75 |
+
st.error("No container found in the database.")
|
| 76 |
+
else:
|
| 77 |
+
st.error("No database found.")
|
| 78 |
+
|
| 79 |
+
# UI to Test Image Insertion Function
|
| 80 |
+
if st.button('Test Insert PNG Images'):
|
| 81 |
+
test_insert_png_images()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# Function to Add an Item
|
| 86 |
+
def add_item_to_container(database_name, container_name, item):
|
| 87 |
+
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
| 88 |
+
container.create_item(item)
|
| 89 |
+
|
| 90 |
+
# Function to Get All Item Keys from a Container
|
| 91 |
+
def get_all_item_keys(database_name, container_name):
|
| 92 |
+
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
| 93 |
+
items = list(container.query_items(
|
| 94 |
+
query="SELECT c.id FROM c",
|
| 95 |
+
enable_cross_partition_query=True))
|
| 96 |
+
return [item['id'] for item in items]
|
| 97 |
+
|
| 98 |
+
# Function to Retrieve and Display an Item
|
| 99 |
+
def display_item(database_name, container_name, item_id):
|
| 100 |
+
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
| 101 |
+
item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 102 |
+
st.markdown(f"- **Item ID**: {item_id}, **Content**: {json.dumps(item)}")
|
| 103 |
+
|
| 104 |
+
# Test Function to Add and Retrieve Image Prompts
|
| 105 |
+
def test_image_prompts(database_name, container_name):
|
| 106 |
+
# Sample data
|
| 107 |
+
image_prompts = [{"id": f"image_{i}", "prompt": f"Image prompt {i}", "image_url": f"http://example.com/image_{i}.jpg"} for i in range(10)]
|
| 108 |
+
|
| 109 |
+
# Add items
|
| 110 |
+
for item in image_prompts:
|
| 111 |
+
add_item_to_container(database_name, container_name, item)
|
| 112 |
+
|
| 113 |
+
# Retrieve and display items
|
| 114 |
+
item_keys = get_all_item_keys(database_name, container_name)
|
| 115 |
+
for key in item_keys:
|
| 116 |
+
display_item(database_name, container_name, key)
|
| 117 |
+
|
| 118 |
+
# UI to Test Image Prompts Function
|
| 119 |
+
if st.button('Test Image Prompts'):
|
| 120 |
+
test_database_name = st.text_input('Enter Test Database Name')
|
| 121 |
+
test_container_name = st.text_input('Enter Test Container Name')
|
| 122 |
+
if test_database_name and test_container_name:
|
| 123 |
+
test_image_prompts(test_database_name, test_container_name)
|
| 124 |
+
else:
|
| 125 |
+
st.error('Please enter the test database and container names.')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Streamlit UI
|
| 131 |
+
st.title('Azure Services Integration with Streamlit')
|
| 132 |
+
|
| 133 |
+
# Azure Cosmos DB - CRUD Operations
|
| 134 |
+
st.subheader('Azure Cosmos DB - CRUD Operations')
|
| 135 |
+
cosmos_db = st.text_input('Database Name')
|
| 136 |
+
cosmos_container = st.text_input('Container Name')
|
| 137 |
+
item_id = st.text_input("Item ID (for Read, Update, Delete)")
|
| 138 |
+
item_data = st.text_area("Item Data (JSON format, for Create and Update)")
|
| 139 |
+
|
| 140 |
+
if st.button('Create Item in Cosmos DB'):
|
| 141 |
+
container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
|
| 142 |
+
container.create_item(item_data)
|
| 143 |
+
|
| 144 |
+
if st.button('Read Item from Cosmos DB'):
|
| 145 |
+
container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
|
| 146 |
+
item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 147 |
+
st.json(item)
|
| 148 |
+
|
| 149 |
+
# Azure Blob Storage - Upload/Download
|
| 150 |
+
st.subheader('Azure Blob Storage - Upload/Download')
|
| 151 |
+
blob_container = st.text_input('Blob Container')
|
| 152 |
+
blob_file = st.file_uploader('Upload file to Blob')
|
| 153 |
+
|
| 154 |
+
if blob_file is not None and st.button('Upload to Blob'):
|
| 155 |
+
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
|
| 156 |
+
blob_client.upload_blob(blob_file.getvalue())
|
| 157 |
+
|
| 158 |
+
# Azure Functions - Trigger
|
| 159 |
+
st.subheader('Azure Functions - Trigger')
|
| 160 |
+
function_url = st.text_input('Function URL')
|
| 161 |
+
|
| 162 |
+
if st.button('Call Azure Function'):
|
| 163 |
+
response = requests.get(function_url)
|
| 164 |
+
st.write('Function Response:', response.text)
|