Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
"""Chart Pattern Detection API — YOLOv8
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
-
# Fix PyTorch 2.6+ weights_only default
|
| 7 |
_original_load = torch.load
|
| 8 |
def _patched_load(*args, **kwargs):
|
| 9 |
if "weights_only" not in kwargs:
|
|
@@ -11,38 +12,42 @@ def _patched_load(*args, **kwargs):
|
|
| 11 |
return _original_load(*args, **kwargs)
|
| 12 |
torch.load = _patched_load
|
| 13 |
|
| 14 |
-
from
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model
|
| 19 |
|
| 20 |
def detect_patterns(image):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=detect_patterns,
|
| 43 |
inputs=gr.Image(type="pil", label="Chart Image"),
|
| 44 |
outputs=gr.Textbox(label="Detected Patterns (JSON)"),
|
| 45 |
title="Chart Pattern Detection — YOLOv8",
|
| 46 |
-
description="Detects: Head & Shoulders (Top/Bottom), Double Top/Bottom, Triangles, Trend Lines",
|
| 47 |
)
|
| 48 |
-
demo.launch()
|
|
|
|
| 1 |
+
"""Chart Pattern Detection API — YOLOv8"""
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import json
|
| 5 |
+
import traceback
|
| 6 |
|
| 7 |
+
# Fix PyTorch 2.6+ weights_only default
|
| 8 |
_original_load = torch.load
|
| 9 |
def _patched_load(*args, **kwargs):
|
| 10 |
if "weights_only" not in kwargs:
|
|
|
|
| 12 |
return _original_load(*args, **kwargs)
|
| 13 |
torch.load = _patched_load
|
| 14 |
|
| 15 |
+
from ultralytics import YOLO
|
| 16 |
+
from huggingface_hub import hf_hub_download
|
| 17 |
|
| 18 |
+
# Download and load model
|
| 19 |
+
model_path = hf_hub_download(repo_id="foduucom/stockmarket-pattern-detection-yolov8", filename="model.pt")
|
| 20 |
+
model = YOLO(model_path)
|
| 21 |
|
| 22 |
def detect_patterns(image):
|
| 23 |
+
try:
|
| 24 |
+
if image is None:
|
| 25 |
+
return json.dumps({"patterns": [], "error": "No image"})
|
| 26 |
+
results = model.predict(source=image, conf=0.25, iou=0.45, verbose=False)
|
| 27 |
+
patterns = []
|
| 28 |
+
for r in results:
|
| 29 |
+
if r.boxes is None:
|
| 30 |
+
continue
|
| 31 |
+
for i in range(len(r.boxes)):
|
| 32 |
+
box = r.boxes[i]
|
| 33 |
+
cls_id = int(box.cls[0])
|
| 34 |
+
conf = float(box.conf[0])
|
| 35 |
+
xyxy = box.xyxy[0].tolist()
|
| 36 |
+
label = r.names[cls_id]
|
| 37 |
+
patterns.append({
|
| 38 |
+
"label": label,
|
| 39 |
+
"confidence": round(conf, 3),
|
| 40 |
+
"bbox": [round(x, 1) for x in xyxy],
|
| 41 |
+
})
|
| 42 |
+
patterns.sort(key=lambda p: p["confidence"], reverse=True)
|
| 43 |
+
return json.dumps({"patterns": patterns, "count": len(patterns)})
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return json.dumps({"patterns": [], "error": str(e), "trace": traceback.format_exc()})
|
| 46 |
|
| 47 |
demo = gr.Interface(
|
| 48 |
fn=detect_patterns,
|
| 49 |
inputs=gr.Image(type="pil", label="Chart Image"),
|
| 50 |
outputs=gr.Textbox(label="Detected Patterns (JSON)"),
|
| 51 |
title="Chart Pattern Detection — YOLOv8",
|
|
|
|
| 52 |
)
|
| 53 |
+
demo.launch(show_error=True)
|