Arghya Ghosh commited on
Commit
44144f8
·
verified ·
1 Parent(s): 91a7111

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -9
main.py CHANGED
@@ -1,13 +1,15 @@
1
- from fastapi import FastAPI, Query
2
  from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
3
  from qwen_vl_utils import process_vision_info
 
4
  import torch
 
5
 
6
  app = FastAPI()
7
 
8
  checkpoint = "Qwen/Qwen2.5-VL-3B-Instruct"
9
- min_pixels = 256*28*28
10
- max_pixels = 1280*28*28
11
  processor = AutoProcessor.from_pretrained(
12
  checkpoint,
13
  min_pixels=min_pixels,
@@ -17,28 +19,35 @@ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
17
  checkpoint,
18
  torch_dtype=torch.bfloat16,
19
  device_map="auto",
20
- # attn_implementation="flash_attention_2",
21
  )
22
 
23
  @app.get("/")
24
  def read_root():
25
  return {"message": "API is live. Use the /predict endpoint."}
26
 
27
- @app.get("/predict")
28
- def predict(image_url: str = Query(...), prompt: str = Query(...)):
 
 
 
 
 
29
  messages = [
30
- {"role": "system", "content": "You are a helpful assistant with vision abilities."},
31
- {"role": "user", "content": [{"type": "image", "image": image_url}, {"type": "text", "text": prompt}]},
32
  ]
 
33
  text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34
  image_inputs, video_inputs = process_vision_info(messages)
 
35
  inputs = processor(
36
  text=[text],
37
  images=image_inputs,
38
  videos=video_inputs,
39
  padding=True,
40
- return_tensors="pt",
41
  ).to(model.device)
 
42
  with torch.no_grad():
43
  generated_ids = model.generate(**inputs, max_new_tokens=128)
44
  generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
  from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
3
  from qwen_vl_utils import process_vision_info
4
+ from PIL import Image
5
  import torch
6
+ import io
7
 
8
  app = FastAPI()
9
 
10
  checkpoint = "Qwen/Qwen2.5-VL-3B-Instruct"
11
+ min_pixels = 256 * 28 * 28
12
+ max_pixels = 1280 * 28 * 28
13
  processor = AutoProcessor.from_pretrained(
14
  checkpoint,
15
  min_pixels=min_pixels,
 
19
  checkpoint,
20
  torch_dtype=torch.bfloat16,
21
  device_map="auto",
 
22
  )
23
 
24
  @app.get("/")
25
  def read_root():
26
  return {"message": "API is live. Use the /predict endpoint."}
27
 
28
+ @app.post("/predict")
29
+ async def predict(file: UploadFile = File(...), prompt: str = Form(...)):
30
+ # Load the image from uploaded file
31
+ image_bytes = await file.read()
32
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
33
+
34
+ # Compose vision-text messages
35
  messages = [
36
+ {"role": "system", "content": "You are a helpful assistant with vision abilities. You are the best OCR reader your task is to do OCR analysis of the given image and return the OCR data"},
37
+ {"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": prompt}]},
38
  ]
39
+
40
  text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
41
  image_inputs, video_inputs = process_vision_info(messages)
42
+
43
  inputs = processor(
44
  text=[text],
45
  images=image_inputs,
46
  videos=video_inputs,
47
  padding=True,
48
+ return_tensors="pt"
49
  ).to(model.device)
50
+
51
  with torch.no_grad():
52
  generated_ids = model.generate(**inputs, max_new_tokens=128)
53
  generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]