codewithRiz commited on
Commit
9243721
·
1 Parent(s): 73b9fb2

updated dashboard

Browse files
Files changed (3) hide show
  1. app.py +176 -45
  2. buck_vs_doe_Detection_best.pt +0 -3
  3. requirements.txt +1 -4
app.py CHANGED
@@ -1,46 +1,177 @@
1
-
2
- from ultralytics import YOLO
3
  import gradio as gr
4
- import numpy as np
5
- from PIL import Image
6
-
7
- # -------------------------
8
- # Load detection model
9
- # -------------------------
10
- model = YOLO("buck_vs_doe_Detection_best.pt")
11
-
12
- # -------------------------
13
- # Inference function
14
- # -------------------------
15
- def predict(image):
16
- # Run inference (YOLO accepts numpy RGB directly)
17
- results = model(image)
18
-
19
- # Take first result (single image)
20
- r = results[0]
21
-
22
- # Plot results (BGR numpy array)
23
- im_bgr = r.plot()
24
-
25
- # Convert BGR → RGB for Gradio
26
- im_rgb = im_bgr[..., ::-1]
27
-
28
- return im_rgb
29
-
30
-
31
- # -------------------------
32
- # Gradio UI
33
- # -------------------------
34
- app = gr.Interface(
35
- fn=predict,
36
- inputs=gr.Image(type="numpy", label="Upload Image"),
37
- outputs=gr.Image(type="numpy", label="Detection Result"),
38
- title="Buck Tracker AI – Deer Detection",
39
- description="YOLO-based buck vs doe detection using Ultralytics native plotting."
40
- )
41
-
42
- # -------------------------
43
- # Launch
44
- # -------------------------
45
- if __name__ == "__main__":
46
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import list_bucket_tree, download_bucket_files
3
+ from datetime import datetime
4
+ import pandas as pd
5
+ import json
6
+ import os
7
+ from dotenv import load_dotenv
8
+
9
+ # ---------- LOAD ENV ----------
10
+ load_dotenv()
11
+ BUCKET_NAME = os.getenv("BUCKET_NAME")
12
+
13
+ PAGE_SIZE = 5
14
+
15
+
16
+ # ---------- LOAD DATA FROM BUCKET ----------
17
+ def extract_customer_data():
18
+ try:
19
+ tree = list_bucket_tree(BUCKET_NAME, recursive=True)
20
+
21
+ json_files = [
22
+ item.path for item in tree
23
+ if item.path.endswith("cameras.json")
24
+ ]
25
+
26
+ if not json_files:
27
+ return pd.DataFrame(columns=["customer_id", "customer_name", "customer_email"])
28
+
29
+ files_map = [(f, f"C:/tmp/{f.replace('/', '_')}") for f in json_files]
30
+
31
+ download_bucket_files(BUCKET_NAME, files=files_map)
32
+
33
+ rows = []
34
+
35
+ for remote_path, local_path in files_map:
36
+ parts = remote_path.split("/")
37
+ customer_id = parts[1] if len(parts) > 1 else "unknown"
38
+
39
+ try:
40
+ with open(local_path, "r", encoding="utf-8") as f:
41
+ data = json.load(f)
42
+
43
+ for item in data:
44
+ rows.append({
45
+ "customer_id": customer_id,
46
+ "customer_name": item.get("customer_name"),
47
+ "customer_email": item.get("customer_email")
48
+ })
49
+
50
+ except Exception as e:
51
+ print(f"File error {remote_path}: {e}")
52
+
53
+ df = pd.DataFrame(rows)
54
+
55
+ # ---------- REMOVE DUPLICATES ----------
56
+ if not df.empty:
57
+ df = df.drop_duplicates(
58
+ subset=["customer_id", "customer_name", "customer_email"]
59
+ )
60
+
61
+ return df
62
+
63
+ except Exception as e:
64
+ print("ERROR:", e)
65
+ return pd.DataFrame(columns=["customer_id", "customer_name", "customer_email"])
66
+
67
+
68
+ # ---------- FILTER ----------
69
+ def apply_search(df, email_query):
70
+ if df is None or not isinstance(df, pd.DataFrame):
71
+ return pd.DataFrame(columns=["customer_id", "customer_name", "customer_email"])
72
+
73
+ if email_query:
74
+ df = df[df["customer_email"].astype(str).str.contains(email_query, case=False, na=False)]
75
+
76
+ return df
77
+
78
+
79
+ # ---------- PAGINATION ----------
80
+ def build_view(df, page, email_query):
81
+ filtered = apply_search(df, email_query)
82
+
83
+ total_pages = max(1, (len(filtered) - 1) // PAGE_SIZE + 1)
84
+ page = max(0, min(page, total_pages - 1))
85
+
86
+ start = page * PAGE_SIZE
87
+ end = start + PAGE_SIZE
88
+
89
+ paged = filtered.iloc[start:end]
90
+
91
+ status = f"Page {page+1} / {total_pages} | Total Results: {len(filtered)}"
92
+
93
+ return paged, page, status
94
+
95
+
96
+ # ---------- ACTIONS ----------
97
+ def search(df, email_query):
98
+ page = 0
99
+ table, page, status = build_view(df, page, email_query)
100
+ return table, page, status, df
101
+
102
+
103
+ def next_page(df, page, email_query):
104
+ page = page + 1
105
+ table, page, status = build_view(df, page, email_query)
106
+ return table, page, status, df
107
+
108
+
109
+ def prev_page(df, page, email_query):
110
+ page = page - 1
111
+ table, page, status = build_view(df, page, email_query)
112
+ return table, page, status, df
113
+
114
+
115
+ def refresh_data(email_query):
116
+ df = extract_customer_data()
117
+ page = 0
118
+ table, page, status = build_view(df, page, email_query)
119
+ return table, page, status, df
120
+
121
+
122
+ # ---------- INITIAL DATA ----------
123
+ df_global = extract_customer_data()
124
+
125
+
126
+ # ---------- UI ----------
127
+ with gr.Blocks() as app:
128
+ gr.Markdown("# 🛰 Buck Tracker Customer Dashboard")
129
+
130
+ state_df = gr.State(df_global)
131
+ state_page = gr.State(0)
132
+
133
+ search_box = gr.Textbox(
134
+ label="🔍 Search Customer Email",
135
+ placeholder="Enter email to search..."
136
+ )
137
+
138
+ table = gr.Dataframe(
139
+ value=build_view(df_global, 0, "")[0],
140
+ interactive=False,
141
+ label="Customer Data"
142
+ )
143
+
144
+ status = gr.Textbox(interactive=False)
145
+
146
+ with gr.Row():
147
+ refresh_btn = gr.Button("🔄 Refresh Bucket Data", variant="primary")
148
+ prev_btn = gr.Button("⬅️ Previous")
149
+ next_btn = gr.Button("Next ➡️")
150
+
151
+ # ---------- EVENTS ----------
152
+
153
+ search_box.change(
154
+ fn=search,
155
+ inputs=[state_df, search_box],
156
+ outputs=[table, state_page, status, state_df]
157
+ )
158
+
159
+ refresh_btn.click(
160
+ fn=refresh_data,
161
+ inputs=[search_box],
162
+ outputs=[table, state_page, status, state_df]
163
+ )
164
+
165
+ next_btn.click(
166
+ fn=next_page,
167
+ inputs=[state_df, state_page, search_box],
168
+ outputs=[table, state_page, status, state_df]
169
+ )
170
+
171
+ prev_btn.click(
172
+ fn=prev_page,
173
+ inputs=[state_df, state_page, search_box],
174
+ outputs=[table, state_page, status, state_df]
175
+ )
176
+
177
+ app.launch()
buck_vs_doe_Detection_best.pt DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:59b80c9c56fdc835892ab2604f5b214700ad3257735d6ecfcd768dee1065b595
3
- size 22527082
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,3 @@
1
- ultralytics==8.3.240
2
  gradio
3
  Pillow
4
- numpy==1.26.4
5
- torch
6
- onnx==1.20.0
 
 
1
  gradio
2
  Pillow
3
+ numpy