Laura Wagner commited on
Commit ·
6f079a5
1
Parent(s): 81d2b02
added deepfake analysis script
Browse files- plots/mivolo.svg +0 -0
- scripts/Section_3-3-4_deepfakes.ipynb +124 -0
plots/mivolo.svg
CHANGED
|
|
|
|
scripts/Section_3-3-4_deepfakes.ipynb
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "a58d08a9",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# LLM annotation of Deepfake adapter dataset"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "markdown",
|
| 13 |
+
"id": "38c3a6a6",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"## Step 1 NER"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": null,
|
| 22 |
+
"id": "ad478ed2",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [],
|
| 25 |
+
"source": [
|
| 26 |
+
"import pandas as pd\n",
|
| 27 |
+
"from pathlib import Path\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"# Set up paths\n",
|
| 30 |
+
"current_dir = Path.cwd()\n",
|
| 31 |
+
"countries = current_dir.parent / \"misc/lists/countries.csv\"\n",
|
| 32 |
+
"professions = current_dir.parent / \"misc/lists/professions.csv\"\n",
|
| 33 |
+
"inputNER = current_dir.parent / \"data/CSV/NER_POI_step01_pre_annotation.csv\"\n",
|
| 34 |
+
"outfile = current_dir.parent / \"data/CSV/NER_POI_step02_annotated.csv\"\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"# Load datasets\n",
|
| 37 |
+
"poi_df = pd.read_csv(inputNER)\n",
|
| 38 |
+
"countries_df = pd.read_csv(countries)\n",
|
| 39 |
+
"professions_df = pd.read_csv(professions)\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# Step 1: Combine tags into one lowercase list\n",
|
| 42 |
+
"def combine_tags(row):\n",
|
| 43 |
+
" return [str(row[f\"tag_{i}\"]).strip().lower() for i in range(1, 8) if pd.notna(row.get(f\"tag_{i}\"))]\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"poi_df[\"tags\"] = poi_df.apply(combine_tags, axis=1)\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Step 2: Build tag → (country, nationality) mapping\n",
|
| 48 |
+
"tag_to_country_nationality = {}\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"for _, row in countries_df.iterrows():\n",
|
| 51 |
+
" country = str(row[\"en_short_name\"]).strip()\n",
|
| 52 |
+
" nationality = str(row[\"nationality\"]).strip()\n",
|
| 53 |
+
"\n",
|
| 54 |
+
" country_lc = country.lower()\n",
|
| 55 |
+
" nationality_lc = nationality.lower()\n",
|
| 56 |
+
"\n",
|
| 57 |
+
" # Add variations to the mapping\n",
|
| 58 |
+
" tag_to_country_nationality[country_lc] = (country, \"\")\n",
|
| 59 |
+
" tag_to_country_nationality[nationality_lc] = (\"\", nationality)\n",
|
| 60 |
+
" tag_to_country_nationality[country_lc.replace(\" \", \"\")] = (country, \"\")\n",
|
| 61 |
+
" tag_to_country_nationality[nationality_lc.replace(\" \", \"\")] = (\"\", nationality)\n",
|
| 62 |
+
"\n",
|
| 63 |
+
" for part in country_lc.split():\n",
|
| 64 |
+
" tag_to_country_nationality[part] = (country, \"\")\n",
|
| 65 |
+
" for part in nationality_lc.split():\n",
|
| 66 |
+
" tag_to_country_nationality[part] = (\"\", nationality)\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"# Step 3: Infer likely_country and likely_nationality\n",
|
| 69 |
+
"def infer_country_and_nationality(tags):\n",
|
| 70 |
+
" for tag in tags:\n",
|
| 71 |
+
" cleaned = tag.replace(\" \", \"\").lower()\n",
|
| 72 |
+
" if cleaned in tag_to_country_nationality:\n",
|
| 73 |
+
" country, nationality = tag_to_country_nationality[cleaned]\n",
|
| 74 |
+
" return pd.Series([country, nationality])\n",
|
| 75 |
+
" return pd.Series([\"\", \"\"])\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"poi_df[[\"likely_country\", \"likely_nationality\"]] = poi_df[\"tags\"].apply(infer_country_and_nationality)\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"# Step 4: Build tag → profession mapping\n",
|
| 80 |
+
"profession_alias_map = {}\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"for _, row in professions_df.iterrows():\n",
|
| 83 |
+
" canonical = str(row['profession']).strip().lower()\n",
|
| 84 |
+
" profession_alias_map[canonical] = canonical\n",
|
| 85 |
+
" for alias_col in ['alias_1', 'alias_2', 'alias_3']:\n",
|
| 86 |
+
" alias = row.get(alias_col)\n",
|
| 87 |
+
" if pd.notna(alias):\n",
|
| 88 |
+
" profession_alias_map[str(alias).strip().lower()] = canonical\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"# Step 5: Infer likely profession from tags\n",
|
| 91 |
+
"def infer_profession_from_tags(tags):\n",
|
| 92 |
+
" matched = []\n",
|
| 93 |
+
" for tag in tags:\n",
|
| 94 |
+
" cleaned = tag.strip().lower()\n",
|
| 95 |
+
" if cleaned in profession_alias_map:\n",
|
| 96 |
+
" matched.append(profession_alias_map[cleaned])\n",
|
| 97 |
+
"\n",
|
| 98 |
+
" if not matched:\n",
|
| 99 |
+
" return \"\"\n",
|
| 100 |
+
" if \"celebrity\" in matched and len(set(matched)) > 1:\n",
|
| 101 |
+
" # Drop 'celebrity' if other professions are present\n",
|
| 102 |
+
" matched = [m for m in matched if m != \"celebrity\"]\n",
|
| 103 |
+
"\n",
|
| 104 |
+
" return matched[0] # Return the first specific match\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"poi_df[\"likely_profession\"] = poi_df[\"tags\"].apply(infer_profession_from_tags)\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"# Step 6: Save enriched dataset\n",
|
| 110 |
+
"poi_df.to_csv(outfile, index=False)\n",
|
| 111 |
+
"\n",
|
| 112 |
+
"# Optional: Preview\n",
|
| 113 |
+
"print(poi_df[[\"real_name\", \"likely_country\", \"likely_nationality\", \"likely_profession\"]].head())\n"
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
],
|
| 117 |
+
"metadata": {
|
| 118 |
+
"language_info": {
|
| 119 |
+
"name": "python"
|
| 120 |
+
}
|
| 121 |
+
},
|
| 122 |
+
"nbformat": 4,
|
| 123 |
+
"nbformat_minor": 5
|
| 124 |
+
}
|