Skip to content

Commit

Permalink
[Discord] Update to use clarifai unit
Browse files Browse the repository at this point in the history
Update to use Clarifai Python SDK through clarifai unit
Change clarifai-grpc to secondary requirement for clarifai
Remove Bot.clarifai_stub
Update to use Clarifai Personal Access Token through clarifai unit
Remove Bot.CLARIFAI_API_KEY
  • Loading branch information
Harmon758 committed Nov 23, 2023
1 parent 80464b6 commit 9970c95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 74 deletions.
8 changes: 1 addition & 7 deletions Discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import aiohttp
from aiohttp import web
import asyncpg
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import service_pb2_grpc
import gidgethub.aiohttp
import git
import google.auth
Expand Down Expand Up @@ -138,7 +136,7 @@ def __init__(self):

# Credentials
for credential in (
"BRAWLHALLA_API_KEY", "CLARIFAI_API_KEY", "CLEVERBOT_API_KEY",
"BRAWLHALLA_API_KEY", "CLEVERBOT_API_KEY",
"DISCORDBOTLIST.COM_API_TOKEN", "DISCORD.BOTS.GG_API_TOKEN",
"DISCORDBOTS.ORG_API_KEY", "FIXER_API_KEY", "FONO_API_TOKEN",
"GIPHY_API_KEY", "GITHUB_PERSONAL_ACCESS_TOKEN", "GOOGLE_API_KEY",
Expand All @@ -160,10 +158,6 @@ def __init__(self):
sentry_sdk.init(self.SENTRY_DSN, release = self.version)

# External Clients
## Clarifai
self.clarifai_stub = service_pb2_grpc.V2Stub(
ClarifaiChannel.get_grpc_channel()
)
## Google Cloud Translation Service
self.google_cloud_project_id = "discord-bot-harmonbot"
## Imgur
Expand Down
84 changes: 18 additions & 66 deletions Discord/cogs/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
import re
from typing import Optional

from clarifai_grpc.grpc.api import service_pb2, resources_pb2
from clarifai_grpc.grpc.api.status import status_code_pb2
import imgurpython

from units import clarifai
from utilities import checks


CLARIFAI_COLOR_MODEL_ID = "eeed0b6733a644cea07cf4c60f87ebb7"
CLARIFAI_GENERAL_MODEL_ID = "aaa03c23b3724a16a56b629203edc62c"
CLARIFAI_NSFW_MODEL_ID = "e9576d86d2004ed1a38ba0cf39ecb4b1"


async def setup(bot):
await bot.add_cog(Images(bot))

Expand Down Expand Up @@ -82,23 +76,11 @@ async def image_color(self, ctx, image_url: Optional[str]):
return
image_url = ctx.message.attachments[0].url

response = ctx.bot.clarifai_stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
model_id = CLARIFAI_COLOR_MODEL_ID,
inputs = [
resources_pb2.Input(
data = resources_pb2.Data(
image = resources_pb2.Image(url = image_url)
)
)
]
),
metadata = (("authorization", f"Key {ctx.bot.CLARIFAI_API_KEY}"),)
)

if response.status.code != status_code_pb2.SUCCESS:
try:
colors = clarifai.image_color(image_url)
except Exception as e:
await ctx.embed_reply(
f"{ctx.bot.error_emoji} Error: {response.outputs[0].status.description}"
f"{ctx.bot.error_emoji} Error: {e}"
)
return

Expand All @@ -107,13 +89,12 @@ async def image_color(self, ctx, image_url: Optional[str]):
color.raw_hex.upper(),
(
f"{color.value * 100:.2f}%\n"
f"{re.sub(r'(?!^)(?=[A-Z])', ' ', color.w3c.name)}\n"
f"({color.w3c.hex.upper()})"
f"{re.sub(r'(?!^)(?=[A-Z])', ' ', color.w3c_name)}\n"
f"({color.w3c_hex.upper()})"
)
)
for color in sorted(
response.outputs[0].data.colors,
key = lambda c: c.value, reverse = True
colors, key = lambda c: c.value, reverse = True
)
]
await ctx.embed_reply(
Expand Down Expand Up @@ -278,32 +259,19 @@ async def image_recognition(self, ctx, image_url: Optional[str]):

image_url = ctx.message.attachments[0].url

response = ctx.bot.clarifai_stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
model_id = CLARIFAI_GENERAL_MODEL_ID,
inputs = [
resources_pb2.Input(
data = resources_pb2.Data(
image = resources_pb2.Image(url = image_url)
)
)
]
),
metadata = (("authorization", f"Key {ctx.bot.CLARIFAI_API_KEY}"),)
)

if response.status.code != status_code_pb2.SUCCESS:
try:
concepts = clarifai.image_recognition(image_url)
except Exception as e:
await ctx.embed_reply(
f"{ctx.bot.error_emoji} Error: {response.outputs[0].status.description}"
f"{ctx.bot.error_emoji} Error: {e}"
)
return

await ctx.embed_reply(
", ".join(
f"**{concept.name}**: {concept.value * 100:.2f}%"
for concept in sorted(
response.outputs[0].data.concepts,
key = lambda c: c.value, reverse = True
concepts, key = lambda c: c.value, reverse = True
)
),
thumbnail_url = image_url
Expand All @@ -320,31 +288,15 @@ async def nsfw(self, ctx, image_url: Optional[str]):
return
image_url = ctx.message.attachments[0].url

response = ctx.bot.clarifai_stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
model_id = CLARIFAI_NSFW_MODEL_ID,
inputs = [
resources_pb2.Input(
data = resources_pb2.Data(
image = resources_pb2.Image(url = image_url)
)
)
]
),
metadata = (("authorization", f"Key {ctx.bot.CLARIFAI_API_KEY}"),)
)

if response.status.code != status_code_pb2.SUCCESS:
try:
percentage = clarifai.image_nsfw(image_url) * 100
except Exception as e:
await ctx.embed_reply(
f"{ctx.bot.error_emoji} Error: {response.outputs[0].status.description}"
f"{ctx.bot.error_emoji} Error: {e}"
)
return

percentages = {
concept.name: concept.value * 100
for concept in response.outputs[0].data.concepts
}
await ctx.embed_reply(
f"NSFW: {percentages['nsfw']:.2f}%", thumbnail_url = image_url
f"NSFW: {percentage:.2f}%", thumbnail_url = image_url
)

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ asyncpg==0.28.0
beautifulsoup4==4.12.2
chess==1.9.4
clarifai==9.10.2
clarifai-grpc==9.10.0
cryptography==41.0.5
dice==4.0.0
emoji==1.7.0
Expand Down Expand Up @@ -104,6 +103,7 @@ catalogue==2.0.10
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.2.0
clarifai-grpc==9.10.0
click==8.1.7
cloudpathlib==0.16.0
colorama==0.4.6
Expand Down

0 comments on commit 9970c95

Please sign in to comment.