-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
executable file
·74 lines (58 loc) · 2.15 KB
/
sample.py
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
#!/usr/bin/env python3
"""Example of using API4AI Alcohol Label Recognition."""
import asyncio
import sys
import aiohttp
# Use 'demo' mode just to try api4ai for free. Free demo is rate limited.
# For more details visit:
# https://api4.ai
#
# Use 'rapidapi' if you want to try api4ai via RapidAPI marketplace.
# For more details visit:
# https://rapidapi.com/api4ai-api4ai-default/api/alcohol-label-recognition/details
MODE = 'demo'
# 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/alco-rec/v1/results',
'headers': {'A4A-CLIENT-APP-ID': 'sample'}
},
'rapidapi': {
'url': 'https://alcohol-label-recognition.p.rapidapi.com/v1/results',
'headers': {'X-RapidAPI-Key': RAPIDAPI_KEY}
}
}
async def main():
"""Entry point."""
image = sys.argv[1] if len(sys.argv) > 1 else 'https://storage.googleapis.com/api4ai-static/samples/alco-rec-1.jpg'
async with aiohttp.ClientSession() as session:
if '://' in image:
# Data from image URL.
data = {'url': image}
else:
# Data from local image file.
data = {'image': open(image, 'rb')}
# Make request.
async with session.post(OPTIONS[MODE]['url'],
data=data,
headers=OPTIONS[MODE]['headers']) as response:
resp_json = await response.json()
resp_text = await response.text()
# Print raw response.
print(f'💬 Raw response:\n{resp_text}\n')
# Parse response and print labels.
text = ''
labels = resp_json['results'][0]['entities'][0]['array']
for i, label in enumerate(labels):
if i > 0:
text += '\n\n'
text += f'{label["drink"]}:'
for prop in label:
if prop != 'drink':
text += f'\n 👉 {prop}: {label[prop]}'
print(f'💬 Labels:\n{text}')
if __name__ == '__main__':
# Run async function in asyncio loop.
asyncio.run(main())