| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| import open_clip | |
| def greet(image): | |
| image = Image.fromarray(file_path) | |
| device = torch.device("cpu") | |
| model, _, preprocess = open_clip.create_model_and_transforms('ViT-L-14', pretrained='openai', device=device) | |
| image = preprocess().unsqueeze(0).to(device) | |
| with torch.amp.autocast(device_type=device.type): | |
| with torch.no_grad(): | |
| image_features = model.encode_image(image) | |
| image_features /= image_features.norm(dim=-1, keepdim=True) | |
| embedding = image_features[0] | |
| return str(embedding) | |
| demo = gr.Interface(fn=greet, inputs=gr.inputs.Image(), outputs="text") | |
| demo.launch() |