-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
executable file
·67 lines (53 loc) · 1.96 KB
/
sample.py
File metadata and controls
executable file
·67 lines (53 loc) · 1.96 KB
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
#!/usr/bin/env python3
"""Example of using API4AI NSFW image classification."""
import os
import sys
import requests
# Use 'demo' mode just to try api4ai for free. ⚠️ Free demo is rate limited and must not be used in real projects.
#
# Use 'normal' mode if you have an API Key from the API4AI Developer Portal. This is the method that users should normally prefer.
#
# Use 'rapidapi' if you want to try api4ai via RapidAPI marketplace.
# For more details visit:
# https://rapidapi.com/api4ai-api4ai-default/api/nsfw3/details
MODE = 'demo'
# Your API4AI key. Fill this variable with the proper value if you have one.
API4AI_KEY = ''
# Your RapidAPI key. Fill this variable with the proper value if you want
# to try api4ai via RapidAPI marketplace.
RAPIDAPI_KEY = ''
OPTIONS = {
'demo': {
'url': 'https://demo.api4ai.cloud/nsfw/v1/results'
},
'normal': {
'url': 'https://api4ai.cloud/nsfw/v1/results',
'headers': {'X-API-KEY': API4AI_KEY}
},
'rapidapi': {
'url': 'https://nsfw3.p.rapidapi.com/v1/results',
'headers': {'X-RapidAPI-Key': RAPIDAPI_KEY}
}
}
if __name__ == '__main__':
# Parse args.
image = sys.argv[1] if len(sys.argv) > 1 else 'https://static.api4.ai/samples/nsfw-1.jpg'
if '://' in image:
# POST image via URL.
response = requests.post(
OPTIONS[MODE]['url'],
headers=OPTIONS[MODE].get('headers'),
data={'url': image})
else:
# POST image as file.
with open(image, 'rb') as image_file:
response = requests.post(
OPTIONS[MODE]['url'],
headers=OPTIONS[MODE].get('headers'),
files={'image': (os.path.basename(image), image_file)}
)
# Print raw response.
print(f'💬 Raw response:\n{response.text}\n')
# Parse response and probabilities.
probs = response.json()['results'][0]['entities'][0]['classes']
print(f'💬 Probabilities:\n{probs}')