Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -243,8 +243,75 @@ def push_to_github(local_path, repo, github_token):
|
|
| 243 |
|
| 244 |
origin.push(refspec=f'{current_branch}:{current_branch}')
|
| 245 |
|
| 246 |
-
|
|
|
|
| 247 |
def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
try:
|
| 249 |
if not container:
|
| 250 |
st.error("Cosmos DB container is not initialized.")
|
|
|
|
| 243 |
|
| 244 |
origin.push(refspec=f'{current_branch}:{current_branch}')
|
| 245 |
|
| 246 |
+
|
| 247 |
+
|
| 248 |
def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=None):
|
| 249 |
+
max_retries = 5
|
| 250 |
+
retry_delay = 1 # seconds
|
| 251 |
+
|
| 252 |
+
def generate_unique_id():
|
| 253 |
+
return f"{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}-{str(uuid.uuid4())}"
|
| 254 |
+
|
| 255 |
+
for attempt in range(max_retries):
|
| 256 |
+
try:
|
| 257 |
+
if not container:
|
| 258 |
+
st.error("Cosmos DB container is not initialized.")
|
| 259 |
+
return
|
| 260 |
+
|
| 261 |
+
new_id = generate_unique_id()
|
| 262 |
+
|
| 263 |
+
if clone_id:
|
| 264 |
+
# If clone_id is provided, we're cloning an existing document
|
| 265 |
+
try:
|
| 266 |
+
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
| 267 |
+
new_doc = existing_doc.copy()
|
| 268 |
+
new_doc['id'] = new_id
|
| 269 |
+
new_doc['cloned_from'] = clone_id
|
| 270 |
+
new_doc['cloned_at'] = datetime.utcnow().isoformat()
|
| 271 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 272 |
+
st.error(f"Document with ID {clone_id} not found for cloning.")
|
| 273 |
+
return
|
| 274 |
+
else:
|
| 275 |
+
# If no clone_id, we're creating a new document
|
| 276 |
+
new_doc = {
|
| 277 |
+
'id': new_id,
|
| 278 |
+
'query': query,
|
| 279 |
+
'response': response,
|
| 280 |
+
'created_at': datetime.utcnow().isoformat()
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
# Insert the new document
|
| 284 |
+
container.create_item(body=new_doc)
|
| 285 |
+
|
| 286 |
+
st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {new_id}")
|
| 287 |
+
|
| 288 |
+
# Refresh the documents in the session state
|
| 289 |
+
st.session_state.documents = list(container.query_items(
|
| 290 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
| 291 |
+
enable_cross_partition_query=True
|
| 292 |
+
))
|
| 293 |
+
|
| 294 |
+
return new_id
|
| 295 |
+
|
| 296 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 297 |
+
if e.status_code == 409: # Conflict error
|
| 298 |
+
st.warning(f"ID conflict occurred. Retrying... (Attempt {attempt + 1})")
|
| 299 |
+
time.sleep(retry_delay)
|
| 300 |
+
else:
|
| 301 |
+
st.error(f"Error saving to Cosmos DB: {e}")
|
| 302 |
+
return
|
| 303 |
+
except Exception as e:
|
| 304 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
| 305 |
+
return
|
| 306 |
+
|
| 307 |
+
st.error("Failed to save document after maximum retries.")
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
# 💾 Save or clone to Cosmos DB - Because every document deserves a twin
|
| 314 |
+
def save_or_clone_to_cosmos_db2(container, query=None, response=None, clone_id=None):
|
| 315 |
try:
|
| 316 |
if not container:
|
| 317 |
st.error("Cosmos DB container is not initialized.")
|