Spaces:
Runtime error
Runtime error
File size: 3,704 Bytes
151ed35 | 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 | {
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"def infer_text(api_url, input_text):\n",
" url = f\"{api_url}/infer\"\n",
" try:\n",
" # Send the input as a JSON object\n",
" response = requests.post(url, json={\"input\": input_text})\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except requests.exceptions.RequestException as e:\n",
" print(f\"Error during API call: {e}\")\n",
" return None\n",
"\n",
"def check_health(api_url):\n",
" url = f\"{api_url}/health\"\n",
" try:\n",
" response = requests.get(url)\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except requests.exceptions.RequestException as e:\n",
" print(f\"Error during API health check: {e}\")\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API Health Check: {'message': 'ok'}\n",
"Predictions: [{'label': 'LABEL_0', 'score': 0.9927427768707275}]\n"
]
}
],
"source": [
"api_url = \"http://localhost:8000\"\n",
"\n",
"# Check the API health status\n",
"health_status = check_health(api_url)\n",
"if health_status:\n",
" print(\"API Health Check:\", health_status)\n",
"else:\n",
" print(\"Failed to connect to the API.\")\n",
"\n",
"# Example input text\n",
"input_text = \"Congratulations! You've won a prize. Click the link to claim your reward.\"\n",
"\n",
"# Call the /infer endpoint\n",
"predictions = infer_text(api_url, input_text)\n",
"if predictions:\n",
" print(\"Predictions:\", predictions)\n",
"else:\n",
" print(\"Failed to get predictions from the API.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DeepFakeModel Test"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response JSON: {'predicted_label': 'Real', 'average_confidence': 0.9984144032001495}\n"
]
}
],
"source": [
"import requests\n",
"\n",
"# Define the API endpoint\n",
"url = \"http://127.0.0.1:8000/infer\"\n",
"\n",
"# Path to the audio file you want to test\n",
"file_path = r\"D:\\repos\\GODAM\\audioFiles\\test.wav\" # Replace with the path to your audio file\n",
"\n",
"# Open the file in binary mode\n",
"with open(file_path, \"rb\") as audio_file:\n",
" # Prepare the file payload\n",
" files = {\"file\": (\"audio.wav\", audio_file, \"audio/wav\")}\n",
" \n",
" # Send the POST request\n",
" response = requests.post(url, files=files)\n",
"\n",
"# Print the response from the API\n",
"if response.status_code == 200:\n",
" print(\"Response JSON:\", response.json())\n",
"else:\n",
" print(f\"Error {response.status_code}: {response.text}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|