wt002 commited on
Commit
a62e933
·
verified ·
1 Parent(s): 59d0991

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +17 -12
agent.py CHANGED
@@ -398,19 +398,24 @@ def create_documents(data_source: str, data: list) -> list:
398
  return docs
399
 
400
  # Path to your data.json
401
- file_path = "/home/wendy/my_hf_agent_course_projects/src/data.json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
 
403
- # Check if the file exists
404
- if os.path.exists(file_path):
405
- # Load the data from the JSON file
406
- with open(file_path, "r", encoding="utf-8") as f:
407
- data = json.load(f)
408
-
409
- # Create documents from the loaded data
410
- docs = create_documents("json", data)
411
- print(f"Documents created: {len(docs)}")
412
- else:
413
- print(f"Error: File {file_path} not found.")
414
 
415
 
416
 
 
398
  return docs
399
 
400
  # Path to your data.json
401
+ file_path = "/home/wendy/Downloads/questions.json"
402
+
403
+ def load_data(file_path: str) -> List[Dict]:
404
+ """Safe JSON data loading with error handling"""
405
+ if not os.path.exists(file_path):
406
+ raise FileNotFoundError(f"Data file not found: {file_path}")
407
+
408
+ if not file_path.endswith('.json'):
409
+ raise ValueError("Invalid file format. Only JSON files supported")
410
+
411
+ try:
412
+ with open(file_path, "r", encoding="utf-8") as f:
413
+ return json.load(f)
414
+ except json.JSONDecodeError:
415
+ raise ValueError("Invalid JSON format in data file")
416
+ except Exception as e:
417
+ raise RuntimeError(f"Error loading data: {str(e)}")
418
 
 
 
 
 
 
 
 
 
 
 
 
419
 
420
 
421