Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
model = AutoModel.from_pretrained(model_name)
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
image_input = feature_extractor(images=image, return_tensors="pt")
|
| 15 |
-
text_input = tokenizer(text, return_tensors="pt")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
outputs="text",
|
| 28 |
-
title="deepseek-vl2-small Demo"
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
# Launch app
|
| 32 |
-
interface.launch()
|
|
|
|
| 1 |
+
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 and processor
|
| 9 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForVision2Seq.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# ✅ Test the model with an image
|
| 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
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
test_image_path = "test.jpg" # Replace with an actual image path
|
| 30 |
+
print("Generated Output:", predict(test_image_path))
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|