FlowKal commited on
Commit
5516ad2
·
verified ·
1 Parent(s): 744edea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # --- Загрузка модели и vocab ---
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)
10
+
11
+ model = PosFormerImprovedWithIAC(
12
+ vocab=vocab,
13
+ vocab_size=len(vocab.itos),
14
+ pad_token_id=vocab.stoi[PAD_TOKEN],
15
+ structure_symbols_set={PAD_TOKEN, SOS_TOKEN, EOS_TOKEN, UNK_TOKEN, "{","}","^","_"},
16
+ id_to_token_map={i:s for s,i in vocab.stoi.items()}
17
+ ).to(DEVICE)
18
+
19
+ checkpoint = torch.load("best_model.pth", map_location=DEVICE)
20
+ model.load_state_dict(checkpoint["model_state_dict"])
21
+ model.eval()
22
+
23
+ # --- Функция для Gradio ---
24
+ def inference(image, beam_size=5):
25
+ return predict_with_beam_search(model, image, vocab, beam_size, DEVICE)
26
+
27
+ # --- Gradio Interface ---
28
+ iface = gr.Interface(
29
+ fn=inference,
30
+ inputs=[
31
+ gr.inputs.Image(type="pil", label="Upload Image"),
32
+ gr.inputs.Slider(minimum=1, maximum=20, default=10, label="Beam Size")
33
+ ],
34
+ outputs=gr.outputs.Textbox(label="Predicted LaTeX"),
35
+ title="Handwritten Formula Recognition",
36
+ description="Загрузите изображение, и модель вернёт LaTeX-строку."
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ iface.launch()