Max005/DeepfakeDetection
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 4,6 "metadata": {},7 "outputs": [],8 "source": [9 "import requests\n",10 "\n",11 "def infer_text(api_url, input_text):\n",12 " url = f\"{api_url}/infer\"\n",13 " try:\n",14 " # Send the input as a JSON object\n",15 " response = requests.post(url, json={\"input\": input_text})\n",16 " response.raise_for_status()\n",17 " return response.json()\n",18 " except requests.exceptions.RequestException as e:\n",19 " print(f\"Error during API call: {e}\")\n",20 " return None\n",21 "\n",22 "def check_health(api_url):\n",23 " url = f\"{api_url}/health\"\n",24 " try:\n",25 " response = requests.get(url)\n",26 " response.raise_for_status()\n",27 " return response.json()\n",28 " except requests.exceptions.RequestException as e:\n",29 " print(f\"Error during API health check: {e}\")\n",30 " return None"31 ]32 },33 {34 "cell_type": "code",35 "execution_count": 5,36 "metadata": {},37 "outputs": [38 {39 "name": "stdout",40 "output_type": "stream",41 "text": [42 "API Health Check: {'message': 'ok'}\n",43 "Predictions: [{'label': 'LABEL_0', 'score': 0.9927427768707275}]\n"44 ]45 }46 ],47 "source": [48 "api_url = \"http://localhost:8000\"\n",49 "\n",50 "# Check the API health status\n",51 "health_status = check_health(api_url)\n",52 "if health_status:\n",53 " print(\"API Health Check:\", health_status)\n",54 "else:\n",55 " print(\"Failed to connect to the API.\")\n",56 "\n",57 "# Example input text\n",58 "input_text = \"Congratulations! You've won a prize. Click the link to claim your reward.\"\n",59 "\n",60 "# Call the /infer endpoint\n",61 "predictions = infer_text(api_url, input_text)\n",62 "if predictions:\n",63 " print(\"Predictions:\", predictions)\n",64 "else:\n",65 " print(\"Failed to get predictions from the API.\")"66 ]67 },68 {69 "cell_type": "markdown",70 "metadata": {},71 "source": [72 "DeepFakeModel Test"73 ]74 },75 {76 "cell_type": "code",77 "execution_count": 4,78 "metadata": {},79 "outputs": [80 {81 "name": "stdout",82 "output_type": "stream",83 "text": [84 "Response JSON: {'predicted_label': 'Real', 'average_confidence': 0.9984144032001495}\n"85 ]86 }87 ],88 "source": [89 "import requests\n",90 "\n",91 "# Define the API endpoint\n",92 "url = \"http://127.0.0.1:8000/infer\"\n",93 "\n",94 "# Path to the audio file you want to test\n",95 "file_path = r\"D:\\repos\\GODAM\\audioFiles\\test.wav\" # Replace with the path to your audio file\n",96 "\n",97 "# Open the file in binary mode\n",98 "with open(file_path, \"rb\") as audio_file:\n",99 " # Prepare the file payload\n",100 " files = {\"file\": (\"audio.wav\", audio_file, \"audio/wav\")}\n",101 " \n",102 " # Send the POST request\n",103 " response = requests.post(url, files=files)\n",104 "\n",105 "# Print the response from the API\n",106 "if response.status_code == 200:\n",107 " print(\"Response JSON:\", response.json())\n",108 "else:\n",109 " print(f\"Error {response.status_code}: {response.text}\")"110 ]111 }112 ],113 "metadata": {114 "kernelspec": {115 "display_name": "base",116 "language": "python",117 "name": "python3"118 },119 "language_info": {120 "codemirror_mode": {121 "name": "ipython",122 "version": 3123 },124 "file_extension": ".py",125 "mimetype": "text/x-python",126 "name": "python",127 "nbconvert_exporter": "python",128 "pygments_lexer": "ipython3",129 "version": "3.12.7"130 }131 },132 "nbformat": 4,133 "nbformat_minor": 2134}135 