Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
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.float32, # Use float32 for CPU compatibility
|
| 10 |
+
low_cpu_mem_usage=True,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
use_flash_attn=False # Disable Flash Attention
|
| 13 |
+
).eval() # Do not move to CUDA, force CPU execution
|
| 14 |
+
|
| 15 |
+
# Load the image processor
|
| 16 |
+
image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternVL2_5-1B')
|
| 17 |
+
|
| 18 |
+
# Define the function to process the image and generate outputs
|
| 19 |
+
def process_image(image):
|
| 20 |
+
try:
|
| 21 |
+
# Convert uploaded image to RGB
|
| 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 on CPU
|
| 28 |
+
outputs = model(pixel_values)
|
| 29 |
+
|
| 30 |
+
# Assuming the model returns embeddings or features
|
| 31 |
+
return f"Output Shape: {outputs.last_hidden_state.shape}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# Create the Gradio interface
|
| 36 |
+
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="InternViT Demo",
|
| 41 |
+
description="Upload an image to process it using the InternViT model from OpenGVLab."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch the demo
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|