29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
import json, urllib.request, base64
|
|
|
|
KEY = "sk-aRNj3UwKSLPsDfh15QNTPwbHxahblfaO"
|
|
BASE = "https://token.sensenova.cn/v1"
|
|
MODEL = "sensenova-6.7-flash-lite"
|
|
|
|
img_b64 = base64.b64encode(open('/tmp/test_shot.jpg', 'rb').read()).decode()
|
|
payload = json.dumps({
|
|
"model": MODEL,
|
|
"messages": [{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}},
|
|
{"type": "text", "text": "请识别这张图片中的所有文字内容,包括数字、股票名称、金额、日期。用中文回复。"}
|
|
]
|
|
}],
|
|
"max_tokens": 1500,
|
|
}).encode()
|
|
req = urllib.request.Request(f"{BASE}/chat/completions", data=payload,
|
|
headers={"Content-Type": "application/json", "Authorization": f"Bearer {KEY}"})
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=90)
|
|
data = json.loads(resp.read().decode())
|
|
msg = data.get('choices', [{}])[0].get('message', {})
|
|
text = msg.get('content', '') or msg.get('reasoning', '')
|
|
print('OCR OK:')
|
|
print(text[:1500])
|
|
except Exception as e:
|
|
print('OCR FAIL:', e) |