vverma commited on
Commit
6816a14
·
1 Parent(s): 1bdf561

added PNG and latex code

Browse files
Files changed (2) hide show
  1. 1+7.png +0 -0
  2. app.py +26 -1
1+7.png ADDED
app.py CHANGED
@@ -1,8 +1,33 @@
1
  from fastapi import FastAPI
 
 
2
 
3
  app = FastAPI()
4
 
5
  @app.get("/")
6
  def greet_json():
7
- return {"Hello": "World!"}
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ from PIL import Image
4
 
5
  app = FastAPI()
6
 
7
  @app.get("/")
8
  def greet_json():
9
+ # Load model and processor from Hugging Face
10
+ processor = TrOCRProcessor.from_pretrained('tjoab/latex_finetuned')
11
+ model = VisionEncoderDecoderModel.from_pretrained('tjoab/latex_finetuned')
12
 
13
+
14
+ # Load all images as a batch
15
+ images = [open_PIL_image(path) for path in paths]
16
+
17
+ # Preprocess the images
18
+ preproc_image = processor.image_processor(images=images, return_tensors="pt").pixel_values
19
+
20
+ # Generate and decode the tokens
21
+ # NOTE: max_length default value is very small, which often results in truncated inference if not set
22
+ pred_ids = model.generate(preproc_image, max_length=128)
23
+ latex_preds = processor.batch_decode(pred_ids, skip_special_tokens=True)
24
+ return {"message": "Success", "latex_preds": latex_preds}
25
+
26
+
27
+
28
+ # Helper funtion (path to either JPEG or PNG)
29
+ def open_PIL_image(image_path: str) -> Image.Image:
30
+ image = Image.open(image_path)
31
+ if image_path.split('.')[-1].lower() == 'png':
32
+ image = Image.composite(image, PIL.Image.new('RGB', image.size, 'white'), image)
33
+ return image