Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -244,69 +244,59 @@ def push_to_github(local_path, repo, github_token):
|
|
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 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
return
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
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 |
-
'
|
279 |
-
'
|
280 |
-
'
|
|
|
281 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
283 |
-
|
284 |
-
|
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 |
-
|
295 |
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
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 |
-
|
308 |
|
|
|
|
|
|
|
|
|
309 |
|
|
|
310 |
|
311 |
|
312 |
|
|
|
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 |
+
try:
|
252 |
+
if not container:
|
253 |
+
st.error("Cosmos DB container is not initialized.")
|
254 |
+
return
|
|
|
255 |
|
256 |
+
new_id = generate_unique_id()
|
257 |
+
|
258 |
+
if clone_id:
|
259 |
+
# If clone_id is provided, we're creating a new document based on an existing one
|
260 |
+
try:
|
261 |
+
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
new_doc = {
|
263 |
'id': new_id,
|
264 |
+
'originalText': existing_doc.get('originalText', ''),
|
265 |
+
'qtPrompts': existing_doc.get('qtPrompts', []),
|
266 |
+
'cloned_from': clone_id,
|
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 |
+
st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {response['id']}")
|
285 |
|
286 |
+
# Refresh the documents in the session state
|
287 |
+
st.session_state.documents = list(container.query_items(
|
288 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
289 |
+
enable_cross_partition_query=True
|
290 |
+
))
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
+
return response['id']
|
293 |
|
294 |
+
except exceptions.CosmosHttpResponseError as e:
|
295 |
+
st.error(f"Error saving to Cosmos DB: {e}")
|
296 |
+
except Exception as e:
|
297 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
298 |
|
299 |
+
return None
|
300 |
|
301 |
|
302 |
|