Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,26 +2,30 @@ import torch
|
|
2 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
3 |
from PIL import Image
|
4 |
|
5 |
-
# β
Define the model name from Hugging Face
|
6 |
MODEL_NAME = "deepseek-ai/deepseek-vl2-small"
|
7 |
|
8 |
-
# β
Load model
|
9 |
-
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
10 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# β
Test
|
13 |
def predict(image_path):
|
14 |
image = Image.open(image_path).convert("RGB")
|
15 |
-
|
16 |
# Process input
|
17 |
inputs = processor(images=image, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
|
19 |
# Generate output
|
20 |
output = model.generate(**inputs)
|
21 |
-
|
22 |
# Decode response
|
23 |
generated_text = processor.batch_decode(output, skip_special_tokens=True)[0]
|
24 |
-
|
25 |
return generated_text
|
26 |
|
27 |
# β
Example Usage
|
|
|
2 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
3 |
from PIL import Image
|
4 |
|
5 |
+
# β
Define the correct model name from Hugging Face
|
6 |
MODEL_NAME = "deepseek-ai/deepseek-vl2-small"
|
7 |
|
8 |
+
# β
Load processor & model with `trust_remote_code=True`
|
9 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
10 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
11 |
+
MODEL_NAME,
|
12 |
+
torch_dtype=torch.float16,
|
13 |
+
trust_remote_code=True # β
This allows loading custom model implementations
|
14 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
|
16 |
+
# β
Test function to process an image
|
17 |
def predict(image_path):
|
18 |
image = Image.open(image_path).convert("RGB")
|
19 |
+
|
20 |
# Process input
|
21 |
inputs = processor(images=image, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
|
23 |
# Generate output
|
24 |
output = model.generate(**inputs)
|
25 |
+
|
26 |
# Decode response
|
27 |
generated_text = processor.batch_decode(output, skip_special_tokens=True)[0]
|
28 |
+
|
29 |
return generated_text
|
30 |
|
31 |
# β
Example Usage
|