File size: 2,638 Bytes
1688f96 |
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 |
import json
import base64
import os
import requests
import json
from PIL import Image, ImageDraw, ImageFont
import ast
import re
def encode_image_to_base64(img_path):
"""
:param img_path: image path
:return: Base64 encoding
"""
try:
with open(img_path, 'rb') as img_file:
img_data = img_file.read()
base64_str = base64.b64encode(img_data).decode('utf-8')
return base64_str
except Exception as e:
print(f"Encoding Error: {e}")
return None
def infer_image_with_api(image_path, prompt):
url = "http://127.0.0.1:8000/v1/chat/completions"
base64_image = encode_image_to_base64(image_path)
payload = json.dumps({
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "你是华为公司开发的多模态大模型,名字是openPangu-VL-7B。你能够处理文本和视觉模态的输入,并给出文本输出。"},
]
},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/jpg;base64,{base64_image }"}},
{"type": "text", "text": prompt},
]
}
],
"model": "pangu_vl",
"max_tokens": 2048,
"temperature": 0,
"stream": False,
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return json.loads(response.text)["choices"][0]["message"]["content"]
def infer_message_with_api(prompt):
url = "http://127.0.0.1:8000/v1/chat/completions"
payload = json.dumps({
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "你是华为公司开发的多模态大模型,名字是openPangu-VL-7B。你能够处理文本和视觉模态的输入,并给出文本输出。"},
]
},
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
]
}
],
"model": "pangu_vl",
"max_tokens": 2048,
"temperature": 0,
"stream": False,
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return json.loads(response.text)["choices"][0]["message"]["content"]
res = infer_message_with_api("你好,你是谁?")
print(res)
|