Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import pickle
|
| 4 |
-
from model import predict_with_beam_search, PosFormerImprovedWithIAC, PAD_TOKEN, SOS_TOKEN, EOS_TOKEN
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
with open("vocab.pkl", "rb") as f:
|
| 9 |
vocab = pickle.load(f)
|
|
@@ -16,24 +16,28 @@ model = PosFormerImprovedWithIAC(
|
|
| 16 |
id_to_token_map={i:s for s,i in vocab.stoi.items()}
|
| 17 |
).to(DEVICE)
|
| 18 |
|
| 19 |
-
checkpoint = torch.load("
|
| 20 |
model.load_state_dict(checkpoint["model_state_dict"])
|
| 21 |
model.eval()
|
| 22 |
|
| 23 |
-
# ---
|
| 24 |
def inference(image, beam_size=5):
|
|
|
|
| 25 |
return predict_with_beam_search(model, image, vocab, beam_size, DEVICE)
|
| 26 |
|
| 27 |
-
# ---
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=inference,
|
| 30 |
inputs=[
|
| 31 |
-
gr.
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
],
|
| 34 |
-
outputs=gr.
|
| 35 |
title="Handwritten Formula Recognition",
|
| 36 |
-
description="
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import pickle
|
| 4 |
+
from model import predict_with_beam_search, PosFormerImprovedWithIAC, PAD_TOKEN, SOS_TOKEN, EOS_TOKEN, UNK_TOKEN
|
| 5 |
|
| 6 |
+
# --- загрузка модели (как в предыдущем примере) ---
|
| 7 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
with open("vocab.pkl", "rb") as f:
|
| 9 |
vocab = pickle.load(f)
|
|
|
|
| 16 |
id_to_token_map={i:s for s,i in vocab.stoi.items()}
|
| 17 |
).to(DEVICE)
|
| 18 |
|
| 19 |
+
checkpoint = torch.load("d256 layers=4 densenet-121 32%.pth", map_location=DEVICE)
|
| 20 |
model.load_state_dict(checkpoint["model_state_dict"])
|
| 21 |
model.eval()
|
| 22 |
|
| 23 |
+
# --- функция инференса ---
|
| 24 |
def inference(image, beam_size=5):
|
| 25 |
+
# image здесь — PIL.Image из Sketchpad
|
| 26 |
return predict_with_beam_search(model, image, vocab, beam_size, DEVICE)
|
| 27 |
|
| 28 |
+
# --- интерфейс с полем рисования ---
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=inference,
|
| 31 |
inputs=[
|
| 32 |
+
gr.Sketchpad(
|
| 33 |
+
shape=(IMG_HEIGHT, IMG_WIDTH),
|
| 34 |
+
label="Нарисуйте формулу"
|
| 35 |
+
),
|
| 36 |
+
gr.Slider(minimum=1, maximum=20, default=10, label="Beam Size")
|
| 37 |
],
|
| 38 |
+
outputs=gr.Textbox(label="Предсказанная LaTeX-строка"),
|
| 39 |
title="Handwritten Formula Recognition",
|
| 40 |
+
description="Нарисуйте формулу на холсте и получите LaTeX."
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|