Views
No views yet
1import requests
2import json
3import base64
4import os
5
6
7requests.packages.urllib3.disable_warnings()
8BAD_RESPONSE = "<error></error>"
9def image_to_base64(image_path):
10 with open(image_path, "rb") as image_file:
11 encoded_string = base64.b64encode(image_file.read())
12 return encoded_string.decode('utf-8')
13
14def history_to_prompt(query):
15 answer_format = 'Answer:'
16 prompt = ''
17 prompt += 'Question: {} {}'.format(query, answer_format)
18 return prompt
19
20def get_response(image_path, question):
21 image_extension = os.path.splitext(image_path)[1][1:]
22
23 base64_img = image_to_base64(image_path)
24
25 url = 'http://127.0.0.1:8080'
26 headers = {
27 'Content-Type': 'application/json',
28 }
29
30 prompt = history_to_prompt(question)
31 payload = {
32 "inputs": f"{prompt}",
33 "stream": False,
34 "parameters": {
35 "best_of": 1,
36 "decoder_input_details": False,
37 "details": False,
38 "repetition_penalty": 1.1,
39 "do_sample": True,
40 "max_new_tokens": 1000,
41 "return_full_text": False,
42 "temperature": 0.8,
43 "top_p": 0.4,
44 "top_k": 1
45 }
46 }
47 try_times = 0
48 while try_times < 3:
49 try_times += 1
50 try:
51 response = requests.post(url, headers=headers, stream=False, data=json.dumps(payload), verify=False)
52 if response.status_code == 200:
53 try:
54 output = response.json()[0]["generated_text"].strip()
55 return output
56 except Exception as e:
57 pass
58 else:
59 print(f"Received bad status code: {response.status_code}")
60 except requests.exceptions.ConnectionError as errc:
61 print("Error Connecting:", errc)
62 except requests.exceptions.Timeout as errt:
63 print("Timeout Error:", errt)
64 except requests.exceptions.RequestException as err:
65 print("Something Else:", err)
66 return BAD_RESPONSE
67if __name__ == "__main__":
68 from glob import glob
69 files = glob("demo.jpeg")
70 for file in files:
71 print(file)
72 print(get_response(
73 image_path=file,
74 question="who is this",
75 ))@misc{wang2023cogvlm,
title={CogVLM: Visual Expert for Pretrained Language Models},
author={Weihan Wang and Qingsong Lv and Wenmeng Yu and Wenyi Hong and Ji Qi and Yan Wang and Junhui Ji and Zhuoyi Yang and Lei Zhao and Xixuan Song and Jiazheng Xu and Bin Xu and Juanzi Li and Yuxiao Dong and Ming Ding and Jie Tang},
year={2023},
eprint={2311.03079},
archivePrefix={arXiv},
primaryClass={cs.CV}
}