AI_DETECTOR_SOTA / scripts /text_generator.py
simonlesaumon's picture
Upload folder using huggingface_hub
eb72d30 verified
Raw
History Blame Contribute Delete
7.53 kB
import random
import numpy as np
# Vocabulary databases for political speech simulation
SUBJECTS = [
"le gouvernement", "l'opposition", "la commission", "notre groupe parlementaire",
"l'Assemblée nationale", "le Sénat", "le peuple français", "les citoyens de nos territoires",
"les contribuables", "la République", "l'État", "la nation", "notre pays",
"les forces vives de la nation", "les classes moyennes", "les services publics"
]
VERBS = [
"proposer", "soutenir", "contester", "adopter", "rejeter", "renforcer", "affaiblir",
"défendre", "débattre", "voter", "garantir", "investir dans", "réformer", "promouvoir",
"dénoncer", "saluer", "déplorer", "interpeller", "exiger", "accompagner"
]
CONCEPTS = [
"la transition écologique", "le pouvoir d'achat", "la sécurité nationale", "la justice sociale",
"le budget de l'État", "la réforme des retraites", "les services publics de proximité",
"l'éducation nationale", "le système de santé", "la souveraineté industrielle et énergétique",
"l'attractivité économique", "la simplification administrative", "la sécurité des biens et des personnes",
"l'égalité des chances", "la cohésion sociale et territoriale", "la dette publique"
]
HUMAN_SPEECH_MARKERS = [
"Mes chers collègues,", "Monsieur le président,", "Madame la ministre,", "à vrai dire,",
"vous le savez bien,", "c'est pourquoi,", "et c'est tant mieux,", "soyons clairs,",
"c'est inadmissible !", "je crois profondément que", "permettez-moi de le dire,",
"sur le terrain,", "nos concitoyens nous le disent,", "c'est une question de bon sens.",
"Il faut arrêter de tourner autour du pot.", "Qu'il me soit permis de rappeler..."
]
AI_CONNECTORS = {
"gpt-4": [
"Tout d'abord,", "En outre,", "De surcroît,", "Par conséquent,", "En conclusion,",
"Il est crucial de souligner que", "Il convient de noter que", "Par ailleurs,", "Ainsi,"
],
"claude-3-opus": [
"Néanmoins,", "Certes,", "Il est important de souligner que", "Dans cette perspective,",
"Toutefois,", "En effet,", "D'une part,", "D'autre part,", "En somme,"
],
"qwen-72b": [
"En premier lieu,", "Deuxièmement,", "De plus,", "C'est pourquoi,", "Enfin,",
"Il faut mentionner que", "Concernant ce point,", "De cette manière,", "En somme,"
],
"gemma-7b": [
"D'abord,", "Aussi,", "Cependant,", "En fait,", "Donc,",
"Pour résumer,", "À cet égard,", "Finalement,", "Pour conclure,"
]
}
HUMAN_INTERRUPTIONS = [
" (Exclamations sur plusieurs bancs)",
" (Applaudissements sur les bancs de la majorité)",
" (Mouvements divers)",
" (Protestations)",
" !", " ?", "..."
]
PARTIES = ["Renaissance", "Rassemblement National", "La France Insoumise", "Les Républicains", "Socialistes", "Écologistes", "Démocrates"]
SPEAKERS = [
"Jean Dupuis", "Marine Lepetit", "Mathilde Legrand", "Laurent Wauquiez", "Olivier Faure",
"Clémentine Autain", "Gérald Darmanin", "Yaël Braun-Pivet", "Bruno Le Maire", "Éric Ciotti",
"Marine Le Pen", "Jean-Luc Mélenchon", "François Ruffin", "Valérie Pécresse"
]
CHAMBERS = ["Assemblée nationale", "Sénat"]
def generate_sentence(is_ai=False, ai_model="gpt-4", word_limit=None):
"""Generates a pseudo-political sentence in French."""
subj = random.choice(SUBJECTS)
verb = random.choice(VERBS)
concept = random.choice(CONCEPTS)
# Structure choices
struct = random.randint(1, 4)
if struct == 1:
sentence = f"{subj} doit {verb} {concept}."
elif struct == 2:
sentence = f"Il convient de {verb} {concept} afin de soutenir {random.choice(SUBJECTS)}."
elif struct == 3:
sentence = f"Quand {subj} décide de {verb} {concept}, c'est l'ensemble de la nation qui réagit."
else:
sentence = f"Nous devons {verb} {concept} car c'est une priorité pour {subj}."
sentence = sentence.capitalize()
if is_ai:
# AI sentences are structured, formal, and use AI connectors
if random.random() < 0.35:
connector = random.choice(AI_CONNECTORS.get(ai_model, AI_CONNECTORS["gpt-4"]))
sentence = f"{connector} {sentence[0].lower()}{sentence[1:]}"
# AI sentences are highly uniform in length, avoiding conversational items
else:
# Human sentences are conversational, have interjections, or are very winded
if random.random() < 0.25:
marker = random.choice(HUMAN_SPEECH_MARKERS)
sentence = f"{marker} {sentence[0].lower()}{sentence[1:]}"
if random.random() < 0.05:
# Human speech interruption or short sentence
sentence = random.choice(["C'est tout à fait exact !", "Nous ne pouvons l'accepter.", "C'est un scandale !", "Pourquoi un tel acharnement ?"])
return sentence
def generate_speech(is_ai=False, ai_model="gpt-4", doc_type="intervention_seance", seed=None):
"""Generates a full simulated political speech with distinct statistical features."""
if seed is not None:
random.seed(seed)
np.random.seed(seed)
paragraphs = []
if is_ai:
# AI text: 3 to 5 paragraphs, very structured
num_paragraphs = random.randint(3, 5)
# Sentence length distributions for AI: Normal distribution with low variance
# Mean word length is standard (around 16-20), low std dev
for i in range(num_paragraphs):
num_sentences = random.randint(2, 4)
p_sentences = []
for j in range(num_sentences):
sent = generate_sentence(is_ai=True, ai_model=ai_model)
p_sentences.append(sent)
paragraphs.append(" ".join(p_sentences))
# Add typical AI formatting, e.g. bullet points or clean markdown sections
if doc_type in ["argumentaire_legislatif", "amendement"] and random.random() < 0.4:
bullet_points = [
f"- En premier lieu, {generate_sentence(is_ai=True, ai_model=ai_model, word_limit=10).lower()}",
f"- En second lieu, {generate_sentence(is_ai=True, ai_model=ai_model, word_limit=10).lower()}",
f"- Enfin, {generate_sentence(is_ai=True, ai_model=ai_model, word_limit=10).lower()}"
]
paragraphs.insert(len(paragraphs)//2, "\n".join(bullet_points))
text = "\n\n".join(paragraphs)
else:
# Human text: Variable length, high variance in sentence length
# Sometime very long paragraphs, sometimes short
num_paragraphs = random.randint(2, 6)
for i in range(num_paragraphs):
# Let's decide if this is a block of text or a short reaction
if random.random() < 0.15:
paragraphs.append(random.choice(["(Applaudissements sur les bancs du groupe.)", "C'est exact !", "Très bien !"]))
continue
num_sentences = random.randint(2, 6)
p_sentences = []
for j in range(num_sentences):
sent = generate_sentence(is_ai=False)
# Maybe append standard parliamentary noise inside sentence
if random.random() < 0.04:
sent += random.choice(HUMAN_INTERRUPTIONS)
p_sentences.append(sent)
paragraphs.append(" ".join(p_sentences))
text = "\n\n".join(paragraphs)
return text