Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,33 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from ultralytics import YOLO
|
| 5 |
-
import shutil
|
| 6 |
-
import uuid
|
| 7 |
-
import base64
|
| 8 |
-
import cv2
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
@app.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return JSONResponse(content={"image": b64_encoded})
|
|
|
|
| 1 |
+
from fastapi import FastAPI, BackgroundTasks
|
| 2 |
+
import time
|
| 3 |
+
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
task_running = False
|
| 8 |
+
thread = None
|
| 9 |
|
| 10 |
+
def background_task():
|
| 11 |
+
while task_running:
|
| 12 |
+
print("Task running...")
|
| 13 |
+
time.sleep(2)
|
| 14 |
|
| 15 |
+
@app.get("/start")
|
| 16 |
+
def start_task():
|
| 17 |
+
global task_running, thread
|
| 18 |
+
if not task_running:
|
| 19 |
+
task_running = True
|
| 20 |
+
thread = threading.Thread(target=background_task)
|
| 21 |
+
thread.start()
|
| 22 |
+
return {"message": "Task started"}
|
| 23 |
+
else:
|
| 24 |
+
return {"message": "Task already running"}
|
| 25 |
|
| 26 |
+
@app.get("/stop")
|
| 27 |
+
def stop_task():
|
| 28 |
+
global task_running
|
| 29 |
+
if task_running:
|
| 30 |
+
task_running = False
|
| 31 |
+
return {"message": "Task stopped"}
|
| 32 |
+
else:
|
| 33 |
+
return {"message": "No task running"}
|
|
|