thliang01 commited on
Commit
8235252
·
unverified ·
1 Parent(s): 630fd26

chore: remove zerogpu

Browse files
Files changed (2) hide show
  1. app.py +12 -69
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,83 +1,26 @@
1
- import os
2
- # Set this before importing torch to avoid issues
3
- os.environ["GRADIO_ALLOW_FLAGGING"] = "never"
4
-
5
  import torch
6
  import gradio as gr
7
  import requests
8
- from torchvision import transforms, models
 
 
 
9
 
10
  # Download human-readable labels for ImageNet.
11
  response = requests.get("https://git.io/JJkYN")
12
  labels = response.text.split("\n")
13
 
14
- # Load ResNet-34 model with pre-trained weights
15
- model = models.resnet34(weights=models.ResNet34_Weights.IMAGENET1K_V1)
16
- model.eval()
17
-
18
- # Preprocessing pipeline
19
- preprocess = transforms.Compose([
20
- transforms.Resize(256),
21
- transforms.CenterCrop(224),
22
- transforms.ToTensor(),
23
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
24
- ])
25
-
26
- def predict_without_gpu(inp):
27
- """CPU-only prediction for fallback"""
28
- if inp is None:
29
- return {}
30
-
31
- input_tensor = preprocess(inp).unsqueeze(0)
32
-
33
  with torch.no_grad():
34
- output = model(input_tensor)
35
- prediction = torch.nn.functional.softmax(output[0], dim=0)
36
- confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
37
-
38
  return confidences
39
 
40
- # Try to use GPU if available
41
- try:
42
- import spaces
43
-
44
- @spaces.GPU(duration=60)
45
- def predict(inp):
46
- """GPU-accelerated prediction"""
47
- if inp is None:
48
- return {}
49
-
50
- device = torch.device("cuda")
51
- model.to(device)
52
-
53
- input_tensor = preprocess(inp).unsqueeze(0).to(device)
54
-
55
- with torch.no_grad():
56
- output = model(input_tensor)
57
- prediction = torch.nn.functional.softmax(output[0], dim=0)
58
- prediction = prediction.cpu()
59
- confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
60
-
61
- return confidences
62
-
63
- print("Using GPU-accelerated prediction")
64
- prediction_fn = predict
65
-
66
- except Exception as e:
67
- print(f"GPU initialization failed: {e}")
68
- print("Using CPU-only prediction")
69
- prediction_fn = predict_without_gpu
70
-
71
- # Create the Gradio interface
72
- demo = gr.Interface(
73
- fn=prediction_fn,
74
  inputs=gr.Image(type="pil"),
75
  outputs=gr.Label(num_top_classes=3),
76
- title="Image Classifier",
77
- description="Upload an image to classify it using ResNet-34",
78
  css=".footer{display:none !important}"
79
- )
80
-
81
- # Launch without problematic parameters
82
- if __name__ == "__main__":
83
- demo.launch()
 
 
 
 
 
1
  import torch
2
  import gradio as gr
3
  import requests
4
+ from torchvision import transforms
5
+
6
+ # Load model
7
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
8
 
9
  # Download human-readable labels for ImageNet.
10
  response = requests.get("https://git.io/JJkYN")
11
  labels = response.text.split("\n")
12
 
13
+ def predict(inp):
14
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  with torch.no_grad():
16
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
17
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
 
 
18
  return confidences
19
 
20
+ gr.Interface(
21
+ fn=predict,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  inputs=gr.Image(type="pil"),
23
  outputs=gr.Label(num_top_classes=3),
24
+ examples=["lion.jpg", "cheetah.jpg", "cat.avif", "hot-dog.avif", "llama.jpg", "medieval_knight.jpg"],
 
25
  css=".footer{display:none !important}"
26
+ ).launch()
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
  torch
2
  gradio
3
- spaces
4
  numpy
5
  torchvision
6
- Pillow
 
1
  torch
2
  gradio
 
3
  numpy
4
  torchvision
5
+ Pillow