internvl2.5 / app.py
xzerus's picture
Update app.py
fcc0cc5 verified
import torch
from PIL import Image
from transformers import AutoModel, CLIPImageProcessor
import gradio as gr
# Load the model
model = AutoModel.from_pretrained(
'OpenGVLab/InternVL2_5-1B',
torch_dtype=torch.float32, # Use float32 for CPU compatibility
low_cpu_mem_usage=True,
trust_remote_code=True,
use_flash_attn=False # Disable Flash Attention
).eval() # Do not move to CUDA, force CPU execution
# Load the image processor
image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL2_5-1B')
# Define the function to process the image and generate outputs
def process_image(image):
try:
# Convert uploaded image to RGB
image = image.convert('RGB')
# Preprocess the image
pixel_values = image_processor(images=image, return_tensors='pt').pixel_values
# Run the model on CPU
outputs = model(pixel_values)
# Assuming the model returns embeddings or features
return f"Output Shape: {outputs.last_hidden_state.shape}"
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface
demo = gr.Interface(
fn=process_image, # Function to process the input
inputs=gr.Image(type="pil"), # Accepts images as input
outputs=gr.Textbox(label="Model Output"), # Displays model output
title="InternViT Demo",
description="Upload an image to process it using the InternViT model from OpenGVLab."
)
# Launch the demo
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)