newtechdevng commited on
Commit
69ae6ea
·
verified ·
1 Parent(s): 6b530b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -1,36 +1,30 @@
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
- import torch
4
  from PIL import Image
5
 
6
- # Load your model from your HF repo
7
- model = torch.load(
8
- hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt"),
9
- map_location="cpu"
10
- )
11
- model.eval()
12
-
13
- # Define labels
14
- labels = ["car", "bike", "mountain", "road"] # update to match your model
15
 
16
  def predict(image):
17
- # Preprocess
18
- from torchvision import transforms
19
- transform = transforms.Compose([
20
- transforms.Resize((224, 224)),
21
- transforms.ToTensor(),
22
- ])
23
- tensor = transform(image).unsqueeze(0)
 
24
 
25
- with torch.no_grad():
26
- output = model(tensor)
27
- predicted = torch.argmax(output, dim=1).item()
28
 
29
- return labels[predicted]
30
 
31
  gr.Interface(
32
  fn=predict,
33
  inputs=gr.Image(type="pil"),
34
- outputs=gr.Label(),
35
- title="Car/Bike/Mountain/Road Detector"
36
  ).launch()
 
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
+ from ultralytics import YOLO
4
  from PIL import Image
5
 
6
+ # Load YOLO model correctly
7
+ model_path = hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt")
8
+ model = YOLO(model_path)
 
 
 
 
 
 
9
 
10
  def predict(image):
11
+ results = model(image)
12
+ result = results[0]
13
+
14
+ output = []
15
+ for box in result.boxes:
16
+ label = result.names[int(box.cls)]
17
+ confidence = float(box.conf)
18
+ output.append(f"{label}: {confidence:.2f}")
19
 
20
+ if not output:
21
+ return "No objects detected"
 
22
 
23
+ return "\n".join(output)
24
 
25
  gr.Interface(
26
  fn=predict,
27
  inputs=gr.Image(type="pil"),
28
+ outputs=gr.Text(label="Detections"),
29
+ title="Car / Bike / Mountain / Road Detector"
30
  ).launch()