Rulga commited on
Commit
a60f617
·
1 Parent(s): 29f1974

Enhance save_chat_history function to save chat history locally and upload to HuggingFace dataset

Browse files
Files changed (1) hide show
  1. app.py +39 -9
app.py CHANGED
@@ -3,7 +3,8 @@ import os
3
  import json
4
  import datetime
5
  from pathlib import Path
6
- from huggingface_hub import InferenceClient
 
7
  from config.constants import DEFAULT_SYSTEM_MESSAGE
8
  from config.settings import (
9
  HF_TOKEN,
@@ -393,7 +394,7 @@ def rebuild_kb():
393
  return f"Error creating knowledge base: {str(e)}"
394
 
395
  def save_chat_history(history, conversation_id):
396
- """Save chat history to a file"""
397
  try:
398
  # Create directory if it doesn't exist
399
  os.makedirs(CHAT_HISTORY_PATH, exist_ok=True)
@@ -414,15 +415,44 @@ def save_chat_history(history, conversation_id):
414
  filename = f"{conversation_id}_{timestamp}.json"
415
  filepath = os.path.join(CHAT_HISTORY_PATH, filename)
416
 
417
- # Save to file
 
 
 
 
 
 
 
418
  with open(filepath, 'w', encoding='utf-8') as f:
419
- json.dump({
420
- "conversation_id": conversation_id,
421
- "timestamp": datetime.datetime.now().isoformat(),
422
- "history": formatted_history
423
- }, f, ensure_ascii=False, indent=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
- print(f"Debug - Chat history saved to {filepath}")
426
  return True
427
  except Exception as e:
428
  print(f"Error saving chat history: {str(e)}")
 
3
  import json
4
  import datetime
5
  from pathlib import Path
6
+ from huggingface_hub import InferenceClient, HfApi
7
+ #from huggingface_hub import InferenceClient
8
  from config.constants import DEFAULT_SYSTEM_MESSAGE
9
  from config.settings import (
10
  HF_TOKEN,
 
394
  return f"Error creating knowledge base: {str(e)}"
395
 
396
  def save_chat_history(history, conversation_id):
397
+ """Save chat history to a file and to HuggingFace dataset"""
398
  try:
399
  # Create directory if it doesn't exist
400
  os.makedirs(CHAT_HISTORY_PATH, exist_ok=True)
 
415
  filename = f"{conversation_id}_{timestamp}.json"
416
  filepath = os.path.join(CHAT_HISTORY_PATH, filename)
417
 
418
+ # Create chat history data
419
+ chat_data = {
420
+ "conversation_id": conversation_id,
421
+ "timestamp": datetime.datetime.now().isoformat(),
422
+ "history": formatted_history
423
+ }
424
+
425
+ # Save to local file
426
  with open(filepath, 'w', encoding='utf-8') as f:
427
+ json.dump(chat_data, f, ensure_ascii=False, indent=2)
428
+
429
+ print(f"Debug - Chat history saved locally to {filepath}")
430
+
431
+ # Now upload to HuggingFace dataset
432
+ try:
433
+ from huggingface_hub import HfApi
434
+
435
+ # Initialize the Hugging Face API client
436
+ api = HfApi(token=HF_TOKEN)
437
+
438
+ # Target path in the dataset
439
+ dataset_chat_dir = "chat_histories"
440
+ target_path = f"{dataset_chat_dir}/{filename}"
441
+
442
+ # Upload the file to the dataset
443
+ api.upload_file(
444
+ path_or_fileobj=filepath,
445
+ path_in_repo=target_path,
446
+ repo_id=DATASET_ID,
447
+ repo_type="dataset"
448
+ )
449
+
450
+ print(f"Debug - Chat history uploaded to dataset at {target_path}")
451
+
452
+ except Exception as e:
453
+ print(f"Warning - Failed to upload chat history to dataset: {str(e)}")
454
+ # Continue execution even if upload fails
455
 
 
456
  return True
457
  except Exception as e:
458
  print(f"Error saving chat history: {str(e)}")