File size: 2,307 Bytes
a130a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""When no language model can be reached (usage limits on every key and model), the proofread still delivers the
basic corrections and says the full check is still to be done; it does not fail. Models are replaced by fakes."""
import time

import pytest
from docx import Document

import detector as D
import fluency as FL
import groqai as G
import humanizer as H
import meaning as MEANING
import proofread as PR

TEXT = ("The model was trained on the the full dataset of 12,500 images and it reached an accuracy of 96.3% [4]. "
        "The results was compared with a baseline that used the same data and the same split of the samples.")


@pytest.fixture
def no_llm(monkeypatch, tmp_path):
    monkeypatch.setattr(G, "KEYS", ["k1"])
    monkeypatch.setattr(G, "_KEY_STATE", [dict(until=0.0, calls=0, err="", tok_left=None, tok_reset=0.0,
                                               tok_limit=None, inflight=0, tokens=0)])
    monkeypatch.setattr(G, "ENABLED", True)
    monkeypatch.setattr(G, "OPENAI_KEY", "")
    monkeypatch.setattr(G, "_down_until", time.time() + 9999)          # every key and model at its limit
    monkeypatch.setattr(D, "score_texts", lambda texts: [0.0] * len(texts))
    monkeypatch.setattr(H, "_detector_scores", lambda texts: [0.0] * len(texts))
    monkeypatch.setattr(FL, "scores", lambda texts: [0.9] * len(texts))
    monkeypatch.setattr(MEANING, "similarities", lambda pairs: [0.95] * len(pairs))
    monkeypatch.setattr(PR, "MODE", "batched")
    import lt_grammar as LT
    monkeypatch.setattr(LT, "MODE", "off")
    d = Document()
    d.add_paragraph(TEXT)
    path = tmp_path / "paper_humanized.docx"
    d.save(str(path))
    return str(path)


def test_basic_proofread_instead_of_an_error(no_llm):
    res = PR.proofread_docx(no_llm)
    assert res["limited"] is True
    assert "Basic proofread only" in res["note"] and "NOT been done yet" in res["note"]
    out = Document(res["path"]).paragraphs[0].text
    assert "the the" not in out                                 # the rule-based correction was still made
    assert "12,500" in out and "96.3%" in out and "[4]" in out


def test_the_error_can_be_restored(no_llm, monkeypatch):
    monkeypatch.setattr(PR, "BASIC_FALLBACK", False)
    with pytest.raises(G.Unavailable):
        PR.proofread_docx(no_llm)