Upload folder using huggingface_hub
Browse files
api.py
CHANGED
@@ -3,7 +3,6 @@ from flask import Flask, request, jsonify
|
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
import base64
|
6 |
-
import torch
|
7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
8 |
import threading
|
9 |
|
@@ -16,20 +15,9 @@ parser.add_argument('--device', type=str, choices=['cpu', 'gpu'], default='auto'
|
|
16 |
args = parser.parse_args()
|
17 |
|
18 |
# Determine the device
|
19 |
-
|
20 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
21 |
-
elif args.device == 'gpu':
|
22 |
-
if torch.cuda.is_available():
|
23 |
-
device = "cuda:0"
|
24 |
-
else:
|
25 |
-
raise ValueError("GPU option specified but no GPU is available.")
|
26 |
-
else:
|
27 |
-
device = "cpu"
|
28 |
-
|
29 |
-
torch_dtype = torch.float16 if device.startswith("cuda") else torch.float32
|
30 |
-
|
31 |
# Initialize the model and processor
|
32 |
-
model = AutoModelForCausalLM.from_pretrained(args.model_path,
|
33 |
processor = AutoProcessor.from_pretrained(args.model_path, trust_remote_code=True)
|
34 |
|
35 |
lock = threading.Lock() # Use a lock to ensure thread safety when accessing the model
|
@@ -38,7 +26,7 @@ def predict_image(image, task: str = "<OD>", prompt: str = None):
|
|
38 |
prompt = task + " " + prompt if prompt else task
|
39 |
print(f"Prompt: {prompt}")
|
40 |
with lock:
|
41 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device
|
42 |
generated_ids = model.generate(
|
43 |
input_ids=inputs["input_ids"],
|
44 |
pixel_values=inputs["pixel_values"],
|
|
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
import base64
|
|
|
6 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
7 |
import threading
|
8 |
|
|
|
15 |
args = parser.parse_args()
|
16 |
|
17 |
# Determine the device
|
18 |
+
device = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Initialize the model and processor
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(args.model_path, trust_remote_code=True).to(device)
|
21 |
processor = AutoProcessor.from_pretrained(args.model_path, trust_remote_code=True)
|
22 |
|
23 |
lock = threading.Lock() # Use a lock to ensure thread safety when accessing the model
|
|
|
26 |
prompt = task + " " + prompt if prompt else task
|
27 |
print(f"Prompt: {prompt}")
|
28 |
with lock:
|
29 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
30 |
generated_ids = model.generate(
|
31 |
input_ids=inputs["input_ids"],
|
32 |
pixel_values=inputs["pixel_values"],
|