sherifleb commited on
Commit
e3ced66
·
verified ·
1 Parent(s): e0e625e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -1,39 +1,33 @@
1
  """Chart Pattern Detection API — YOLOv8"""
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
 
13
  def detect_patterns(image):
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)):
23
  box = r.boxes[i]
24
- cls_id = int(box.cls[0])
25
- conf = float(box.conf[0])
26
- xyxy = box.xyxy[0].tolist()
27
- label = r.names.get(cls_id, f"class_{cls_id}")
28
  patterns.append({
29
- "label": label,
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
 
38
  demo = gr.Interface(
39
  fn=detect_patterns,
 
1
  """Chart Pattern Detection API — YOLOv8"""
2
  import gradio as gr
3
  import json
4
+ from ultralyticsplus import YOLO
5
 
6
+ model = YOLO("foduucom/stockmarket-pattern-detection-yolov8")
7
+ model.overrides["conf"] = 0.20
8
+ model.overrides["iou"] = 0.45
 
 
9
  print(f"Model loaded. Classes: {model.names}")
10
 
11
  def detect_patterns(image):
12
  try:
13
  if image is None:
14
  return json.dumps({"patterns": [], "error": "No image"})
15
+ results = model.predict(source=image, imgsz=640)
16
  patterns = []
17
  for r in results:
18
  if r.boxes is None or len(r.boxes) == 0:
19
  continue
20
  for i in range(len(r.boxes)):
21
  box = r.boxes[i]
 
 
 
 
22
  patterns.append({
23
+ "label": r.names.get(int(box.cls[0]), "unknown"),
24
+ "confidence": round(float(box.conf[0]), 3),
25
+ "bbox": [round(float(x), 1) for x in box.xyxy[0].tolist()],
26
  })
27
  patterns.sort(key=lambda p: p["confidence"], reverse=True)
28
  return json.dumps({"patterns": patterns, "count": len(patterns)})
29
  except Exception as e:
30
+ return json.dumps({"patterns": [], "error": str(e)})
31
 
32
  demo = gr.Interface(
33
  fn=detect_patterns,