vverma
commited on
Commit
·
7d5d5aa
1
Parent(s):
2661513
HTML form
Browse files- __pycache__/app.cpython-39.pyc +0 -0
- app.py +22 -9
- templates/form.html +18 -0
__pycache__/app.cpython-39.pyc
CHANGED
Binary files a/__pycache__/app.cpython-39.pyc and b/__pycache__/app.cpython-39.pyc differ
|
|
app.py
CHANGED
@@ -1,31 +1,44 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
-
from fastapi.responses import
|
|
|
|
|
3 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
10 |
processor = TrOCRProcessor.from_pretrained('tjoab/latex_finetuned')
|
11 |
model = VisionEncoderDecoderModel.from_pretrained('tjoab/latex_finetuned')
|
12 |
|
13 |
-
@app.
|
14 |
-
async def
|
|
|
|
|
|
|
|
|
15 |
if file.content_type not in ["image/png", "image/jpeg"]:
|
16 |
-
return
|
17 |
|
18 |
-
# Read image contents
|
19 |
contents = await file.read()
|
20 |
image = Image.open(io.BytesIO(contents))
|
21 |
image = prepare_image(image)
|
22 |
|
23 |
-
# Preprocess and run inference
|
24 |
inputs = processor(images=image, return_tensors="pt").pixel_values
|
25 |
pred_ids = model.generate(inputs, max_length=128)
|
26 |
latex_preds = processor.batch_decode(pred_ids, skip_special_tokens=True)
|
27 |
|
28 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def prepare_image(image: Image.Image) -> Image.Image:
|
31 |
"""Converts image to RGB if needed and flattens transparency if present."""
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Form, Request
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
from fastapi.templating import Jinja2Templates
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Setup template engine
|
12 |
+
templates = Jinja2Templates(directory="templates")
|
13 |
+
|
14 |
+
# Load model and processor once
|
15 |
processor = TrOCRProcessor.from_pretrained('tjoab/latex_finetuned')
|
16 |
model = VisionEncoderDecoderModel.from_pretrained('tjoab/latex_finetuned')
|
17 |
|
18 |
+
@app.get("/", response_class=HTMLResponse)
|
19 |
+
async def form_page(request: Request):
|
20 |
+
return templates.TemplateResponse("form.html", {"request": request, "result": None})
|
21 |
+
|
22 |
+
@app.post("/", response_class=HTMLResponse)
|
23 |
+
async def handle_upload(request: Request, file: UploadFile = File(...)):
|
24 |
if file.content_type not in ["image/png", "image/jpeg"]:
|
25 |
+
return templates.TemplateResponse("form.html", {"request": request, "result": "Invalid file type"})
|
26 |
|
|
|
27 |
contents = await file.read()
|
28 |
image = Image.open(io.BytesIO(contents))
|
29 |
image = prepare_image(image)
|
30 |
|
|
|
31 |
inputs = processor(images=image, return_tensors="pt").pixel_values
|
32 |
pred_ids = model.generate(inputs, max_length=128)
|
33 |
latex_preds = processor.batch_decode(pred_ids, skip_special_tokens=True)
|
34 |
|
35 |
+
return templates.TemplateResponse("form.html", {"request": request, "result": latex_preds[0]})
|
36 |
+
|
37 |
+
def prepare_image(image: Image.Image) -> Image.Image:
|
38 |
+
if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info):
|
39 |
+
background = Image.new('RGB', image.size, 'white')
|
40 |
+
return Image.alpha_composite(background, image.convert('RGBA')).convert('RGB')
|
41 |
+
return image.convert('RGB')
|
42 |
|
43 |
def prepare_image(image: Image.Image) -> Image.Image:
|
44 |
"""Converts image to RGB if needed and flattens transparency if present."""
|
templates/form.html
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
<head>
|
4 |
+
<title>LaTeX Image Parser</title>
|
5 |
+
</head>
|
6 |
+
<body>
|
7 |
+
<h1>Upload an Image</h1>
|
8 |
+
<form action="/" method="post" enctype="multipart/form-data">
|
9 |
+
<input type="file" name="file" accept="image/png,image/jpeg" required>
|
10 |
+
<button type="submit">Upload</button>
|
11 |
+
</form>
|
12 |
+
|
13 |
+
{% if result %}
|
14 |
+
<h2>Prediction:</h2>
|
15 |
+
<pre>{{ result }}</pre>
|
16 |
+
{% endif %}
|
17 |
+
</body>
|
18 |
+
</html>
|