-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-client-example.py
100 lines (83 loc) · 2.97 KB
/
api-client-example.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2021 - 2023, Bill Kennedy (https://github.com/rbbrdckybk/MiniGPT-4)
# SPDX-License-Identifier: MIT
# Simple MiniGPT-4 client
# start with: python api-client-example.py
# server must be running (see api-server.py)
import requests
import json
from os.path import exists
class MiniGPT4_Client:
def __init__(self):
self.url = 'http://localhost:5000'
self.debug = True
# reset the MiniGPT-4 session
def server_status(self):
if self.debug:
print('\nRequesting server status...')
url = self.url + '/api/v1/status'
r = requests.request("GET", url)
return r.text
# reset the MiniGPT-4 session
def server_reset(self):
if self.debug:
print('\nRequesting server reset...')
url = self.url + '/api/v1/reset'
r = requests.request("GET", url)
return r.text
# send an image to MiniGPT-4
# img is the image filename including path
def upload(self, img):
if self.debug:
print('\nUploading image to server (' + img + ')...')
if exists(img):
url = self.url + '/api/v1/upload'
payload = {}
mime = 'image/jpeg'
if img.lower().endswith('.png'):
mime = 'image/png'
files = [ ('file', (img, open(img,'rb'), mime)) ]
r = requests.request("POST", url, data=payload, files=files)
return r.text
else:
print('Error: attempt to upload image that does not exist: ' + img)
return ''
# ask the MiniGPT-4 server a question
def ask(self, message):
if self.debug:
print('\nSending query to server ("' + message + '")...')
url = self.url + '/api/v1/ask'
payload = {"message": message}
r = requests.request("POST", url, data=payload)
return r.text
# tell the MiniGPT-4 server to shut down
def server_shutdown(self):
url = self.url + '/api/v1/shutdown'
r = requests.request("GET", url)
return r.text
# r is a reponse from the MiniGPT-4 server
def debug_response(self, r):
if r != None and r != '':
parsed = json.loads(r)
print('Server response:')
print(json.dumps(parsed, indent=4))
else:
print('Empty response!')
# entry point
if __name__ == '__main__':
client = MiniGPT4_Client()
# get the server status
r = client.server_status()
client.debug_response(r)
# reset the server session
r = client.server_reset()
client.debug_response(r)
# upload an image to the server
r = client.upload('img\\simpsons.jpg')
client.debug_response(r)
# ask some questions
r = client.ask('what famous TV show characters are depicted in this image?')
client.debug_response(r)
r = client.ask('list 10 keywords appropriate for this image')
client.debug_response(r)
r = client.ask('write a short description for this image')
client.debug_response(r)