Spaces:
Build error
Build error
LayBraid
commited on
Commit
•
744fb8a
1
Parent(s):
5b62fe7
test app
Browse files
app.py
CHANGED
@@ -1,10 +1,45 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
if __name__ == "__main__":
|
10 |
-
gr.Interface(fn=send_inputs, inputs=["
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import clip
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
from PIL import Image
|
7 |
+
from torchvision.datasets import CIFAR100
|
8 |
|
9 |
+
model, preprocess = clip.load('ViT-B/32', "cpu")
|
10 |
|
11 |
+
|
12 |
+
cifar100 = CIFAR100(root=os.path.expanduser("~/.cache"), download=True, train=False)
|
13 |
+
|
14 |
+
|
15 |
+
IMG_SIZE = 32 if torch.cuda.is_available() else 32
|
16 |
+
COMPOSED_TRANSFORMERS = transforms.Compose([
|
17 |
+
transforms.Resize(IMG_SIZE),
|
18 |
+
transforms.ToTensor(),
|
19 |
+
])
|
20 |
+
|
21 |
+
NORMALIZE_TENSOR = transforms.Normalize(
|
22 |
+
mean=[0.485, 0.456, 0.406],
|
23 |
+
std=[0.229, 0.224, 0.225]
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
def np_array_to_tensor_image(img, width=IMG_SIZE, height=IMG_SIZE, device='cpu'):
|
28 |
+
image = Image.fromarray(img).convert('RGB').resize((width, height))
|
29 |
+
image = COMPOSED_TRANSFORMERS(image).unsqueeze(0)
|
30 |
+
return image.to(device, torch.float)
|
31 |
+
|
32 |
+
|
33 |
+
def normalize_tensor(tensor: torch.tensor) -> torch.tensor:
|
34 |
+
return NORMALIZE_TENSOR(tensor)
|
35 |
+
|
36 |
+
|
37 |
+
def send_inputs(img):
|
38 |
+
img = np_array_to_tensor_image(img)
|
39 |
+
img = normalize_tensor(img)
|
40 |
+
result = model(img)
|
41 |
+
return result
|
42 |
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
+
gr.Interface(fn=send_inputs, inputs=["image"], outputs="text").launch()
|