Spaces:
Sleeping
Sleeping
File size: 5,580 Bytes
cb1153d 805da6a cb1153d 805da6a cb1153d f382593 cb1153d 805da6a cb1153d f382593 cb1153d 3d58904 cb1153d 805da6a cb1153d 805da6a f382593 cb1153d 805da6a f382593 cb1153d f382593 cb1153d d2234c1 cb1153d f382593 cb1153d 805da6a f382593 805da6a f382593 d2234c1 f382593 d2234c1 cb1153d 3d58904 cb1153d f382593 cb1153d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd \n",
"import requests \n",
"import datetime as dt\n",
"import re\n",
"import json\n",
"from tqdm import tqdm\n",
"import os\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"if \"OPENAI_API_KEY\" not in os.environ:\n",
" with open('secrets/keys.txt', 'r') as f:\n",
" keys = json.loads(f.read())\n",
"else : \n",
" keys=os.environ"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"save_path = 'save'\n",
"content_path = 'extract_sciences_po'\n",
"\n",
"\n",
"def retrieve_classifications(name, mapping_prompt):\n",
"\n",
" df = pd.read_csv('extract_sciences_po.csv')\n",
"\n",
"\n",
" if os.path.exists(f\"{save_path}/output_{name}.txt\"):\n",
" with open(f\"{save_path}/output_{name}.txt\", 'r') as f : \n",
" out_dict = json.loads(f.read())\n",
" out_df = pd.DataFrame.from_dict(out_dict)\n",
" out = out_dict\n",
" else : \n",
" out_df = pd.DataFrame(columns = ['item_id', 'categorie_principale', 'categorie_secondaire'])\n",
" out = []\n",
"\n",
" df_to_process = df.loc[~df.item_id.isin(out_df.item_id)]\n",
"\n",
" if mapping_prompt[name]['client']=='deepseek':\n",
" client = OpenAI(api_key=keys[\"DEEPSEEK_API_KEY\"], base_url=\"https://api.deepseek.com\")\n",
" model=\"deepseek-chat\"\n",
" else:\n",
" client=OpenAI(api_key=keys['OPENAI_API_KEY'])\n",
" model=\"gpt-4o\"\n",
"\n",
" df_to_process = df.loc[~df.item_id.isin(out_df.item_id)]\n",
"\n",
"\n",
" with open(mapping_prompt[name]['path_prompt'], 'r') as f:\n",
" prompt = f.read()\n",
"\n",
" with tqdm(total=df_to_process.shape[0]) as pbar:\n",
" for i, row in df_to_process.iterrows():\n",
" titre_brut = f\"{row.item_id}_\"+row.titre.lower().strip().replace(f\"\\xa0\", ' ').replace(' : ', ':').replace(' ', '_').replace('/', '')\n",
" \n",
" with open(f'{content_path}/{titre_brut}.txt', 'r') as f:\n",
" text = f.read()\n",
"\n",
" messages = [{\"role\": \"system\", \"content\": prompt},\n",
" {\"role\": \"user\", \"content\": text}]\n",
"\n",
" response = client.chat.completions.create(\n",
" model=model,\n",
" messages=messages,\n",
" response_format={\n",
" 'type': 'json_object'\n",
" }\n",
" )\n",
" try : \n",
" cat_json = json.loads(response.choices[0].message.content)\n",
"\n",
" out.append({\n",
" 'item_id':row.item_id, \n",
" 'categorie_principale': cat_json['categorie_principale'],\n",
" 'categorie_secondaire': cat_json['categorie_secondaire'],\n",
" })\n",
" \n",
" with open(f'{save_path}/output_{name}.txt', 'w+') as f : \n",
" f.write(json.dumps(out))\n",
"\n",
" except Exception as e : \n",
" print(f'Error with article {row.item_id}')\n",
" pass\n",
"\n",
" \n",
" pbar.update(1)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sans_titre_1\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"0it [00:00, ?it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"0it [00:00, ?it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"favarel_et_al\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 21%|██▏ | 41/191 [05:28<16:54, 6.76s/it]"
]
}
],
"source": [
"with open('mapping_prompts.txt', 'r') as f : \n",
" mapping = json.loads(f.read())\n",
"\n",
"for name in mapping.keys():\n",
" print(name)\n",
" retrieve_classifications(name, mapping)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Ajouter images"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|