AI_DETECTOR_SOTA / scripts /generate_synthetic.py
simonlesaumon's picture
Upload folder using huggingface_hub
eb72d30 verified
Raw
History Blame Contribute Delete
3.11 kB
import os
import sys
import yaml
import argparse
import pandas as pd
import numpy as np
import random
from datetime import datetime, timedelta
# Import text generator
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from text_generator import generate_speech, PARTIES, SPEAKERS, CHAMBERS
def load_config(config_path):
with open(config_path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
def generate_ai_corpus(sample_size, models, doc_types, seed=42):
"""Generates a synthetic AI dataset representing text produced by different LLMs."""
print(f"Generating synthetic AI corpus ({sample_size} samples) across models {models}...")
random.seed(seed + 1000)
np.random.seed(seed + 1000)
data = []
# AI speeches are typically newer (e.g. 2022-2026)
start_date = datetime(2022, 11, 30) # ChatGPT release date
end_date = datetime(2026, 5, 1)
date_range_days = (end_date - start_date).days
for i in range(sample_size):
date_speech = start_date + timedelta(days=random.randint(0, date_range_days))
model = random.choice(models)
doc_type = random.choice(doc_types)
speaker = random.choice(SPEAKERS)
party = random.choice(PARTIES)
chamber = random.choice(CHAMBERS)
# Calculate legislature (16th or higher)
leg = "16" if date_speech.year >= 2022 else "15"
# Simulating standard prompt injected for generation
prompt = f"Rédige un texte parlementaire en français de type '{doc_type}' sur un sujet de politique nationale. Style : {model}."
speech_text = generate_speech(is_ai=True, ai_model=model, doc_type=doc_type, seed=i + 10000)
# Clean text
speech_text = " ".join(speech_text.split())
data.append({
"text": speech_text,
"label_human_ai": 1,
"source": model,
"speaker": speaker,
"party": party,
"date": date_speech.strftime("%Y-%m-%d"),
"chamber": chamber,
"document_type": doc_type,
"legislature": leg,
"generation_prompt": prompt
})
return pd.DataFrame(data)
def main():
parser = argparse.ArgumentParser(description="Generate synthetic AI parliamentary corpus.")
parser.add_argument("--config", default="configs/config.yaml", help="Path to config file")
args = parser.parse_args()
config = load_config(args.config)
raw_dir = config["paths"]["raw_dir"]
os.makedirs(raw_dir, exist_ok=True)
sample_size = config["data_collection"]["sample_size_ai"]
models = config["synthetic_generation"]["models"]
doc_types = config["synthetic_generation"]["document_types"]
seed = config["data_collection"]["seed"]
df_ai = generate_ai_corpus(sample_size, models, doc_types, seed)
output_path = os.path.join(raw_dir, "ai_corpus.csv")
df_ai.to_csv(output_path, index=False)
print(f"Successfully generated and saved AI corpus to {output_path}")
if __name__ == "__main__":
main()