Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
|
8 |
+
# Load model and processor
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
model_name = "tjoab/latex_finetuned"
|
12 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
13 |
+
model = AutoModelForVision2Seq.from_pretrained(model_name).to(device)
|
14 |
+
return processor, model
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
processor, model = load_model()
|
19 |
+
|
20 |
+
st.title("LaTeX Image to Text Converter")
|
21 |
+
st.write("Upload an image containing a handwritten or printed math expression, and get the LaTeX code.")
|
22 |
+
|
23 |
+
# TODO: Add .png support (doesnt work as is with PIL Image.open())
|
24 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg"])
|
25 |
+
|
26 |
+
if uploaded_file:
|
27 |
+
image = Image.open(uploaded_file).convert("RGB")
|
28 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
29 |
+
|
30 |
+
# Preprocess image as model expects, then run inference
|
31 |
+
with st.spinner("Processing..."):
|
32 |
+
preproc_image = processor.image_processor(image, return_tensors="pt").pixel_values
|
33 |
+
preproc_image = preproc_image.to(device)
|
34 |
+
|
35 |
+
pred_ids = model.generate(preproc_image, max_length=128)
|
36 |
+
latex_pred = processor.batch_decode(pred_ids, skip_special_tokens=True)[0]
|
37 |
+
|
38 |
+
st.subheader("Predicted LaTeX Code:")
|
39 |
+
st.code(latex_pred, language="latex")
|