FastAPI service wrapping the Surya-OCR-2 model (datalab-to) served through vLLM: legacy /v1/api/ai/* endpoints, an OpenAI-compatible /v1/chat/completions endpoint, a coalescing request batcher, a local OCR CLI, Docker packaging, multilingual example outputs, and quantization/concurrency benchmarks. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
# import base64
|
|
# import requests
|
|
# import os
|
|
|
|
# # Configuration
|
|
# API_URL = "http://localhost:5002/v1/api/ai/suya_ocr"
|
|
|
|
# # Helper to encode image
|
|
# def encode_image_to_base64(path):
|
|
# with open(path, "rb") as image_file:
|
|
# return base64.b64encode(image_file.read()).decode("utf-8")
|
|
|
|
# def test_ocr_endpoint_requests():
|
|
# image_path = "/path/to/surya/ttt.png"
|
|
# assert os.path.exists(image_path), f"Image file not found: {image_path}"
|
|
|
|
# encoded_image = encode_image_to_base64(image_path)
|
|
|
|
# payload = {
|
|
# "file": encoded_image,
|
|
# "type": "jpg",
|
|
# "skip_text_detection": False,
|
|
# "skip_table_detection": False,
|
|
# "recognize_math": False,
|
|
# "ocr_with_boxes": True
|
|
# }
|
|
|
|
# # Send request using requests
|
|
# response = requests.post(API_URL, json=payload)
|
|
|
|
# assert response.status_code == 200
|
|
# response_json = response.json()
|
|
# assert response_json["code"] == 200
|
|
# assert "ocr_text_json" in response_json["data"]
|
|
# assert "text_lines" in response_json["data"]
|
|
|
|
# print("OCR Response Text:")
|
|
# print(response_json["data"]["text_lines"])
|
|
|
|
|
|
# import time
|
|
# tik = time.time()
|
|
# test_ocr_endpoint_requests()
|
|
# tok = time.time()
|
|
# print("time consumption: ", tok - tik)
|
|
|
|
|
|
|
|
import requests
|
|
|
|
# API 地址
|
|
url = "http://127.0.0.1:5002/image2text"
|
|
|
|
# 测试用图像路径
|
|
image_path = "/path/to/surya/ttt.png" # 替换为你自己的图像路径
|
|
|
|
# 发起 POST 请求
|
|
with open(image_path, "rb") as image_file:
|
|
files = {"file": ("ttt.png", image_file, "image/png")}
|
|
response = requests.post(url, files=files)
|
|
|
|
# 打印响应
|
|
print("Status Code:", response.status_code)
|
|
print("Response JSON:", response.json())
|