|
|
|
|
|
from datasets import Dataset, Features, Value, Sequence
|
|
|
import json
|
|
|
import os
|
|
|
|
|
|
|
|
|
JSON_FILE_PATH = "Indian_CIVICS_Dataset.json"
|
|
|
OUTPUT_DIR = "indian_civics_dataset_hf"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DATASET_FEATURES = Features(
|
|
|
{
|
|
|
"ID": Value(dtype="string"),
|
|
|
"Topic": Value(dtype="string"),
|
|
|
"Sub-Topic": Value(dtype="string"),
|
|
|
"Statement": Value(dtype="string"),
|
|
|
"Statement - Translation": Value(dtype="string"),
|
|
|
"Data Source": Value(dtype="string"),
|
|
|
"Data Producer Organization": Value(dtype="string"),
|
|
|
"Organization Type": Value(dtype="string"),
|
|
|
"Language": Value(dtype="string"),
|
|
|
"State/Region": Value(dtype="string"),
|
|
|
"Link": Value(dtype="string"),
|
|
|
|
|
|
"Consensus Value Annotation": Sequence(feature=Value(dtype="string")),
|
|
|
}
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def load_data_from_json(file_path):
|
|
|
"""Loads a list of dictionaries from a JSON file."""
|
|
|
if not os.path.exists(file_path):
|
|
|
raise FileNotFoundError(f"Error: JSON file not found at {file_path}")
|
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
data_list = json.load(f)
|
|
|
return data_list
|
|
|
|
|
|
def convert_to_hf_format(data_list):
|
|
|
"""Converts a list of dictionaries into a dictionary of lists (HF format)."""
|
|
|
if not data_list:
|
|
|
return {}
|
|
|
|
|
|
|
|
|
keys = data_list[0].keys()
|
|
|
hf_data = {key: [] for key in keys}
|
|
|
|
|
|
|
|
|
for item in data_list:
|
|
|
for key in keys:
|
|
|
hf_data[key].append(item.get(key))
|
|
|
|
|
|
return hf_data
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
try:
|
|
|
print(f"Loading data from: {JSON_FILE_PATH}...")
|
|
|
|
|
|
|
|
|
statement_list = load_data_from_json(JSON_FILE_PATH)
|
|
|
print(f"✅ Loaded {len(statement_list)} entries.")
|
|
|
|
|
|
|
|
|
hf_sample_data = convert_to_hf_format(statement_list)
|
|
|
|
|
|
|
|
|
dataset = Dataset.from_dict(
|
|
|
hf_sample_data,
|
|
|
features=DATASET_FEATURES
|
|
|
)
|
|
|
|
|
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
dataset.save_to_disk(OUTPUT_DIR)
|
|
|
|
|
|
print("-" * 50)
|
|
|
print(f"✅ Dataset successfully created.")
|
|
|
print(f"Total examples: {len(dataset)}")
|
|
|
print(f"Dataset saved locally to: ./{OUTPUT_DIR}")
|
|
|
print("-" * 50)
|
|
|
print("--- Sample Example (Index 0) ---")
|
|
|
print(dataset[0])
|
|
|
print("-" * 50)
|
|
|
|
|
|
except FileNotFoundError as e:
|
|
|
print(f"\nFATAL ERROR: {e}")
|
|
|
print(f"Please make sure the file '{JSON_FILE_PATH}' is in the same directory as '{os.path.basename(__file__)}'.")
|
|
|
except Exception as e:
|
|
|
print(f"\nAn unexpected error occurred during dataset creation: {e}") |