Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,17 @@ from PIL import Image
|
|
| 3 |
from transformers import AutoModel, CLIPImageProcessor
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load the model
|
| 7 |
model = AutoModel.from_pretrained(
|
| 8 |
'OpenGVLab/InternVL2_5-1B',
|
| 9 |
-
torch_dtype=torch.
|
| 10 |
low_cpu_mem_usage=True,
|
| 11 |
trust_remote_code=True,
|
| 12 |
-
use_flash_attn=
|
| 13 |
-
).eval() #
|
| 14 |
|
| 15 |
# Load the image processor
|
| 16 |
image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL2_5-1B')
|
|
@@ -22,10 +25,11 @@ def process_image(image):
|
|
| 22 |
image = image.convert('RGB')
|
| 23 |
|
| 24 |
# Preprocess the image
|
| 25 |
-
pixel_values = image_processor(images=image, return_tensors='pt').pixel_values
|
| 26 |
|
| 27 |
-
# Run the model
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
# Assuming the model returns embeddings or features
|
| 31 |
return f"Output Shape: {outputs.last_hidden_state.shape}"
|
|
@@ -37,10 +41,10 @@ demo = gr.Interface(
|
|
| 37 |
fn=process_image, # Function to process the input
|
| 38 |
inputs=gr.Image(type="pil"), # Accepts images as input
|
| 39 |
outputs=gr.Textbox(label="Model Output"), # Displays model output
|
| 40 |
-
title="
|
| 41 |
-
description="Upload an image to process it using the
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the demo
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
from transformers import AutoModel, CLIPImageProcessor
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Force the use of GPU
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
# Load the model
|
| 10 |
model = AutoModel.from_pretrained(
|
| 11 |
'OpenGVLab/InternVL2_5-1B',
|
| 12 |
+
torch_dtype=torch.float16, # Use float16 for GPU efficiency
|
| 13 |
low_cpu_mem_usage=True,
|
| 14 |
trust_remote_code=True,
|
| 15 |
+
use_flash_attn=True # Enable Flash Attention for improved performance
|
| 16 |
+
).to(device).eval() # Explicitly move the model to GPU
|
| 17 |
|
| 18 |
# Load the image processor
|
| 19 |
image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL2_5-1B')
|
|
|
|
| 25 |
image = image.convert('RGB')
|
| 26 |
|
| 27 |
# Preprocess the image
|
| 28 |
+
pixel_values = image_processor(images=image, return_tensors='pt').pixel_values.to(device) # Ensure tensor is on GPU
|
| 29 |
|
| 30 |
+
# Run the model
|
| 31 |
+
with torch.no_grad(): # Disable gradient calculations for inference
|
| 32 |
+
outputs = model(pixel_values)
|
| 33 |
|
| 34 |
# Assuming the model returns embeddings or features
|
| 35 |
return f"Output Shape: {outputs.last_hidden_state.shape}"
|
|
|
|
| 41 |
fn=process_image, # Function to process the input
|
| 42 |
inputs=gr.Image(type="pil"), # Accepts images as input
|
| 43 |
outputs=gr.Textbox(label="Model Output"), # Displays model output
|
| 44 |
+
title="InternVL2_5 Demo",
|
| 45 |
+
description="Upload an image to process it using the InternVL2_5-1B model from OpenGVLab."
|
| 46 |
)
|
| 47 |
|
| 48 |
# Launch the demo
|
| 49 |
if __name__ == "__main__":
|
| 50 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|