-
Notifications
You must be signed in to change notification settings - Fork 44.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blocks/nvidia): Add Nvidia deepfake detection block (#9213)
Adding a block to allow users to detect deepfakes in their workflows. This block takes in an image as input and returns the probability of it being a deepfake as well as the bounding boxes around the image. ### Changes 🏗️ - Added NvidiaDeepfakeDetectBlock - Added the ability to upload images on the frontend - Added the ability to render base64 encoded images on the frontend <img width="1001" alt="Screenshot 2025-01-07 at 2 16 42 PM" src="https://github.com/user-attachments/assets/c3d090f3-3981-4235-a66b-f8e2a3920a4d" /> ### Checklist 📋 #### For code changes: - [ ] I have clearly listed my changes in the PR description - [ ] I have made a test plan - [ ] I have tested my changes according to the test plan: <!-- Put your test plan here: --> - [ ] ... <details> <summary>Example test plan</summary> - [ ] Create from scratch and execute an agent with at least 3 blocks - [ ] Import an agent from file upload, and confirm it executes correctly - [ ] Upload agent to marketplace - [ ] Import an agent from marketplace and confirm it executes correctly - [ ] Edit an agent from monitor, and confirm it executes correctly </details> #### For configuration changes: - [ ] `.env.example` is updated or already compatible with my changes - [ ] `docker-compose.yml` is updated or already compatible with my changes - [ ] I have included a list of my configuration changes in the PR description (under **Changes**) <details> <summary>Examples of configuration changes</summary> - Changing ports - Adding new services that need to communicate with each other - Secrets or environment variable changes - New or infrastructure changes such as databases </details> --------- Co-authored-by: Nicholas Tindle <[email protected]>
- Loading branch information
1 parent
4115f65
commit b558cca
Showing
10 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Literal | ||
|
||
from pydantic import SecretStr | ||
|
||
from backend.data.model import APIKeyCredentials, CredentialsField, CredentialsMetaInput | ||
from backend.integrations.providers import ProviderName | ||
|
||
NvidiaCredentials = APIKeyCredentials | ||
NvidiaCredentialsInput = CredentialsMetaInput[ | ||
Literal[ProviderName.NVIDIA], | ||
Literal["api_key"], | ||
] | ||
|
||
TEST_CREDENTIALS = APIKeyCredentials( | ||
id="01234567-89ab-cdef-0123-456789abcdef", | ||
provider="nvidia", | ||
api_key=SecretStr("mock-nvidia-api-key"), | ||
title="Mock Nvidia API key", | ||
expires_at=None, | ||
) | ||
|
||
TEST_CREDENTIALS_INPUT = { | ||
"provider": TEST_CREDENTIALS.provider, | ||
"id": TEST_CREDENTIALS.id, | ||
"type": TEST_CREDENTIALS.type, | ||
"title": TEST_CREDENTIALS.title, | ||
} | ||
|
||
|
||
def NvidiaCredentialsField() -> NvidiaCredentialsInput: | ||
"""Creates an Nvidia credentials input on a block.""" | ||
return CredentialsField(description="The Nvidia integration requires an API Key.") |
90 changes: 90 additions & 0 deletions
90
autogpt_platform/backend/backend/blocks/nvidia/deepfake.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from backend.blocks.nvidia._auth import ( | ||
NvidiaCredentials, | ||
NvidiaCredentialsField, | ||
NvidiaCredentialsInput, | ||
) | ||
from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema | ||
from backend.data.model import SchemaField | ||
from backend.util.request import requests | ||
|
||
|
||
class NvidiaDeepfakeDetectBlock(Block): | ||
class Input(BlockSchema): | ||
credentials: NvidiaCredentialsInput = NvidiaCredentialsField() | ||
image_base64: str = SchemaField( | ||
description="Image to analyze for deepfakes", image_upload=True | ||
) | ||
return_image: bool = SchemaField( | ||
description="Whether to return the processed image with markings", | ||
default=False, | ||
) | ||
|
||
class Output(BlockSchema): | ||
status: str = SchemaField( | ||
description="Detection status (SUCCESS, ERROR, CONTENT_FILTERED)", | ||
default="", | ||
) | ||
image: str = SchemaField( | ||
description="Processed image with detection markings (if return_image=True)", | ||
default="", | ||
image_output=True, | ||
) | ||
is_deepfake: float = SchemaField( | ||
description="Probability that the image is a deepfake (0-1)", | ||
default=0.0, | ||
) | ||
|
||
def __init__(self): | ||
super().__init__( | ||
id="8c7d0d67-e79c-44f6-92a1-c2600c8aac7f", | ||
description="Detects potential deepfakes in images using Nvidia's AI API", | ||
categories={BlockCategory.SAFETY}, | ||
input_schema=NvidiaDeepfakeDetectBlock.Input, | ||
output_schema=NvidiaDeepfakeDetectBlock.Output, | ||
) | ||
|
||
def run( | ||
self, input_data: Input, *, credentials: NvidiaCredentials, **kwargs | ||
) -> BlockOutput: | ||
url = "https://ai.api.nvidia.com/v1/cv/hive/deepfake-image-detection" | ||
|
||
headers = { | ||
"accept": "application/json", | ||
"content-type": "application/json", | ||
"Authorization": f"Bearer {credentials.api_key.get_secret_value()}", | ||
} | ||
|
||
image_data = f"data:image/jpeg;base64,{input_data.image_base64}" | ||
|
||
payload = { | ||
"input": [image_data], | ||
"return_image": input_data.return_image, | ||
} | ||
|
||
try: | ||
response = requests.post(url, headers=headers, json=payload) | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
result = data.get("data", [{}])[0] | ||
|
||
# Get deepfake probability from first bounding box if any | ||
deepfake_prob = 0.0 | ||
if result.get("bounding_boxes"): | ||
deepfake_prob = result["bounding_boxes"][0].get("is_deepfake", 0.0) | ||
|
||
yield "status", result.get("status", "ERROR") | ||
yield "is_deepfake", deepfake_prob | ||
|
||
if input_data.return_image: | ||
image_data = result.get("image", "") | ||
output_data = f"data:image/jpeg;base64,{image_data}" | ||
yield "image", output_data | ||
else: | ||
yield "image", "" | ||
|
||
except Exception as e: | ||
yield "error", str(e) | ||
yield "status", "ERROR" | ||
yield "is_deepfake", 0.0 | ||
yield "image", "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters