File size: 1,048 Bytes
ef85a7a |
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 |
import requests
import base64
# Hugging Face Endpoint
HF_TOKEN = "hf_<>"
HF_ENDPOINT = "https://"
# 读取本地图片并转换为 Base64
def encode_image(image_path):
with open(image_path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
# 构造请求数据
image_base64 = encode_image("./test.png")
payload = {
"inputs": { # Wrap everything inside "inputs"
"image": image_base64, # 发送 Base64 编码的图片
"convs": [
{"role": "system", "content": "You are agent that can see, talk and act."},
{"role": "user", "content": "<image_start><image><image_end>\nWhat is in this image?"}
]
}
}
# 发送请求
headers = {
"Accept" : "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {HF_TOKEN}",
}
response = requests.post(HF_ENDPOINT, headers=headers, json=payload)
# 解析响应
if response.status_code == 200:
print("Response:", response.json())
else:
print("Error:", response.status_code, response.text) |