french-classic-conversations / convert_to_jsonl.py
Volko76's picture
Added to jsonl
ee6fa55 verified
"""
Convert HuggingFace dataset Volko76/french-classic-conversations to JSONL format
with messages structure including system prompt.
"""
from datasets import load_dataset
import json
import os
# Configuration
SYSTEM_MESSAGE = "You are a helpful assistant."
OUTPUT_FILE = "french_classic_conversations.jsonl"
def main():
# Load the dataset from HuggingFace
print("Loading dataset from HuggingFace...")
dataset = load_dataset("Volko76/french-classic-conversations")
print(f"Dataset loaded: {len(dataset['train'])} rows")
print(f"Columns: {dataset['train'].column_names}")
# Check the structure of the first row
sample = dataset['train'][0]
print("\nSample row structure:")
print(str(sample)[:1000])
# Convert to JSONL format with system message
print(f"\nConverting to JSONL format...")
with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
for row in dataset['train']:
# Get the conversations from the row - it might be a JSON string
conversations = row['conversations']
if isinstance(conversations, str):
conversations = json.loads(conversations)
# Create the messages list with system prompt first
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
# Add the conversation messages
for msg in conversations:
messages.append({
"role": msg['role'],
"content": msg['content']
})
# Write as JSONL
json_line = json.dumps({"messages": messages}, ensure_ascii=False)
f.write(json_line + '\n')
print(f"Conversion complete! Output saved to: {OUTPUT_FILE}")
# Verify the output - read and display first few lines
print("\n" + "="*60)
print("First 2 entries from the output file:\n")
with open(OUTPUT_FILE, 'r', encoding='utf-8') as f:
for i, line in enumerate(f):
if i >= 2:
break
data = json.loads(line)
print(f"Entry {i+1}:")
print(json.dumps(data, indent=2, ensure_ascii=False)[:1000])
print("\n" + "-"*40 + "\n")
# Count total entries and file size
with open(OUTPUT_FILE, 'r', encoding='utf-8') as f:
total_lines = sum(1 for _ in f)
file_size = os.path.getsize(OUTPUT_FILE) / (1024 * 1024) # MB
print("="*60)
print(f"Total conversations: {total_lines}")
print(f"File size: {file_size:.2f} MB")
if __name__ == "__main__":
main()