Max005/DeepfakeDetection
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 1,6 "metadata": {},7 "outputs": [8 {9 "name": "stdout",10 "output_type": "stream",11 "text": [12 "Collecting fastapi (from -r requirements.txt (line 1))\n",13 " Using cached fastapi-0.115.6-py3-none-any.whl.metadata (27 kB)\n",14 "Collecting uvicorn (from -r requirements.txt (line 2))\n",15 " Using cached uvicorn-0.34.0-py3-none-any.whl.metadata (6.5 kB)\n",16 "Collecting transformers (from -r requirements.txt (line 3))\n",17 " Using cached transformers-4.47.0-py3-none-any.whl.metadata (43 kB)\n",18 "Collecting torch (from -r requirements.txt (line 4))\n",19 " Using cached torch-2.5.1-cp312-cp312-win_amd64.whl.metadata (28 kB)\n",20 "Collecting torchvision (from -r requirements.txt (line 5))\n",21 " Using cached torchvision-0.20.1-cp312-cp312-win_amd64.whl.metadata (6.2 kB)\n",22 "Collecting torchaudio (from -r requirements.txt (line 6))\n",23 " Using cached torchaudio-2.5.1-cp312-cp312-win_amd64.whl.metadata (6.5 kB)\n",24 "Collecting PySoundFile (from -r requirements.txt (line 7))\n",25 " Using cached PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl.metadata (9.4 kB)\n",26 "Collecting ffmpeg-python (from -r requirements.txt (line 8))\n",27 " Using cached ffmpeg_python-0.2.0-py3-none-any.whl.metadata (1.7 kB)\n",28 "Collecting pydantic (from -r requirements.txt (line 9))\n",29 " Using cached pydantic-2.10.3-py3-none-any.whl.metadata (172 kB)\n",30 "Collecting numpy (from -r requirements.txt (line 10))\n",31 " Using cached numpy-2.2.0-cp312-cp312-win_amd64.whl.metadata (60 kB)\n",32 "Collecting python-multipart (from -r requirements.txt (line 11))\n",33 " Using cached python_multipart-0.0.19-py3-none-any.whl.metadata (1.8 kB)\n",34 "Collecting starlette<0.42.0,>=0.40.0 (from fastapi->-r requirements.txt (line 1))\n",35 " Using cached starlette-0.41.3-py3-none-any.whl.metadata (6.0 kB)\n",36 "Requirement already satisfied: typing-extensions>=4.8.0 in c:\\users\\asus\\anaconda3\\envs\\modelenv\\lib\\site-packages (from fastapi->-r requirements.txt (line 1)) (4.12.2)\n",37 "Collecting click>=7.0 (from uvicorn->-r requirements.txt (line 2))\n",38 " Using cached click-8.1.7-py3-none-any.whl.metadata (3.0 kB)\n",39 "Collecting h11>=0.8 (from uvicorn->-r requirements.txt (line 2))\n",40 " Using cached h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)\n",41 "Collecting filelock (from transformers->-r requirements.txt (line 3))\n",42 " Using cached filelock-3.16.1-py3-none-any.whl.metadata (2.9 kB)\n",43 "Collecting huggingface-hub<1.0,>=0.24.0 (from transformers->-r requirements.txt (line 3))\n",44 " Using cached huggingface_hub-0.27.0-py3-none-any.whl.metadata (13 kB)\n",45 "Requirement already satisfied: packaging>=20.0 in c:\\users\\asus\\anaconda3\\envs\\modelenv\\lib\\site-packages (from transformers->-r requirements.txt (line 3)) (24.2)\n",46 "Collecting pyyaml>=5.1 (from transformers->-r requirements.txt (line 3))\n",47 " Using cached PyYAML-6.0.2-cp312-cp312-win_amd64.whl.metadata (2.1 kB)\n",48 "Collecting regex!=2019.12.17 (from transformers->-r requirements.txt (line 3))\n",49 " Using cached regex-2024.11.6-cp312-cp312-win_amd64.whl.metadata (41 kB)\n",50 "Collecting requests (from transformers->-r requirements.txt (line 3))\n",51 " Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)\n",52 "Collecting tokenizers<0.22,>=0.21 (from transformers->-r requirements.txt (line 3))\n",53 " Using cached tokenizers-0.21.0-cp39-abi3-win_amd64.whl.metadata (6.9 kB)\n",54 "Collecting safetensors>=0.4.1 (from transformers->-r requirements.txt (line 3))\n",55 " Using cached safetensors-0.4.5-cp312-none-win_amd64.whl.metadata (3.9 kB)\n",56 "Collecting tqdm>=4.27 (from transformers->-r requirements.txt (line 3))\n",57 " Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n",58 "Collecting networkx (from torch->-r requirements.txt (line 4))\n",59 " Using cached networkx-3.4.2-py3-none-any.whl.metadata (6.3 kB)\n",60 "Collecting jinja2 (from torch->-r requirements.txt (line 4))\n",61 " Using cached jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB)\n",62 "Collecting fsspec (from torch->-r requirements.txt (line 4))\n",63 " Using cached fsspec-2024.10.0-py3-none-any.whl.metadata (11 kB)\n",64 "Requirement already satisfied: setuptools in c:\\users\\asus\\anaconda3\\envs\\modelenv\\lib\\site-packages (from torch->-r requirements.txt (line 4)) (75.6.0)\n",65 "Collecting sympy==1.13.1 (from torch->-r requirements.txt (line 4))\n",66 " Using cached sympy-1.13.1-py3-none-any.whl.metadata (12 kB)\n",67 "Collecting mpmath<1.4,>=1.1.0 (from sympy==1.13.1->torch->-r requirements.txt (line 4))\n",68 " Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\n",69 "Collecting pillow!=8.3.*,>=5.3.0 (from torchvision->-r requirements.txt (line 5))\n",70 " Downloading pillow-11.0.0-cp312-cp312-win_amd64.whl.metadata (9.3 kB)\n",71 "Collecting cffi>=0.6 (from PySoundFile->-r requirements.txt (line 7))\n",72 " Using cached cffi-1.17.1-cp312-cp312-win_amd64.whl.metadata (1.6 kB)\n",73 "Collecting future (from ffmpeg-python->-r requirements.txt (line 8))\n",74 " Using cached future-1.0.0-py3-none-any.whl.metadata (4.0 kB)\n",75 "Collecting annotated-types>=0.6.0 (from pydantic->-r requirements.txt (line 9))\n",76 " Using cached annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",77 "Collecting pydantic-core==2.27.1 (from pydantic->-r requirements.txt (line 9))\n",78 " Using cached pydantic_core-2.27.1-cp312-none-win_amd64.whl.metadata (6.7 kB)\n",79 "Collecting pycparser (from cffi>=0.6->PySoundFile->-r requirements.txt (line 7))\n",80 " Using cached pycparser-2.22-py3-none-any.whl.metadata (943 bytes)\n",81 "Requirement already satisfied: colorama in c:\\users\\asus\\anaconda3\\envs\\modelenv\\lib\\site-packages (from click>=7.0->uvicorn->-r requirements.txt (line 2)) (0.4.6)\n",82 "Collecting anyio<5,>=3.4.0 (from starlette<0.42.0,>=0.40.0->fastapi->-r requirements.txt (line 1))\n",83 " Using cached anyio-4.7.0-py3-none-any.whl.metadata (4.7 kB)\n",84 "Collecting MarkupSafe>=2.0 (from jinja2->torch->-r requirements.txt (line 4))\n",85 " Using cached MarkupSafe-3.0.2-cp312-cp312-win_amd64.whl.metadata (4.1 kB)\n",86 "Collecting charset-normalizer<4,>=2 (from requests->transformers->-r requirements.txt (line 3))\n",87 " Using cached charset_normalizer-3.4.0-cp312-cp312-win_amd64.whl.metadata (34 kB)\n",88 "Collecting idna<4,>=2.5 (from requests->transformers->-r requirements.txt (line 3))\n",89 " Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n",90 "Collecting urllib3<3,>=1.21.1 (from requests->transformers->-r requirements.txt (line 3))\n",91 " Using cached urllib3-2.2.3-py3-none-any.whl.metadata (6.5 kB)\n",92 "Collecting certifi>=2017.4.17 (from requests->transformers->-r requirements.txt (line 3))\n",93 " Using cached certifi-2024.12.14-py3-none-any.whl.metadata (2.3 kB)\n",94 "Collecting sniffio>=1.1 (from anyio<5,>=3.4.0->starlette<0.42.0,>=0.40.0->fastapi->-r requirements.txt (line 1))\n",95 " Using cached sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)\n",96 "Using cached fastapi-0.115.6-py3-none-any.whl (94 kB)\n",97 "Using cached uvicorn-0.34.0-py3-none-any.whl (62 kB)\n",98 "Using cached transformers-4.47.0-py3-none-any.whl (10.1 MB)\n",99 "Using cached torch-2.5.1-cp312-cp312-win_amd64.whl (203.0 MB)\n",100 "Using cached sympy-1.13.1-py3-none-any.whl (6.2 MB)\n",101 "Using cached torchvision-0.20.1-cp312-cp312-win_amd64.whl (1.6 MB)\n",102 "Using cached torchaudio-2.5.1-cp312-cp312-win_amd64.whl (2.4 MB)\n",103 "Using cached PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl (671 kB)\n",104 "Using cached ffmpeg_python-0.2.0-py3-none-any.whl (25 kB)\n",105 "Using cached pydantic-2.10.3-py3-none-any.whl (456 kB)\n",106 "Using cached pydantic_core-2.27.1-cp312-none-win_amd64.whl (2.0 MB)\n",107 "Using cached numpy-2.2.0-cp312-cp312-win_amd64.whl (12.6 MB)\n",108 "Using cached python_multipart-0.0.19-py3-none-any.whl (24 kB)\n",109 "Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",110 "Using cached cffi-1.17.1-cp312-cp312-win_amd64.whl (181 kB)\n",111 "Using cached click-8.1.7-py3-none-any.whl (97 kB)\n",112 "Using cached h11-0.14.0-py3-none-any.whl (58 kB)\n",113 "Using cached huggingface_hub-0.27.0-py3-none-any.whl (450 kB)\n",114 "Using cached fsspec-2024.10.0-py3-none-any.whl (179 kB)\n",115 "Downloading pillow-11.0.0-cp312-cp312-win_amd64.whl (2.6 MB)\n",116 " ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",117 " ---------------------------------------- 2.6/2.6 MB 21.2 MB/s eta 0:00:00\n",118 "Using cached PyYAML-6.0.2-cp312-cp312-win_amd64.whl (156 kB)\n",119 "Using cached regex-2024.11.6-cp312-cp312-win_amd64.whl (273 kB)\n",120 "Using cached safetensors-0.4.5-cp312-none-win_amd64.whl (286 kB)\n",121 "Using cached starlette-0.41.3-py3-none-any.whl (73 kB)\n",122 "Using cached tokenizers-0.21.0-cp39-abi3-win_amd64.whl (2.4 MB)\n",123 "Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)\n",124 "Using cached filelock-3.16.1-py3-none-any.whl (16 kB)\n",125 "Using cached future-1.0.0-py3-none-any.whl (491 kB)\n",126 "Using cached jinja2-3.1.4-py3-none-any.whl (133 kB)\n",127 "Using cached networkx-3.4.2-py3-none-any.whl (1.7 MB)\n",128 "Using cached requests-2.32.3-py3-none-any.whl (64 kB)\n",129 "Using cached anyio-4.7.0-py3-none-any.whl (93 kB)\n",130 "Using cached certifi-2024.12.14-py3-none-any.whl (164 kB)\n",131 "Using cached charset_normalizer-3.4.0-cp312-cp312-win_amd64.whl (102 kB)\n",132 "Using cached idna-3.10-py3-none-any.whl (70 kB)\n",133 "Using cached MarkupSafe-3.0.2-cp312-cp312-win_amd64.whl (15 kB)\n",134 "Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\n",135 "Using cached urllib3-2.2.3-py3-none-any.whl (126 kB)\n",136 "Using cached pycparser-2.22-py3-none-any.whl (117 kB)\n",137 "Using cached sniffio-1.3.1-py3-none-any.whl (10 kB)\n",138 "Installing collected packages: mpmath, urllib3, tqdm, sympy, sniffio, safetensors, regex, pyyaml, python-multipart, pydantic-core, pycparser, pillow, numpy, networkx, MarkupSafe, idna, h11, future, fsspec, filelock, click, charset-normalizer, certifi, annotated-types, uvicorn, requests, pydantic, jinja2, ffmpeg-python, cffi, anyio, torch, starlette, PySoundFile, huggingface-hub, torchvision, torchaudio, tokenizers, fastapi, transformers\n",139 "Successfully installed MarkupSafe-3.0.2 PySoundFile-0.9.0.post1 annotated-types-0.7.0 anyio-4.7.0 certifi-2024.12.14 cffi-1.17.1 charset-normalizer-3.4.0 click-8.1.7 fastapi-0.115.6 ffmpeg-python-0.2.0 filelock-3.16.1 fsspec-2024.10.0 future-1.0.0 h11-0.14.0 huggingface-hub-0.27.0 idna-3.10 jinja2-3.1.4 mpmath-1.3.0 networkx-3.4.2 numpy-2.2.0 pillow-11.0.0 pycparser-2.22 pydantic-2.10.3 pydantic-core-2.27.1 python-multipart-0.0.19 pyyaml-6.0.2 regex-2024.11.6 requests-2.32.3 safetensors-0.4.5 sniffio-1.3.1 starlette-0.41.3 sympy-1.13.1 tokenizers-0.21.0 torch-2.5.1 torchaudio-2.5.1 torchvision-0.20.1 tqdm-4.67.1 transformers-4.47.0 urllib3-2.2.3 uvicorn-0.34.0\n",140 "Note: you may need to restart the kernel to use updated packages.\n"141 ]142 }143 ],144 "source": [145 "pip install -r requirements.txt"146 ]147 },148 {149 "cell_type": "code",150 "execution_count": 2,151 "metadata": {},152 "outputs": [153 {154 "name": "stdout",155 "output_type": "stream",156 "text": [157 "['soundfile']\n"158 ]159 }160 ],161 "source": [162 "import torchaudio\n",163 "print(str(torchaudio.list_audio_backends()))"164 ]165 },166 {167 "cell_type": "code",168 "execution_count": 3,169 "metadata": {},170 "outputs": [171 {172 "name": "stdout",173 "output_type": "stream",174 "text": [175 "Note: you may need to restart the kernel to use updated packages.\n"176 ]177 }178 ],179 "source": [180 "pip list --format=freeze > requirements.txt"181 ]182 },183 {184 "cell_type": "code",185 "execution_count": 12,186 "metadata": {},187 "outputs": [188 {189 "name": "stderr",190 "output_type": "stream",191 "text": [192 "<>:13: SyntaxWarning: invalid escape sequence '\\m'\n",193 "<>:17: SyntaxWarning: invalid escape sequence '\\H'\n",194 "<>:13: SyntaxWarning: invalid escape sequence '\\m'\n",195 "<>:17: SyntaxWarning: invalid escape sequence '\\H'\n",196 "C:\\Users\\Asus\\AppData\\Local\\Temp\\ipykernel_18220\\208613059.py:13: SyntaxWarning: invalid escape sequence '\\m'\n",197 " model_path = \"Deepfake\\model\"\n",198 "C:\\Users\\Asus\\AppData\\Local\\Temp\\ipykernel_18220\\208613059.py:17: SyntaxWarning: invalid escape sequence '\\H'\n",199 " cache_dir=\"D:\\HuggingFace\",\n"200 ]201 }202 ],203 "source": [204 "from transformers import pipeline\n",205 "from transformers import AutoProcessor, AutoModelForAudioClassification\n",206 "from fastapi import FastAPI\n",207 "from pydantic import BaseModel\n",208 "import uvicorn\n",209 "import torchaudio\n",210 "import torch\n",211 "\n",212 "# Define the input schema\n",213 "class InputData(BaseModel):\n",214 " input: str\n",215 "\n",216 "model_path = \"Deepfake\\model\"\n",217 "processor = AutoProcessor.from_pretrained(model_path)\n",218 "# Instantiate the model\n",219 "model = AutoModelForAudioClassification.from_pretrained(pretrained_model_name_or_path=model_path,\n",220 " cache_dir=\"D:\\HuggingFace\",\n",221 " local_files_only=True,\n",222 " )\n"223 ]224 },225 {226 "cell_type": "markdown",227 "metadata": {},228 "source": [229 "Functions"230 ]231 },232 {233 "cell_type": "code",234 "execution_count": 29,235 "metadata": {},236 "outputs": [],237 "source": []238 },239 {240 "cell_type": "code",241 "execution_count": 6,242 "metadata": {},243 "outputs": [],244 "source": [245 "def prepare_audio(file_path, sampling_rate=16000, duration=10):\n",246 " \"\"\"\n",247 " Prepares audio by loading, resampling, and returning it in manageable chunks.\n",248 " \n",249 " Parameters:\n",250 " - file_path: Path to the audio file.\n",251 " - sampling_rate: Target sampling rate for the audio.\n",252 " - duration: Duration in seconds for each chunk.\n",253 " \n",254 " Returns:\n",255 " - A list of audio chunks, each as a numpy array.\n",256 " \"\"\"\n",257 " # Load and resample the audio file\n",258 " waveform, original_sampling_rate = torchaudio.load(file_path)\n",259 " \n",260 " # Convert stereo to mono if necessary\n",261 " if waveform.shape[0] > 1: # More than 1 channel\n",262 " waveform = torch.mean(waveform, dim=0, keepdim=True)\n",263 " \n",264 " # Resample if needed\n",265 " if original_sampling_rate != sampling_rate:\n",266 " resampler = torchaudio.transforms.Resample(orig_freq=original_sampling_rate, new_freq=sampling_rate)\n",267 " waveform = resampler(waveform)\n",268 " \n",269 " # Calculate chunk size in samples\n",270 " chunk_size = sampling_rate * duration\n",271 " audio_chunks = []\n",272 "\n",273 " # Split the audio into chunks\n",274 " for start in range(0, waveform.shape[1], chunk_size):\n",275 " chunk = waveform[:, start:start + chunk_size]\n",276 " \n",277 " # Pad the last chunk if it's shorter than the chunk size\n",278 " if chunk.shape[1] < chunk_size:\n",279 " padding = chunk_size - chunk.shape[1]\n",280 " chunk = torch.nn.functional.pad(chunk, (0, padding))\n",281 " \n",282 " audio_chunks.append(chunk.squeeze().numpy())\n",283 " \n",284 " return audio_chunks\n"285 ]286 },287 {288 "cell_type": "code",289 "execution_count": 14,290 "metadata": {},291 "outputs": [],292 "source": [293 "import torch.nn.functional as F\n",294 "\n",295 "def predict_audio(file_path):\n",296 " \"\"\"\n",297 " Predicts the class of an audio file by aggregating predictions from chunks and calculates confidence.\n",298 " \n",299 " Args:\n",300 " file_path (str): Path to the audio file.\n",301 "\n",302 " Returns:\n",303 " dict: Contains the predicted class label and average confidence score.\n",304 " \"\"\"\n",305 " # Prepare audio chunks\n",306 " audio_chunks = prepare_audio(file_path)\n",307 " predictions = []\n",308 " confidences = []\n",309 "\n",310 " for i, chunk in enumerate(audio_chunks):\n",311 " # Prepare input for the model\n",312 " inputs = processor(\n",313 " chunk, sampling_rate=16000, return_tensors=\"pt\", padding=True\n",314 " )\n",315 " \n",316 " # Perform inference\n",317 " with torch.no_grad():\n",318 " outputs = model(**inputs)\n",319 " logits = outputs.logits\n",320 " \n",321 " # Apply softmax to calculate probabilities\n",322 " probabilities = F.softmax(logits, dim=1)\n",323 " \n",324 " # Get the predicted class and its confidence\n",325 " confidence, predicted_class = torch.max(probabilities, dim=1)\n",326 " predictions.append(predicted_class.item())\n",327 " confidences.append(confidence.item())\n",328 " \n",329 " # Aggregate predictions (majority voting)\n",330 " aggregated_prediction_id = max(set(predictions), key=predictions.count)\n",331 " predicted_label = model.config.id2label[aggregated_prediction_id]\n",332 " \n",333 " # Calculate average confidence across chunks\n",334 " average_confidence = sum(confidences) / len(confidences)\n",335 "\n",336 " return {\n",337 " \"predicted_label\": predicted_label,\n",338 " \"average_confidence\": average_confidence\n",339 " }\n",340 "\n"341 ]342 },343 {344 "cell_type": "code",345 "execution_count": 15,346 "metadata": {},347 "outputs": [348 {349 "name": "stdout",350 "output_type": "stream",351 "text": [352 "Chunk shape: (160000,)\n",353 "Chunk shape: (160000,)\n",354 "Chunk shape: (160000,)\n",355 "Chunk shape: (160000,)\n",356 "Chunk shape: (160000,)\n",357 "Chunk shape: (160000,)\n",358 "Chunk shape: (160000,)\n",359 "Chunk shape: (160000,)\n",360 "Chunk shape: (160000,)\n",361 "Chunk shape: (160000,)\n",362 "Predicted Class: {'predicted_label': 'Real', 'average_confidence': 0.9984144032001495}\n"363 ]364 },365 {366 "ename": "",367 "evalue": "",368 "output_type": "error",369 "traceback": [370 "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",371 "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",372 "\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",373 "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."374 ]375 }376 ],377 "source": [378 "# Example: Test a single audio file\n",379 "file_path = r\"D:\\repos\\GODAM\\audioFiles\\test.wav\" # Replace with your audio file path\n",380 "predicted_class = predict_audio(file_path)\n",381 "print(f\"Predicted Class: {predicted_class}\")"382 ]383 }384 ],385 "metadata": {386 "kernelspec": {387 "display_name": "modelEnv",388 "language": "python",389 "name": "python3"390 },391 "language_info": {392 "codemirror_mode": {393 "name": "ipython",394 "version": 3395 },396 "file_extension": ".py",397 "mimetype": "text/x-python",398 "name": "python",399 "nbconvert_exporter": "python",400 "pygments_lexer": "ipython3",401 "version": "3.12.8"402 }403 },404 "nbformat": 4,405 "nbformat_minor": 2406}407 