typst_hlm / ocr /qwen_ocr.py
dlxj
m
073f689
Raw
History Blame Contribute Delete
1.15 kB
# 如果没有安装OpenAI Python SDK,可使用命令安装:pip install OpenAI
import base64
from openai import OpenAI
# 读取图片并转为 base64
image_path = "0375.jpg"
with open(image_path, "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
# 初始化客户端
client = OpenAI(
base_url="https://www.autodl.art/api/v1",
api_key="",
)
# 调用接口(这里使用了stream=True进行流式响应)
stream = client.chat.completions.create(
model="qwen3.6-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "提取图片中的文字,按阅读顺序输出"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}"
}
}
]
}
],
stream=True,
)
# 流式打印输出
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")