Snapshot
Browse files- frontend/src/components/App.tsx +12 -8
- frontend/src/components/WordChip.tsx +5 -3
- main.py +22 -3
frontend/src/components/App.tsx
CHANGED
|
@@ -25,7 +25,7 @@ export default function App() {
|
|
| 25 |
const [words, setWords] = useState<Word[]>([])
|
| 26 |
const [isLoading, setIsLoading] = useState(false)
|
| 27 |
|
| 28 |
-
const check = async () => {
|
| 29 |
setIsLoading(true)
|
| 30 |
try {
|
| 31 |
const checkedWords = await checkText(text)
|
|
@@ -39,20 +39,24 @@ export default function App() {
|
|
| 39 |
const toggleMode = async () => {
|
| 40 |
if (mode === "edit") {
|
| 41 |
setIsLoading(true)
|
| 42 |
-
await check()
|
| 43 |
} else {
|
| 44 |
setMode("edit")
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
const handleReplace = async (index: number, newWord: string) => {
|
| 49 |
-
|
| 50 |
-
updatedWords
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
setWords(updatedWords)
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
let result
|
|
|
|
| 25 |
const [words, setWords] = useState<Word[]>([])
|
| 26 |
const [isLoading, setIsLoading] = useState(false)
|
| 27 |
|
| 28 |
+
const check = async (text: string) => {
|
| 29 |
setIsLoading(true)
|
| 30 |
try {
|
| 31 |
const checkedWords = await checkText(text)
|
|
|
|
| 39 |
const toggleMode = async () => {
|
| 40 |
if (mode === "edit") {
|
| 41 |
setIsLoading(true)
|
| 42 |
+
await check(text)
|
| 43 |
} else {
|
| 44 |
setMode("edit")
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
const handleReplace = async (index: number, newWord: string) => {
|
| 49 |
+
console.log("handleReplace", index, newWord)
|
| 50 |
+
const updatedWords = words.map((w, i) => {
|
| 51 |
+
if (i === index) {
|
| 52 |
+
return { text: newWord, logprob: 0, replacements: [] }
|
| 53 |
+
}
|
| 54 |
+
return w
|
| 55 |
+
})
|
| 56 |
setWords(updatedWords)
|
| 57 |
+
const newText = updatedWords.map(w => w.text).join("")
|
| 58 |
+
setText(newText)
|
| 59 |
+
await check(newText)
|
| 60 |
}
|
| 61 |
|
| 62 |
let result
|
frontend/src/components/WordChip.tsx
CHANGED
|
@@ -36,6 +36,7 @@ export const WordChip = ({
|
|
| 36 |
}
|
| 37 |
|
| 38 |
const handleReplacement = (event: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
|
| 39 |
const newWord = event.target.value
|
| 40 |
onReplace(newWord)
|
| 41 |
setIsExpanded(false);
|
|
@@ -58,11 +59,12 @@ export const WordChip = ({
|
|
| 58 |
position: "absolute",
|
| 59 |
top: "100%",
|
| 60 |
left: 0,
|
| 61 |
-
zIndex: 1000
|
|
|
|
| 62 |
}}
|
| 63 |
-
size={replacements.length}
|
| 64 |
-
autoFocus
|
| 65 |
>
|
|
|
|
| 66 |
{replacements.map((r, i) => (
|
| 67 |
<option key={i} value={r}>{r}</option>
|
| 68 |
))}
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
const handleReplacement = (event: React.ChangeEvent<HTMLSelectElement>) => {
|
| 39 |
+
console.log("handleReplacement", event.target.value)
|
| 40 |
const newWord = event.target.value
|
| 41 |
onReplace(newWord)
|
| 42 |
setIsExpanded(false);
|
|
|
|
| 59 |
position: "absolute",
|
| 60 |
top: "100%",
|
| 61 |
left: 0,
|
| 62 |
+
zIndex: 1000,
|
| 63 |
+
overflowY: "auto"
|
| 64 |
}}
|
| 65 |
+
size={replacements.length + 1}
|
|
|
|
| 66 |
>
|
| 67 |
+
<option key="original" hidden>{word}</option>
|
| 68 |
{replacements.map((r, i) => (
|
| 69 |
<option key={i} value={r}>{r}</option>
|
| 70 |
))}
|
main.py
CHANGED
|
@@ -1,15 +1,34 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 3 |
|
| 4 |
-
from models import CheckResponse
|
| 5 |
from completions import check_text, load_model
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
model, tokenizer, device = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.get("/check", response_model=CheckResponse)
|
| 12 |
def check(text: str):
|
| 13 |
-
return CheckResponse(text=text, words=
|
| 14 |
|
| 15 |
app.mount("/", StaticFiles(directory="frontend/public", html=True))
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from functools import lru_cache
|
| 4 |
|
| 5 |
+
from models import ApiWord, CheckResponse
|
| 6 |
from completions import check_text, load_model
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# model, tokenizer, device = load_model()
|
| 11 |
+
|
| 12 |
+
def rep(i):
|
| 13 |
+
if i == 3:
|
| 14 |
+
return -10, [" jumped", " leaps"]
|
| 15 |
+
if i == 5:
|
| 16 |
+
return -10, [" calm"]
|
| 17 |
+
if i == 7:
|
| 18 |
+
return -10, [" dog", " cat", " bird", " fish"]
|
| 19 |
+
return -3, []
|
| 20 |
+
|
| 21 |
+
@lru_cache(maxsize=100)
|
| 22 |
+
def cached_check_text(text: str):
|
| 23 |
+
# return check_text(text, model, tokenizer, device)
|
| 24 |
+
result = []
|
| 25 |
+
for i, w in enumerate(text.split()):
|
| 26 |
+
logprob, replacements = rep(i)
|
| 27 |
+
result.append(ApiWord(text=f" {w}", logprob=logprob, replacements=replacements))
|
| 28 |
+
return result
|
| 29 |
|
| 30 |
@app.get("/check", response_model=CheckResponse)
|
| 31 |
def check(text: str):
|
| 32 |
+
return CheckResponse(text=text, words=cached_check_text(text))
|
| 33 |
|
| 34 |
app.mount("/", StaticFiles(directory="frontend/public", html=True))
|