Rulga commited on
Commit
bb55c03
·
1 Parent(s): 0608f93

Add conversation deletion functionality: Implement methods to delete conversation files from Hugging Face dataset and local storage, enhancing data management capabilities.

Browse files
Files changed (1) hide show
  1. app.py +155 -1
app.py CHANGED
@@ -801,6 +801,143 @@ def save_system_prompt(prompt_text):
801
  except Exception as e:
802
  logger.error(f"Error saving system prompt: {str(e)}")
803
  return f"Error saving prompt: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804
 
805
  def initialize_app():
806
  """Initialize app with user preferences"""
@@ -1139,7 +1276,7 @@ with gr.Blocks(css="""
1139
  )
1140
  qa_table = gr.Dataframe(
1141
  value=pd.DataFrame(
1142
- columns=["Conversation ID", "Question", "Timestamp", "Evaluated"]
1143
  ),
1144
  interactive=True,
1145
  wrap=True,
@@ -1156,6 +1293,9 @@ with gr.Blocks(css="""
1156
  interactive=True
1157
  )
1158
  load_btn = gr.Button("Load Conversation")
 
 
 
1159
 
1160
  # Conversation content section
1161
  gr.Markdown("### Evaluate Response")
@@ -1257,6 +1397,20 @@ with gr.Blocks(css="""
1257
  inputs=[min_rating, export_path],
1258
  outputs=[export_status]
1259
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1260
 
1261
  # Model change handler - outside of Tabs but inside Blocks
1262
  model_selector.change(
 
801
  except Exception as e:
802
  logger.error(f"Error saving system prompt: {str(e)}")
803
  return f"Error saving prompt: {str(e)}"
804
+
805
+ def delete_conversation_from_huggingface(conversation_id):
806
+ """
807
+ Delete conversation files from Hugging Face dataset by ID
808
+
809
+ Args:
810
+ conversation_id: ID of conversation to delete
811
+
812
+ Returns:
813
+ Success status (bool) and message (str)
814
+ """
815
+ try:
816
+ from huggingface_hub import HfApi, hf_hub_download
817
+ from huggingface_hub.utils import RepositoryNotFoundError, RevisionNotFoundError
818
+
819
+ if not conversation_id:
820
+ return False, "No conversation ID provided"
821
+
822
+ # Initialize API
823
+ api = HfApi(token=HF_TOKEN)
824
+
825
+ # Get list of files in dataset
826
+ try:
827
+ # Extract just the directory name from DATASET_CHAT_HISTORY_PATH
828
+ dir_name = os.path.basename(DATASET_CHAT_HISTORY_PATH)
829
+
830
+ # Get all files in chat history directory
831
+ files_info = api.list_repo_files(
832
+ repo_id=DATASET_ID,
833
+ repo_type="dataset",
834
+ path=dir_name
835
+ )
836
+
837
+ # Find files with matching conversation ID
838
+ matching_files = [
839
+ file for file in files_info
840
+ if f"{conversation_id}_" in os.path.basename(file)
841
+ ]
842
+
843
+ if not matching_files:
844
+ return False, f"No files found in dataset for conversation ID: {conversation_id}"
845
+
846
+ # Delete each matching file
847
+ for file_path in matching_files:
848
+ try:
849
+ api.delete_file(
850
+ repo_id=DATASET_ID,
851
+ repo_type="dataset",
852
+ path_in_repo=file_path
853
+ )
854
+ logger.info(f"Deleted file from HF dataset: {file_path}")
855
+ except Exception as e:
856
+ logger.error(f"Error deleting file {file_path} from dataset: {str(e)}")
857
+
858
+ # Try to delete evaluation file if it exists
859
+ evaluation_path = f"{dir_name}/evaluations/evaluation_{conversation_id}.json"
860
+ try:
861
+ api.delete_file(
862
+ repo_id=DATASET_ID,
863
+ repo_type="dataset",
864
+ path_in_repo=evaluation_path
865
+ )
866
+ logger.info(f"Deleted evaluation file from HF dataset: {evaluation_path}")
867
+ except Exception as e:
868
+ # It's okay if evaluation file doesn't exist
869
+ logger.debug(f"Could not delete evaluation file {evaluation_path}: {str(e)}")
870
+
871
+ return True, f"Deleted {len(matching_files)} file(s) from dataset for conversation: {conversation_id}"
872
+
873
+ except (RepositoryNotFoundError, RevisionNotFoundError) as e:
874
+ return False, f"Dataset or path not found: {str(e)}"
875
+
876
+ except Exception as e:
877
+ logger.error(f"Error deleting conversation from dataset: {str(e)}")
878
+ return False, f"Error deleting conversation from dataset: {str(e)}"
879
+
880
+
881
+ def delete_conversation(conversation_id, evaluator):
882
+ """
883
+ Delete conversation files by ID
884
+
885
+ Args:
886
+ conversation_id: ID of conversation to delete
887
+ evaluator: ChatEvaluator instance
888
+
889
+ Returns:
890
+ Message about deletion status
891
+ """
892
+ try:
893
+ if not conversation_id:
894
+ return "Error: No conversation ID provided"
895
+
896
+ # Get all chat files
897
+ chat_files = evaluator.get_chat_files()
898
+
899
+ # Find matching files (there might be multiple files with same conversation ID)
900
+ matching_files = []
901
+ for chat_file in chat_files:
902
+ filename = os.path.basename(chat_file)
903
+ if filename.startswith(f"{conversation_id}_"):
904
+ matching_files.append(chat_file)
905
+
906
+ if not matching_files:
907
+ return f"No files found for conversation ID: {conversation_id}"
908
+
909
+ # Delete all matching files
910
+ deleted_count = 0
911
+ for file_path in matching_files:
912
+ try:
913
+ os.remove(file_path)
914
+ deleted_count += 1
915
+ logger.info(f"Deleted chat file: {file_path}")
916
+ except Exception as e:
917
+ logger.error(f"Error deleting file {file_path}: {str(e)}")
918
+
919
+ # Check if there are evaluation files for this conversation
920
+ evaluation_path = os.path.join(
921
+ evaluator.annotations_dir,
922
+ f"evaluation_{conversation_id}.json"
923
+ )
924
+
925
+ if os.path.exists(evaluation_path):
926
+ try:
927
+ os.remove(evaluation_path)
928
+ logger.info(f"Deleted evaluation file: {evaluation_path}")
929
+ return f"Deleted {deleted_count} chat file(s) and evaluation for conversation: {conversation_id}"
930
+ except Exception as e:
931
+ logger.error(f"Error deleting evaluation file {evaluation_path}: {str(e)}")
932
+
933
+ return f"Deleted {deleted_count} chat file(s) for conversation: {conversation_id}"
934
+ except Exception as e:
935
+ logger.error(f"Error deleting conversation: {str(e)}")
936
+ return f"Error deleting conversation: {str(e)}"
937
+
938
+
939
+
940
+
941
 
942
  def initialize_app():
943
  """Initialize app with user preferences"""
 
1276
  )
1277
  qa_table = gr.Dataframe(
1278
  value=pd.DataFrame(
1279
+ columns=["Conversation ID", "Question", "Answer", "Evaluated"]
1280
  ),
1281
  interactive=True,
1282
  wrap=True,
 
1293
  interactive=True
1294
  )
1295
  load_btn = gr.Button("Load Conversation")
1296
+ delete_btn = gr.Button("Delete Conversation", variant="stop")
1297
+
1298
+ delete_status = gr.Textbox(label="Delete Status", interactive=False)
1299
 
1300
  # Conversation content section
1301
  gr.Markdown("### Evaluate Response")
 
1397
  inputs=[min_rating, export_path],
1398
  outputs=[export_status]
1399
  )
1400
+
1401
+ # Обработчик для удаления чата
1402
+ delete_btn.click(
1403
+ fn=delete_conversation,
1404
+ inputs=[selected_conversation, chat_evaluator], # Используем созданный ранее объект chat_evaluator
1405
+ outputs=[delete_status]
1406
+ )
1407
+
1408
+ # Обновление таблицы и статуса после удаления
1409
+ delete_btn.click(
1410
+ fn=lambda: get_evaluation_status(chat_evaluator, force_reload=True),
1411
+ inputs=[],
1412
+ outputs=[evaluation_status, qa_table, refresh_data_status]
1413
+ )
1414
 
1415
  # Model change handler - outside of Tabs but inside Blocks
1416
  model_selector.change(