星火大模型——多模态API调用(图像+文本)

参考文档

环境配置

pip3 install websocket-client

核心代码

import _thread as thread
import base64
import datetime
import hashlib
import hmac
import json
from urllib.parse import urlparse
import ssl
from datetime import datetime
from time import mktime
from urllib.parse import urlencode
from wsgiref.handlers import format_date_time
import websocket  # 使用websocket_client
from PIL import Image
import io

appid = "xxx"    #填写控制台中获取的 APPID 信息
api_secret = "xxxx"   #填写控制台中获取的 APISecret 信息
api_key ="xxxx"    #填写控制台中获取的 APIKey 信息
#  = open("images\PointAClouth.png",'rb').read()

# Function to resize the image
def resize_image(image_path, size):
    image = Image.open(image_path)
    resized_image = image.resize(size)
    print(resized_image.size)
    # import pdb; pdb.set_trace()
    return resized_image

# Function to encode the image
def encode_image(image):
    image_byte_array = io.BytesIO()
    image.save(image_byte_array, format='PNG')
    image_byte_array = image_byte_array.getvalue()
    return image_byte_array, base64.b64encode(image_byte_array).decode('utf-8')

# Path to your image
image_path = "images/PointAClouth.png"

# Getting the base64 string of the resized image
new_size = (1000, 1000)
resized_image = resize_image(image_path, new_size)
imagedata, base64_image = encode_image(resized_image)



imageunderstanding_url = "wss://spark-api.cn-huabei-1.xf-yun.com/v2.1/image"#云端环境的服务地址
text =[
    {
   
        "role": "user", 
        "content":
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值