Spaces:
Build error
Build error
Commit ·
8ba1b51
1
Parent(s): c04146c
Add token authorisation
Browse files- app.py +41 -12
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -5,8 +5,29 @@ import numpy as np
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
|
|
|
|
|
|
|
|
|
| 8 |
from megadetector.detection import run_detector
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model = run_detector.load_detector('MDV5A')
|
| 11 |
|
| 12 |
# CVAT categories - customize based on your model's classes
|
|
@@ -46,10 +67,19 @@ def process_predictions(outputs, image, confidence_threshold=0.5):
|
|
| 46 |
|
| 47 |
return results
|
| 48 |
|
| 49 |
-
def predict(image_data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
#
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
if isinstance(image_data, Image.Image):
|
| 54 |
image = image_data
|
| 55 |
elif isinstance(image_data, str) and image_data.startswith("data:image"):
|
|
@@ -62,7 +92,6 @@ def predict(image_data):
|
|
| 62 |
image = Image.open(image_data)
|
| 63 |
|
| 64 |
# Process image with model
|
| 65 |
-
|
| 66 |
outputs = model.generate_detections_one_image(image)
|
| 67 |
|
| 68 |
# Process predictions
|
|
@@ -76,7 +105,8 @@ def predict(image_data):
|
|
| 76 |
|
| 77 |
# Create Gradio interface for testing
|
| 78 |
def gradio_interface(image):
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
# Draw bounding boxes on image for visualization
|
| 82 |
img_draw = image.copy()
|
|
@@ -89,17 +119,16 @@ def gradio_interface(image):
|
|
| 89 |
|
| 90 |
return img_draw, json.dumps(results, indent=2)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
# 1. A REST API endpoint for CVAT
|
| 94 |
-
# 2. A user interface for testing
|
| 95 |
-
|
| 96 |
-
# REST API for CVAT
|
| 97 |
app = gr.Interface(
|
| 98 |
fn=predict,
|
| 99 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 100 |
outputs="json",
|
| 101 |
title="Object Detection API for CVAT",
|
| 102 |
-
description="Upload an image to get object detection predictions in CVAT-compatible format",
|
| 103 |
flagging_mode="never",
|
| 104 |
)
|
| 105 |
|
|
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
+
import os
|
| 9 |
+
import secrets
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
from megadetector.detection import run_detector
|
| 12 |
|
| 13 |
+
# Load environment variables for configuration
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Access token configuration
|
| 17 |
+
# You can set a fixed token in your Space's environment variables
|
| 18 |
+
# or generate a random one on startup (less secure)
|
| 19 |
+
API_TOKEN = os.getenv("API_TOKEN")
|
| 20 |
+
if not API_TOKEN:
|
| 21 |
+
# Generate a random token if not provided - will change on restart!
|
| 22 |
+
API_TOKEN = secrets.token_hex(16)
|
| 23 |
+
print(f"Generated API token: {API_TOKEN}")
|
| 24 |
+
print("IMPORTANT: This token will change if the space restarts!")
|
| 25 |
+
print("Set a permanent token in the Space's environment variables.")
|
| 26 |
+
|
| 27 |
+
def validate_token(token):
|
| 28 |
+
"""Validate the provided access token"""
|
| 29 |
+
return token == API_TOKEN
|
| 30 |
+
|
| 31 |
model = run_detector.load_detector('MDV5A')
|
| 32 |
|
| 33 |
# CVAT categories - customize based on your model's classes
|
|
|
|
| 67 |
|
| 68 |
return results
|
| 69 |
|
| 70 |
+
def predict(image_data, token=None):
|
| 71 |
+
"""Main prediction function for API endpoint
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
image_data: The image to be processed
|
| 75 |
+
token: Access token for authentication
|
| 76 |
+
"""
|
| 77 |
try:
|
| 78 |
+
# Validate access token
|
| 79 |
+
if token is None or not validate_token(token):
|
| 80 |
+
return {"error": "Authentication failed. Invalid or missing access token."}
|
| 81 |
+
|
| 82 |
+
# Handle various image input formats
|
| 83 |
if isinstance(image_data, Image.Image):
|
| 84 |
image = image_data
|
| 85 |
elif isinstance(image_data, str) and image_data.startswith("data:image"):
|
|
|
|
| 92 |
image = Image.open(image_data)
|
| 93 |
|
| 94 |
# Process image with model
|
|
|
|
| 95 |
outputs = model.generate_detections_one_image(image)
|
| 96 |
|
| 97 |
# Process predictions
|
|
|
|
| 105 |
|
| 106 |
# Create Gradio interface for testing
|
| 107 |
def gradio_interface(image):
|
| 108 |
+
# For the demo interface, we'll automatically pass the token
|
| 109 |
+
results = predict(image, API_TOKEN)
|
| 110 |
|
| 111 |
# Draw bounding boxes on image for visualization
|
| 112 |
img_draw = image.copy()
|
|
|
|
| 119 |
|
| 120 |
return img_draw, json.dumps(results, indent=2)
|
| 121 |
|
| 122 |
+
# API endpoint for CVAT
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
app = gr.Interface(
|
| 124 |
fn=predict,
|
| 125 |
+
inputs=[
|
| 126 |
+
gr.Image(type="filepath"),
|
| 127 |
+
gr.Textbox(label="Access Token", type="password")
|
| 128 |
+
],
|
| 129 |
outputs="json",
|
| 130 |
title="Object Detection API for CVAT",
|
| 131 |
+
description=f"Upload an image to get object detection predictions in CVAT-compatible format. Requires access token.",
|
| 132 |
flagging_mode="never",
|
| 133 |
)
|
| 134 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
megadetector
|
| 2 |
pillow
|
| 3 |
gradio
|
| 4 |
-
numpy
|
|
|
|
|
|
| 1 |
megadetector
|
| 2 |
pillow
|
| 3 |
gradio
|
| 4 |
+
numpy
|
| 5 |
+
python-dotenv
|