sherifleb commited on
Commit
c2a5a49
·
verified ·
1 Parent(s): b56dfce

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +2 -25
app.py CHANGED
@@ -2,23 +2,11 @@
2
  import gradio as gr
3
  import json
4
  import traceback
5
- import os
6
-
7
- # Fix PyTorch 2.6 weights_only issue BEFORE importing ultralytics
8
- import torch
9
- _orig_load = torch.load
10
- def _safe_load(*a, **kw):
11
- kw.setdefault("weights_only", False)
12
- return _orig_load(*a, **kw)
13
- torch.load = _safe_load
14
 
15
  from ultralytics import YOLO
16
  from huggingface_hub import hf_hub_download
17
 
18
- # Download model
19
  model_path = hf_hub_download(repo_id="foduucom/stockmarket-pattern-detection-yolov8", filename="model.pt")
20
- print(f"Model path: {model_path}, exists: {os.path.exists(model_path)}, size: {os.path.getsize(model_path)}")
21
-
22
  model = YOLO(model_path)
23
  print(f"Model loaded. Classes: {model.names}")
24
 
@@ -26,14 +14,9 @@ def detect_patterns(image):
26
  try:
27
  if image is None:
28
  return json.dumps({"patterns": [], "error": "No image"})
29
-
30
- # Run with verbose to see what happens
31
- results = model.predict(source=image, conf=0.20, iou=0.45, imgsz=640, verbose=True)
32
-
33
  patterns = []
34
  for r in results:
35
- print(f"Result: boxes={r.boxes.shape if r.boxes is not None else 'None'}, "
36
- f"names={r.names}")
37
  if r.boxes is None or len(r.boxes) == 0:
38
  continue
39
  for i in range(len(r.boxes)):
@@ -47,14 +30,8 @@ def detect_patterns(image):
47
  "confidence": round(conf, 3),
48
  "bbox": [round(x, 1) for x in xyxy],
49
  })
50
-
51
  patterns.sort(key=lambda p: p["confidence"], reverse=True)
52
- return json.dumps({
53
- "patterns": patterns,
54
- "count": len(patterns),
55
- "model_classes": model.names,
56
- "image_size": str(image.size) if hasattr(image, 'size') else "unknown",
57
- })
58
  except Exception as e:
59
  return json.dumps({"patterns": [], "error": str(e), "trace": traceback.format_exc()})
60
 
 
2
  import gradio as gr
3
  import json
4
  import traceback
 
 
 
 
 
 
 
 
 
5
 
6
  from ultralytics import YOLO
7
  from huggingface_hub import hf_hub_download
8
 
 
9
  model_path = hf_hub_download(repo_id="foduucom/stockmarket-pattern-detection-yolov8", filename="model.pt")
 
 
10
  model = YOLO(model_path)
11
  print(f"Model loaded. Classes: {model.names}")
12
 
 
14
  try:
15
  if image is None:
16
  return json.dumps({"patterns": [], "error": "No image"})
17
+ results = model.predict(source=image, conf=0.20, iou=0.45, imgsz=640, verbose=False)
 
 
 
18
  patterns = []
19
  for r in results:
 
 
20
  if r.boxes is None or len(r.boxes) == 0:
21
  continue
22
  for i in range(len(r.boxes)):
 
30
  "confidence": round(conf, 3),
31
  "bbox": [round(x, 1) for x in xyxy],
32
  })
 
33
  patterns.sort(key=lambda p: p["confidence"], reverse=True)
34
+ return json.dumps({"patterns": patterns, "count": len(patterns)})
 
 
 
 
 
35
  except Exception as e:
36
  return json.dumps({"patterns": [], "error": str(e), "trace": traceback.format_exc()})
37