fai-adh commited on
Commit
b3a2163
·
verified ·
1 Parent(s): f709321

Upload translation_candidates.ipynb

Browse files
Files changed (1) hide show
  1. translation_candidates.ipynb +136 -0
translation_candidates.ipynb ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Fon–French benchmark — étape 1 : propositions de traduction\n",
8
+ "\n",
9
+ "Ce notebook génère des **propositions** français → Fon pour les 40 scénarios. Elles ne doivent pas être considérées comme la vérité terrain : elles doivent être relues et validées par une personne maîtrisant le Fon avant l'évaluation de Qwen.\n",
10
+ "\n",
11
+ "Modèle de traduction proposé : `nellaw/nllb-fongbe-french-translation`.\n"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": null,
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "!pip -q install -U transformers sentencepiece accelerate pandas torch\n"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "import pandas as pd\n",
30
+ "import torch\n",
31
+ "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
32
+ "\n",
33
+ "MODEL_NAME = 'nellaw/nllb-fongbe-french-translation'\n",
34
+ "\n",
35
+ "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
36
+ "print('Device:', device)\n",
37
+ "\n",
38
+ "tokenizer = AutoTokenizer.from_pretrained(\n",
39
+ " MODEL_NAME,\n",
40
+ " src_lang='fra_Latn',\n",
41
+ " tgt_lang='fon_Latn'\n",
42
+ ")\n",
43
+ "\n",
44
+ "model = AutoModelForSeq2SeqLM.from_pretrained(\n",
45
+ " MODEL_NAME,\n",
46
+ " torch_dtype=torch.float16 if device == 'cuda' else torch.float32\n",
47
+ ")\n",
48
+ "model.to(device)\n",
49
+ "model.eval()\n"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "INPUT_FILE = '/content/fon_code_switching_scenarios_40.csv'\n",
59
+ "df = pd.read_csv(INPUT_FILE)\n",
60
+ "\n",
61
+ "def translate_fr_to_fon(text):\n",
62
+ " inputs = tokenizer(\n",
63
+ " text,\n",
64
+ " return_tensors='pt',\n",
65
+ " max_length=256,\n",
66
+ " truncation=True\n",
67
+ " ).to(device)\n",
68
+ "\n",
69
+ " with torch.no_grad():\n",
70
+ " outputs = model.generate(\n",
71
+ " **inputs,\n",
72
+ " max_length=256,\n",
73
+ " num_beams=5,\n",
74
+ " forced_bos_token_id=tokenizer.convert_tokens_to_ids('fon_Latn')\n",
75
+ " )\n",
76
+ "\n",
77
+ " return tokenizer.decode(outputs[0], skip_special_tokens=True)\n"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": null,
83
+ "metadata": {},
84
+ "outputs": [],
85
+ "source": [
86
+ "df['fon_candidate'] = df['french_source'].apply(translate_fr_to_fon)\n",
87
+ "df['human_verified'] = False\n",
88
+ "df['verification_notes'] = ''\n",
89
+ "\n",
90
+ "df[['scenario_id', 'category', 'french_source', 'fon_candidate',\n",
91
+ " 'expected_answer', 'human_verified', 'verification_notes']].head(10)\n"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "markdown",
96
+ "metadata": {},
97
+ "source": [
98
+ "## Validation humaine obligatoire\n",
99
+ "\n",
100
+ "Pour chaque ligne, vérifier :\n",
101
+ "\n",
102
+ "1. le sens du français est conservé ;\n",
103
+ "2. les nombres, noms, relations et horaires sont conservés ;\n",
104
+ "3. la formulation Fon est naturelle et compréhensible ;\n",
105
+ "4. aucune information supplémentaire n'a été ajoutée ;\n",
106
+ "5. la réponse attendue reste la même.\n",
107
+ "\n",
108
+ "Mettre `human_verified = True` uniquement après vérification.\n"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "OUTPUT_FILE = '/content/fon_translation_candidates_40.csv'\n",
118
+ "df.to_csv(OUTPUT_FILE, index=False, encoding='utf-8-sig')\n",
119
+ "print('Saved:', OUTPUT_FILE)\n"
120
+ ]
121
+ }
122
+ ],
123
+ "metadata": {
124
+ "kernelspec": {
125
+ "display_name": "Python 3",
126
+ "language": "python",
127
+ "name": "python3"
128
+ },
129
+ "language_info": {
130
+ "name": "python",
131
+ "version": "3.x"
132
+ }
133
+ },
134
+ "nbformat": 4,
135
+ "nbformat_minor": 5
136
+ }