File size: 983 Bytes
b295d62
 
a54cb5b
b295d62
a54cb5b
83ec4f2
b295d62
 
 
a54cb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83ec4f2
b295d62
 
a54cb5b
b295d62
 
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
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from functools import lru_cache

from models import ApiWord, CheckResponse
from completions import check_text, load_model

app = FastAPI()

# model, tokenizer, device = load_model()

def rep(i):
    if i == 3:
        return -10, [" jumped", " leaps"]
    if i == 5:
        return -10, [" calm"]
    if i == 7:
        return -10, [" dog", " cat", " bird", " fish"]
    return -3, []

@lru_cache(maxsize=100)
def cached_check_text(text: str):
    # return check_text(text, model, tokenizer, device)
    result = []
    for i, w in enumerate(text.split()):
        logprob, replacements = rep(i)
        result.append(ApiWord(text=f" {w}", logprob=logprob, replacements=replacements))
    return result

@app.get("/check", response_model=CheckResponse)
def check(text: str):
    return CheckResponse(text=text, words=cached_check_text(text))

app.mount("/", StaticFiles(directory="frontend/public", html=True))