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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from azure.cosmos import CosmosClient, PartitionKey
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from streamlit.components.v1 import html
|
| 6 |
+
|
| 7 |
+
# Cosmos DB configuration
|
| 8 |
+
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
| 9 |
+
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
| 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 |
+
def call_stored_procedure(record):
|
| 22 |
+
try:
|
| 23 |
+
response = container.scripts.execute_stored_procedure(
|
| 24 |
+
sproc="processPrompt",
|
| 25 |
+
params=[record],
|
| 26 |
+
partition_key=record['id']
|
| 27 |
+
)
|
| 28 |
+
return True, response
|
| 29 |
+
except Exception as e:
|
| 30 |
+
error_message = f"Error type: {type(e).__name__}\nError message: {str(e)}"
|
| 31 |
+
if hasattr(e, 'sub_status'):
|
| 32 |
+
error_message += f"\nSub-status: {e.sub_status}"
|
| 33 |
+
if hasattr(e, 'response'):
|
| 34 |
+
error_message += f"\nResponse: {e.response}"
|
| 35 |
+
return False, error_message
|
| 36 |
+
|
| 37 |
+
def fetch_all_records():
|
| 38 |
+
query = "SELECT * FROM c"
|
| 39 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
| 40 |
+
return pd.DataFrame(items)
|
| 41 |
+
|
| 42 |
+
def delete_records(ids):
|
| 43 |
+
try:
|
| 44 |
+
for id in ids:
|
| 45 |
+
container.delete_item(item=id, partition_key=id)
|
| 46 |
+
return True, f"Successfully deleted {len(ids)} records"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return False, f"Error deleting records: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def create_html_component(records):
|
| 51 |
+
html_template = """
|
| 52 |
+
<style>
|
| 53 |
+
.song-list {
|
| 54 |
+
font-family: Arial, sans-serif;
|
| 55 |
+
}
|
| 56 |
+
.song-item {
|
| 57 |
+
display: flex;
|
| 58 |
+
align-items: center;
|
| 59 |
+
margin-bottom: 10px;
|
| 60 |
+
padding: 10px;
|
| 61 |
+
border: 1px solid #ddd;
|
| 62 |
+
border-radius: 5px;
|
| 63 |
+
}
|
| 64 |
+
.song-image {
|
| 65 |
+
width: 50px;
|
| 66 |
+
height: 50px;
|
| 67 |
+
margin-right: 10px;
|
| 68 |
+
}
|
| 69 |
+
.song-details {
|
| 70 |
+
flex-grow: 1;
|
| 71 |
+
}
|
| 72 |
+
.song-title {
|
| 73 |
+
font-weight: bold;
|
| 74 |
+
}
|
| 75 |
+
.song-description {
|
| 76 |
+
font-style: italic;
|
| 77 |
+
color: #666;
|
| 78 |
+
}
|
| 79 |
+
.song-actions {
|
| 80 |
+
display: flex;
|
| 81 |
+
align-items: center;
|
| 82 |
+
}
|
| 83 |
+
.song-actions button {
|
| 84 |
+
margin-left: 5px;
|
| 85 |
+
}
|
| 86 |
+
</style>
|
| 87 |
+
<div class="song-list">
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
for record in records:
|
| 91 |
+
html_template += f"""
|
| 92 |
+
<div class="song-item">
|
| 93 |
+
<img src="https://via.placeholder.com/50" class="song-image" alt="Song thumbnail">
|
| 94 |
+
<div class="song-details">
|
| 95 |
+
<div class="song-title">{record['name']}</div>
|
| 96 |
+
<div class="song-description">{record['document']}</div>
|
| 97 |
+
</div>
|
| 98 |
+
<div class="song-actions">
|
| 99 |
+
<button onclick="Streamlit.setComponentValue({{action: 'extend', id: '{record['id']}'}})">Extend</button>
|
| 100 |
+
<label>
|
| 101 |
+
Public
|
| 102 |
+
<input type="checkbox" onclick="Streamlit.setComponentValue({{action: 'toggle_public', id: '{record['id']}'}})">
|
| 103 |
+
</label>
|
| 104 |
+
<button onclick="Streamlit.setComponentValue({{action: 'like', id: '{record['id']}'}})">π</button>
|
| 105 |
+
<button onclick="Streamlit.setComponentValue({{action: 'dislike', id: '{record['id']}'}})">π</button>
|
| 106 |
+
</div>
|
| 107 |
+
</div>
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
html_template += "</div>"
|
| 111 |
+
return html_template
|
| 112 |
+
|
| 113 |
+
# Streamlit app
|
| 114 |
+
st.title("π Cosmos DB Record Management")
|
| 115 |
+
|
| 116 |
+
# Sidebar
|
| 117 |
+
st.sidebar.title("Suno")
|
| 118 |
+
st.sidebar.markdown("### Home")
|
| 119 |
+
st.sidebar.markdown("### Create")
|
| 120 |
+
st.sidebar.markdown("### Library")
|
| 121 |
+
st.sidebar.markdown("### Explore (BETA)")
|
| 122 |
+
st.sidebar.markdown("---")
|
| 123 |
+
st.sidebar.markdown("2360 credits")
|
| 124 |
+
st.sidebar.markdown("### Subscription")
|
| 125 |
+
st.sidebar.markdown("### What's New? 5")
|
| 126 |
+
st.sidebar.markdown("### Community")
|
| 127 |
+
st.sidebar.markdown("### Help")
|
| 128 |
+
st.sidebar.markdown("### About")
|
| 129 |
+
|
| 130 |
+
# Login section
|
| 131 |
+
if 'logged_in' not in st.session_state:
|
| 132 |
+
st.session_state.logged_in = False
|
| 133 |
+
|
| 134 |
+
if not st.session_state.logged_in:
|
| 135 |
+
st.subheader("π Login")
|
| 136 |
+
input_key = Key
|
| 137 |
+
if st.button("π Login"):
|
| 138 |
+
if input_key:
|
| 139 |
+
st.session_state.primary_key = input_key
|
| 140 |
+
st.session_state.logged_in = True
|
| 141 |
+
st.rerun()
|
| 142 |
+
else:
|
| 143 |
+
st.error("Invalid key. Please check your environment variables.")
|
| 144 |
+
else:
|
| 145 |
+
# Initialize Cosmos DB client
|
| 146 |
+
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
| 147 |
+
database = client.get_database_client(DATABASE_NAME)
|
| 148 |
+
container = database.get_container_client(CONTAINER_NAME)
|
| 149 |
+
|
| 150 |
+
# Main Content Area
|
| 151 |
+
st.title("Create")
|
| 152 |
+
st.checkbox("Custom Mode", key="custom_mode")
|
| 153 |
+
st.text_area("Song Description", "a futuristic anime song about a literal banana", key="song_description")
|
| 154 |
+
st.radio("Instrumental", ["Yes", "No"], key="instrumental")
|
| 155 |
+
st.selectbox("Version", ["v1", "v2", "v3"], key="version")
|
| 156 |
+
st.button("Create π΅")
|
| 157 |
+
|
| 158 |
+
# Fetch and display all records
|
| 159 |
+
st.subheader("π All Records")
|
| 160 |
+
records = fetch_all_records().to_dict('records')
|
| 161 |
+
|
| 162 |
+
# Create and render the HTML component
|
| 163 |
+
html_component = create_html_component(records)
|
| 164 |
+
component_value = html(html_component, height=400)
|
| 165 |
+
|
| 166 |
+
# Handle component interactions
|
| 167 |
+
if component_value:
|
| 168 |
+
action = component_value['action']
|
| 169 |
+
record_id = component_value['id']
|
| 170 |
+
if action == 'extend':
|
| 171 |
+
st.write(f"Extending record {record_id}")
|
| 172 |
+
elif action == 'toggle_public':
|
| 173 |
+
st.write(f"Toggling public status for record {record_id}")
|
| 174 |
+
elif action == 'like':
|
| 175 |
+
st.write(f"Liked record {record_id}")
|
| 176 |
+
elif action == 'dislike':
|
| 177 |
+
st.write(f"Disliked record {record_id}")
|
| 178 |
+
|
| 179 |
+
# Add delete and download buttons
|
| 180 |
+
col1, col2 = st.columns(2)
|
| 181 |
+
with col1:
|
| 182 |
+
if st.button("ποΈ Delete Selected"):
|
| 183 |
+
st.warning("Deletion functionality needs to be implemented for the new component")
|
| 184 |
+
|
| 185 |
+
with col2:
|
| 186 |
+
if st.download_button("π₯ Download Data", pd.DataFrame(records).to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
| 187 |
+
st.success("Data downloaded successfully!")
|
| 188 |
+
|
| 189 |
+
# Input fields for new record
|
| 190 |
+
st.subheader("π Enter New Record Details")
|
| 191 |
+
new_id = st.text_input("ID")
|
| 192 |
+
new_name = st.text_input("Name")
|
| 193 |
+
new_document = st.text_area("Document")
|
| 194 |
+
new_evaluation_text = st.text_area("Evaluation Text")
|
| 195 |
+
new_evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
| 196 |
+
|
| 197 |
+
col1, col2 = st.columns(2)
|
| 198 |
+
|
| 199 |
+
# Insert Record button
|
| 200 |
+
with col1:
|
| 201 |
+
if st.button("πΎ Insert Record"):
|
| 202 |
+
record = {
|
| 203 |
+
"id": new_id,
|
| 204 |
+
"name": new_name,
|
| 205 |
+
"document": new_document,
|
| 206 |
+
"evaluationText": new_evaluation_text,
|
| 207 |
+
"evaluationScore": new_evaluation_score
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
success, response = insert_record(record)
|
| 211 |
+
if success:
|
| 212 |
+
st.success("β
Record inserted successfully!")
|
| 213 |
+
st.json(response)
|
| 214 |
+
else:
|
| 215 |
+
st.error(f"β Failed to insert record: {response}")
|
| 216 |
+
st.rerun()
|
| 217 |
+
|
| 218 |
+
# Call Procedure button
|
| 219 |
+
with col2:
|
| 220 |
+
if st.button("π§ Call Procedure"):
|
| 221 |
+
record = {
|
| 222 |
+
"id": new_id,
|
| 223 |
+
"name": new_name,
|
| 224 |
+
"document": new_document,
|
| 225 |
+
"evaluationText": new_evaluation_text,
|
| 226 |
+
"evaluationScore": new_evaluation_score
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
success, response = call_stored_procedure(record)
|
| 230 |
+
if success:
|
| 231 |
+
st.success("β
Stored procedure executed successfully!")
|
| 232 |
+
st.json(response)
|
| 233 |
+
else:
|
| 234 |
+
st.error(f"β Failed to execute stored procedure: {response}")
|
| 235 |
+
|
| 236 |
+
# Logout button
|
| 237 |
+
if st.button("πͺ Logout"):
|
| 238 |
+
st.session_state.logged_in = False
|
| 239 |
+
st.rerun()
|
| 240 |
+
|
| 241 |
+
# Display connection info
|
| 242 |
+
st.sidebar.markdown("---")
|
| 243 |
+
st.sidebar.subheader("π Connection Information")
|
| 244 |
+
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 245 |
+
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 246 |
+
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 247 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
| 248 |
+
|
| 249 |
+
# Preview section
|
| 250 |
+
st.markdown("---")
|
| 251 |
+
st.markdown("### Select a song to preview.")
|