|
import torch |
|
from PIL import Image |
|
from transformers import AutoModel, CLIPImageProcessor |
|
import gradio as gr |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
model = AutoModel.from_pretrained( |
|
'OpenGVLab/InternVL2_5-1B', |
|
torch_dtype=torch.float16, |
|
low_cpu_mem_usage=True, |
|
trust_remote_code=True, |
|
use_flash_attn=True |
|
).to(device).eval() |
|
|
|
|
|
image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL2_5-1B') |
|
|
|
|
|
def process_image(image): |
|
try: |
|
|
|
image = image.convert('RGB') |
|
|
|
|
|
pixel_values = image_processor(images=image, return_tensors='pt').pixel_values.to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(pixel_values) |
|
|
|
|
|
return f"Output Shape: {outputs.last_hidden_state.shape}" |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=process_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Textbox(label="Model Output"), |
|
title="InternVL2_5 Demo", |
|
description="Upload an image to process it using the InternVL2_5-1B model from OpenGVLab." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|