sguna commited on
Commit
b13e92e
1 Parent(s): 3cf4b90

Upload api_client_sample.py

Browse files

adding sample code to call the api

Files changed (1) hide show
  1. api_client_sample.py +39 -0
api_client_sample.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+
4
+ import requests
5
+ from PIL import Image
6
+
7
+ API_URL = "https://xt41z6d0qly7bg8w.us-east-1.aws.endpoints.huggingface.cloud"
8
+ headers = {
9
+ "Accept": "application/json",
10
+ "Authorization": "Bearer hf_###",
11
+ "Content-Type": "application/json"
12
+ }
13
+
14
+
15
+ def query(payload):
16
+ response = requests.post(API_URL, headers=headers, json=payload)
17
+ return response.json()
18
+
19
+
20
+ image_path = 'car_clock.jpg' # Replace with the path to your image file
21
+
22
+ # Open the image and convert it to bytes
23
+ with Image.open(image_path) as img:
24
+ img_bytes = io.BytesIO()
25
+ img.save(img_bytes, format='JPEG')
26
+ image_bytes = img_bytes.getvalue()
27
+ image_base64 = base64.b64encode(image_bytes).decode('utf-8')
28
+
29
+ output = query({
30
+ "inputs": {
31
+ "text": "",
32
+ "image": image_base64
33
+ },
34
+ "parameters": {
35
+ "max_new_tokens": 150
36
+ }
37
+ })
38
+
39
+ print(output)