Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,121 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
-
from tasks import
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
# Load environment variables
|
6 |
load_dotenv()
|
7 |
|
|
|
|
|
|
|
|
|
8 |
app = FastAPI(
|
9 |
-
title="
|
10 |
description="API for the Frugal AI Challenge evaluation endpoints"
|
11 |
)
|
12 |
-
|
13 |
-
# Include all routers
|
14 |
app.include_router(image.router)
|
15 |
|
|
|
16 |
@app.get("/")
|
17 |
async def root():
|
18 |
return {
|
19 |
-
"message": "
|
20 |
"endpoints": {
|
21 |
-
"
|
|
|
22 |
}
|
23 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
3 |
from dotenv import load_dotenv
|
4 |
+
from tasks import image
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
7 |
+
import os
|
8 |
+
from huggingface_hub import HfApi
|
9 |
+
|
10 |
|
|
|
11 |
load_dotenv()
|
12 |
|
13 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
14 |
+
api = HfApi(token=HF_TOKEN)
|
15 |
+
|
16 |
+
# FastAPI app
|
17 |
app = FastAPI(
|
18 |
+
title="Frugal AI Challenge API",
|
19 |
description="API for the Frugal AI Challenge evaluation endpoints"
|
20 |
)
|
|
|
|
|
21 |
app.include_router(image.router)
|
22 |
|
23 |
+
|
24 |
@app.get("/")
|
25 |
async def root():
|
26 |
return {
|
27 |
+
"message": "Wildfire Smoke Detector",
|
28 |
"endpoints": {
|
29 |
+
"dataset evaluation": "/image",
|
30 |
+
"single image detection": "/detect-smoke"
|
31 |
}
|
32 |
+
}
|
33 |
+
|
34 |
+
# ---------------------
|
35 |
+
# Gradio integration
|
36 |
+
# ---------------------
|
37 |
+
DEFAULT_PARAMS = {
|
38 |
+
"image": {
|
39 |
+
"dataset_name": "pyronear/pyro-sdis", # Replace with your actual HF dataset
|
40 |
+
"test_size": 0.2,
|
41 |
+
"test_seed": 42
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
def evaluate_model(task: str, space_url: str):
|
46 |
+
if "localhost" in space_url:
|
47 |
+
api_url = f"{space_url}/{task}"
|
48 |
+
else:
|
49 |
+
try:
|
50 |
+
# Assume Hugging Face space URL logic
|
51 |
+
info_space = api.space_info(repo_id=space_url)
|
52 |
+
host = info_space.host
|
53 |
+
api_url = f"{host}/{task}"
|
54 |
+
except:
|
55 |
+
return None, None, None, f"Space '{space_url}' not found"
|
56 |
+
|
57 |
+
try:
|
58 |
+
params = DEFAULT_PARAMS[task]
|
59 |
+
response = requests.post(api_url, json=params)
|
60 |
+
if response.status_code != 200:
|
61 |
+
return None, None, None, f"API call failed with status {response.status_code}"
|
62 |
+
|
63 |
+
results = response.json()
|
64 |
+
accuracy = results.get("classification_accuracy", results.get("accuracy", 0))
|
65 |
+
emissions = results.get("emissions_gco2eq", 0)
|
66 |
+
energy = results.get("energy_consumed_wh", 0)
|
67 |
+
return accuracy, emissions, energy, results
|
68 |
+
|
69 |
+
except Exception as e:
|
70 |
+
return None, None, None, str(e)
|
71 |
+
|
72 |
+
def evaluate_single_image(image_path, space_url):
|
73 |
+
api_url = f"{space_url}/detect-smoke"
|
74 |
+
with open(image_path, "rb") as f:
|
75 |
+
files = {"file": f}
|
76 |
+
response = requests.post(api_url, files=files)
|
77 |
+
|
78 |
+
if response.status_code != 200:
|
79 |
+
return f"Error: {response.status_code}", None
|
80 |
+
|
81 |
+
result = response.json()
|
82 |
+
msg = "✅ Smoke detected" if result["smoke_detected"] else "❌ No smoke"
|
83 |
+
return msg, result
|
84 |
+
|
85 |
+
# Gradio UI
|
86 |
+
with gr.Blocks(title="Frugal AI Challenge") as demo:
|
87 |
+
gr.Markdown("# 🌲 Wildfire Smoke Detector")
|
88 |
+
|
89 |
+
with gr.Tab("Evaluate Dataset Model"):
|
90 |
+
text_space_url = gr.Textbox(placeholder="username/your-space", label="API Base URL")
|
91 |
+
text_route = gr.Textbox(value="image", label="Route Name")
|
92 |
+
text_evaluate_btn = gr.Button("Evaluate Model")
|
93 |
+
text_accuracy = gr.Textbox(label="Accuracy")
|
94 |
+
text_emissions = gr.Textbox(label="Emissions (gCO2eq)")
|
95 |
+
text_energy = gr.Textbox(label="Energy (Wh)")
|
96 |
+
text_results_json = gr.JSON(label="Full Results")
|
97 |
+
|
98 |
+
text_evaluate_btn.click(
|
99 |
+
lambda url, route: evaluate_model(route.strip("/"), url),
|
100 |
+
inputs=[text_space_url, text_route],
|
101 |
+
outputs=[text_accuracy, text_emissions, text_energy, text_results_json],
|
102 |
+
concurrency_limit=5,
|
103 |
+
concurrency_id="eval_queue"
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.Tab("Single Image Detection"):
|
107 |
+
detect_url = gr.Textbox(placeholder="username/your-space",label="API Base URL")
|
108 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
109 |
+
detect_button = gr.Button("Detect Smoke")
|
110 |
+
detect_result = gr.Textbox(label="Detection Result")
|
111 |
+
detect_json = gr.JSON(label="Raw Response")
|
112 |
+
|
113 |
+
detect_button.click(
|
114 |
+
evaluate_single_image,
|
115 |
+
inputs=[image_input, detect_url],
|
116 |
+
outputs=[detect_result, detect_json]
|
117 |
+
)
|
118 |
+
|
119 |
+
# Mount Gradio to FastAPI
|
120 |
+
app.mount("/gradio", WSGIMiddleware(demo))
|
121 |
+
|