DEV Community

Manuel Abeledo
Manuel Abeledo

Posted on

Building Real-Time Deepfake Detection in Slack

Imagine the following scenario. You are taking care of some tickets, fixing stuff, polishing pull requests, fully in the flow. Someone messages you in Slack, and when you open the chat window, you see this picture:

Famous actor in a horrible white shirt

Well, that’s odd, I’m a huge Tom Cruise fan and I’m pretty sure he would never wear a Polo shirt.

Obviously, obviously, the need to set the record straight will overcome the necessities of the business, and you spend the next twenty minutes looking for the source of the image and proof that this is, in fact, not Captain Maverick. And of course, you find it.

Proof that the aforementioned famous actor picture is a fake

And now you will need another thirty minutes to go back to the flow, which is, let’s say, suboptimal.

In all seriousness, we all have had this kind of interaction, with our colleagues, our family, our friends. Deepfakes are virtually everywhere, and they are only getting more complex and difficult to detect with the naked eye. And while an image of Tom Cruise wearing Polo is perhaps not a big deal (debatable, but let’s move on), an image of a prominent figure in your organization doing something reputationally damaging announcing a sudden upcoming change in how your line of business is regulated definitely is.

Deception through AI-generated or -manipulated media has never been so easy to fabricate at scale. Thus, detection should follow suit.

That’s the reason why we, at Reality Defender, decided to publish a set of new tools and features, to help curb this growing synthetic media threat vector everywhere. To make deepfake detection even more accessible, we launched a free tier so developers, students, and small businesses can test and harness our best-in-class solution.

What can you expect from our SDK

Being developers ourselves, we took a look at what we value the most when it comes to integrating with third parties, and applied the desired principles to our very own SDKs.

  • Simplicity. All in a single package, and two main instructions: just upload the media you want to analyze, and check the results.
  • Flexibility. Choose between Python, Typescript, Java, Go, and Rust.
  • Transparency. All variants are available as open source.
  • Accessibility. Any developer can get 50 audio or image scans per month with the new free tier.
  • Community. We know that we can only go as far as you help us to, so we are open to suggestions, 24/7.

Integrating Reality Defender with Slack using Python

Let’s say that we want anyone with a Reality Defender API key, to be able to click on the contextual menu of any media posted on Slack, and get an authenticity verdict in a few seconds, e.g.

A contextual menu with the Reality Defender option enabled

Setting up our Slack workspace

Going back to our friend in the chat, it turns out that, at some point in the past, we were granted admin access to the Slack workspace console, which is really fortunate!

If you are not familiar with how Slack workspaces work, nor with the Slack Bolt framework, this is a good time to take a look at the documentation.

For the purposes of plugging our Reality Defender app in the Slack workspace, we’ll need the following.

  • Socket mode enabled.
  • Home tab enabled.
  • Analyze media shortcut with the following attributes: Location: Messages; Callback ID: analyze.
  • The following slash commands: /analysis-status and /setup-rd
  • OAuth bot token
  • The following bot token scopes: chat:write, commands, files:read, links:read, remote_files:read.
  • Subscribe to the following bot events: app_home_opened

In terms of permissions, we need our app to be able to see files and links, and to message back with the results of the analysis. Be aware that, with the aforementioned setup, the app will only be able to see files and links in channels it has been invited to.

We’ll then have two commands and a shortcut in our app, that must match with those configured for the app in the workspace app dashboard:

  • app.command(“/setup-rd”)
  • app.shortcut(“analyze”)
  • app.command(“/analysis-status”)

Interacting with Reality Defender through the Python SDK

Now, our user should be able to do exactly three things:

  • Log in with their Reality Defender API key.
  • Access the contextual menu of a media file, and click on the app shortcut.
  • Request and see the results of the analysis.

And because we don’t want to force our user to mindlessly request the results over and over again, the app should be able to write them in a thread right under the posted media, e.g.

Result presentation

This translates, in terms of things handled by the Reality Defender SDK, to just three operations: configure your client, upload media, and retrieve results.

Configuring the client requires only your API key. You could also specify the endpoint base URL, but we won’t need that here.

“””Configure the Reality Defender client”””
rd_client = RealityDefender(api_key=YOUR_API_KEY)
Enter fullscreen mode Exit fullscreen mode

Equally simple is uploading media. Note that files would need to be downloaded from Slack first, as the SDK does not support link analysis just yet.

"""Upload media to the Reality Defender platform"""
upload_result = await rd_client.upload(file_path=filename)
Enter fullscreen mode Exit fullscreen mode

With our request_id at hand, we could finally poll for results.

"""Wait for completion and retrieve results"""
result = await rd_client.get_result(
    upload_result["request_id"], max_attempts=60
)
Enter fullscreen mode Exit fullscreen mode

For convenience, we could also write this inside a simple wrapper that asynchronously checks for results and notifies the user when they are ready.

async def analysis() -> None:
    """
    Run analysis and wait for completion.
    """
    result = await rd_client.get_result(
        request_id, max_attempts=60
    )

    await self._notify_analysis_complete(result, request_id)
Enter fullscreen mode Exit fullscreen mode

Putting everything together

You can find the code of a complete and functional app in our public Slack app repository, which better illustrates how the app deals with result polling and messaging back into the Slack workspace. It also includes Docker configuration files, so you could give it both a Slack bot and app tokens, and run it right away. And of course, it is licensed under the terms of the Apache License 2.0, so feel free to modify and redistribute it.

And that is it! We hope this helps you keep your Slack workspace free of unwanted deepfakes, and more importantly, allows you to get back into the flow as quickly as possible.

Top comments (0)