Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -244,60 +244,79 @@ def push_to_github(local_path, repo, github_token):
|
|
| 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 |
-
def generate_unique_id():
|
| 249 |
-
return f"{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}-{str(uuid.uuid4())}"
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
new_doc = {
|
| 263 |
'id': new_id,
|
| 264 |
-
'
|
| 265 |
-
'
|
| 266 |
-
'
|
| 267 |
-
'cloned_at': datetime.utcnow().isoformat()
|
| 268 |
}
|
| 269 |
-
except exceptions.CosmosResourceNotFoundError:
|
| 270 |
-
st.error(f"Document with ID {clone_id} not found for cloning.")
|
| 271 |
-
return
|
| 272 |
-
else:
|
| 273 |
-
# If no clone_id, we're creating a new document
|
| 274 |
-
new_doc = {
|
| 275 |
-
'id': new_id,
|
| 276 |
-
'query': query,
|
| 277 |
-
'response': response,
|
| 278 |
-
'created_at': datetime.utcnow().isoformat()
|
| 279 |
-
}
|
| 280 |
-
|
| 281 |
-
# Insert the new document
|
| 282 |
-
response = container.create_item(body=new_doc)
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
st.error(f"An unexpected error occurred: {str(e)}")
|
| 298 |
|
|
|
|
| 299 |
return None
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
|
| 303 |
# 💾 Save or clone to Cosmos DB - Because every document deserves a twin
|
|
|
|
| 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 |
+
def generate_complex_unique_id():
|
| 250 |
+
timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
|
| 251 |
+
random_component = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=8))
|
| 252 |
+
return f"{timestamp}-{random_component}-{str(uuid.uuid4())}"
|
| 253 |
|
| 254 |
+
max_retries = 10
|
| 255 |
+
base_delay = 0.1 # 100 ms
|
| 256 |
|
| 257 |
+
for attempt in range(max_retries):
|
| 258 |
+
try:
|
| 259 |
+
new_id = generate_complex_unique_id()
|
| 260 |
+
|
| 261 |
+
if clone_id:
|
| 262 |
+
try:
|
| 263 |
+
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
| 264 |
+
new_doc = {
|
| 265 |
+
'id': new_id,
|
| 266 |
+
'originalText': existing_doc.get('originalText', ''),
|
| 267 |
+
'qtPrompts': existing_doc.get('qtPrompts', []),
|
| 268 |
+
'cloned_from': clone_id,
|
| 269 |
+
'cloned_at': datetime.utcnow().isoformat()
|
| 270 |
+
}
|
| 271 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 272 |
+
st.error(f"Document with ID {clone_id} not found for cloning.")
|
| 273 |
+
return None
|
| 274 |
+
else:
|
| 275 |
new_doc = {
|
| 276 |
'id': new_id,
|
| 277 |
+
'query': query,
|
| 278 |
+
'response': response,
|
| 279 |
+
'created_at': datetime.utcnow().isoformat()
|
|
|
|
| 280 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
+
# Attempt to create the item
|
| 283 |
+
response = container.create_item(body=new_doc)
|
| 284 |
+
|
| 285 |
+
st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {response['id']}")
|
| 286 |
+
|
| 287 |
+
# Refresh the documents in the session state
|
| 288 |
+
st.session_state.documents = list(container.query_items(
|
| 289 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
| 290 |
+
enable_cross_partition_query=True
|
| 291 |
+
))
|
| 292 |
+
|
| 293 |
+
return response['id']
|
| 294 |
|
| 295 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 296 |
+
if e.status_code == 409: # Conflict error
|
| 297 |
+
delay = base_delay * (2 ** attempt) + random.uniform(0, 0.1)
|
| 298 |
+
st.warning(f"ID conflict occurred. Retrying in {delay:.2f} seconds... (Attempt {attempt + 1})")
|
| 299 |
+
time.sleep(delay)
|
| 300 |
+
else:
|
| 301 |
+
st.error(f"Error saving to Cosmos DB: {e}")
|
| 302 |
+
return None
|
| 303 |
|
| 304 |
+
except Exception as e:
|
| 305 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
| 306 |
+
return None
|
|
|
|
| 307 |
|
| 308 |
+
st.error("Failed to save document after maximum retries.")
|
| 309 |
return None
|
| 310 |
|
| 311 |
+
# Usage in your Streamlit app
|
| 312 |
+
if st.button("📄 Clone Document", key=f'clone_button_{idx}'):
|
| 313 |
+
with st.spinner("Cloning document..."):
|
| 314 |
+
cloned_id = save_or_clone_to_cosmos_db(container, clone_id=doc['id'])
|
| 315 |
+
if cloned_id:
|
| 316 |
+
st.success(f"Document cloned successfully with new ID: {cloned_id}")
|
| 317 |
+
st.rerun()
|
| 318 |
+
else:
|
| 319 |
+
st.error("Failed to clone document. Please try again.")
|
| 320 |
|
| 321 |
|
| 322 |
# 💾 Save or clone to Cosmos DB - Because every document deserves a twin
|