File size: 1,234 Bytes
ca1a504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Text inputs that the tokenizer cannot take (a lone UTF-16 surrogate, valid in JSON) are a 400, not a 500."""

from __future__ import annotations

import os
import sys

import pytest

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(HERE)))
from tt_dsocr import config as C  # noqa: E402
from tt_dsocr.api import DeepSeekOcr, InputError  # noqa: E402


def _model(variant):
    m = DeepSeekOcr.__new__(DeepSeekOcr)
    m.cfg = C.resolve_config({"DSOCR_VARIANT": variant, "DSOCR_BACKEND": "fake"})
    m.vinfo = m.cfg.info
    m.tasks = C.TASK_PROMPTS[variant]
    return m


@pytest.mark.parametrize("variant", ["deepseek-ocr", "deepseek-ocr-2", "unlimited-ocr-2"])
def test_lone_surrogate_prompt_is_an_input_error(variant):
    m = _model(variant)
    with pytest.raises(InputError, match="surrogate"):
        m.resolve_prompt(None, "read this \ud800 please", None)
    if "locate" in m.tasks:
        with pytest.raises(InputError, match="surrogate"):
            m.resolve_prompt("locate", None, "x\udfff")
    # ordinary non-ASCII text is fine
    assert m.resolve_prompt(None, "읽어 주세요 ✓", None)[1] == "읽어 주세요 ✓"