Spaces:
Running
Running
Refactor conversation deletion logic: Enhance file deletion process for conversations by supporting multiple annotation file paths and updating file retrieval method.
Browse files
app.py
CHANGED
|
@@ -802,7 +802,7 @@ def save_system_prompt(prompt_text):
|
|
| 802 |
except Exception as e:
|
| 803 |
logger.error(f"Error saving system prompt: {str(e)}")
|
| 804 |
return f"Error saving prompt: {str(e)}"
|
| 805 |
-
|
| 806 |
def delete_conversation_from_huggingface(conversation_id):
|
| 807 |
"""
|
| 808 |
Delete conversation files from Hugging Face dataset by ID
|
|
@@ -823,16 +823,18 @@ def delete_conversation_from_huggingface(conversation_id):
|
|
| 823 |
# Get list of files in dataset
|
| 824 |
try:
|
| 825 |
# Get all files in dataset
|
| 826 |
-
files = api.
|
| 827 |
repo_id=DATASET_ID,
|
| 828 |
repo_type="dataset"
|
| 829 |
)
|
| 830 |
|
| 831 |
# Find files with matching conversation ID in chat history
|
|
|
|
|
|
|
| 832 |
chat_files = [
|
| 833 |
-
file
|
| 834 |
-
if file.
|
| 835 |
-
f"{conversation_id}_" in os.path.basename(file
|
| 836 |
]
|
| 837 |
|
| 838 |
if not chat_files:
|
|
@@ -851,17 +853,30 @@ def delete_conversation_from_huggingface(conversation_id):
|
|
| 851 |
logger.error(f"Error deleting file {file_path} from dataset: {str(e)}")
|
| 852 |
|
| 853 |
# Try to delete annotation file if it exists
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
return True, f"Deleted {len(chat_files)} file(s) from dataset for conversation: {conversation_id}"
|
| 867 |
|
|
@@ -870,7 +885,7 @@ def delete_conversation_from_huggingface(conversation_id):
|
|
| 870 |
|
| 871 |
except Exception as e:
|
| 872 |
logger.error(f"Error deleting conversation from dataset: {str(e)}")
|
| 873 |
-
return False, f"Error deleting conversation from dataset: {str(e)}"
|
| 874 |
|
| 875 |
def delete_conversation(conversation_id, evaluator):
|
| 876 |
"""
|
|
|
|
| 802 |
except Exception as e:
|
| 803 |
logger.error(f"Error saving system prompt: {str(e)}")
|
| 804 |
return f"Error saving prompt: {str(e)}"
|
| 805 |
+
|
| 806 |
def delete_conversation_from_huggingface(conversation_id):
|
| 807 |
"""
|
| 808 |
Delete conversation files from Hugging Face dataset by ID
|
|
|
|
| 823 |
# Get list of files in dataset
|
| 824 |
try:
|
| 825 |
# Get all files in dataset
|
| 826 |
+
files = api.list_repo_files(
|
| 827 |
repo_id=DATASET_ID,
|
| 828 |
repo_type="dataset"
|
| 829 |
)
|
| 830 |
|
| 831 |
# Find files with matching conversation ID in chat history
|
| 832 |
+
# В пути может быть chat_history или chat-history
|
| 833 |
+
chat_dir = os.path.basename(DATASET_CHAT_HISTORY_PATH)
|
| 834 |
chat_files = [
|
| 835 |
+
file for file in files
|
| 836 |
+
if (file.startswith(f"{chat_dir}/") or file.startswith(f"chat_history/") or file.startswith(f"chat-history/")) and
|
| 837 |
+
f"{conversation_id}_" in os.path.basename(file)
|
| 838 |
]
|
| 839 |
|
| 840 |
if not chat_files:
|
|
|
|
| 853 |
logger.error(f"Error deleting file {file_path} from dataset: {str(e)}")
|
| 854 |
|
| 855 |
# Try to delete annotation file if it exists
|
| 856 |
+
# Учитываем разные варианты пути к аннотациям
|
| 857 |
+
annotations_base = os.path.basename(DATASET_ANNOTATIONS_PATH)
|
| 858 |
+
annotation_paths = [
|
| 859 |
+
f"{annotations_base}/annotation_{conversation_id}.json",
|
| 860 |
+
f"{annotations_base}/evaluation_{conversation_id}.json",
|
| 861 |
+
f"annotations/annotation_{conversation_id}.json",
|
| 862 |
+
f"annotations/evaluation_{conversation_id}.json",
|
| 863 |
+
f"evaluations/annotation_{conversation_id}.json",
|
| 864 |
+
f"evaluations/evaluation_{conversation_id}.json",
|
| 865 |
+
f"{chat_dir}/evaluations/evaluation_{conversation_id}.json"
|
| 866 |
+
]
|
| 867 |
+
|
| 868 |
+
for annotation_path in annotation_paths:
|
| 869 |
+
try:
|
| 870 |
+
if annotation_path in files:
|
| 871 |
+
api.delete_file(
|
| 872 |
+
repo_id=DATASET_ID,
|
| 873 |
+
repo_type="dataset",
|
| 874 |
+
path_in_repo=annotation_path
|
| 875 |
+
)
|
| 876 |
+
logger.info(f"Deleted annotation file from HF dataset: {annotation_path}")
|
| 877 |
+
except Exception as e:
|
| 878 |
+
# It's okay if annotation file doesn't exist
|
| 879 |
+
logger.debug(f"Could not delete annotation file {annotation_path}: {str(e)}")
|
| 880 |
|
| 881 |
return True, f"Deleted {len(chat_files)} file(s) from dataset for conversation: {conversation_id}"
|
| 882 |
|
|
|
|
| 885 |
|
| 886 |
except Exception as e:
|
| 887 |
logger.error(f"Error deleting conversation from dataset: {str(e)}")
|
| 888 |
+
return False, f"Error deleting conversation from dataset: {str(e)}"
|
| 889 |
|
| 890 |
def delete_conversation(conversation_id, evaluator):
|
| 891 |
"""
|