Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,52 +1,75 @@
|
|
| 1 |
-
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
import uvicorn
|
| 4 |
import asyncio
|
| 5 |
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
viewers = set()
|
| 11 |
|
| 12 |
-
|
| 13 |
@app.websocket("/ws/client")
|
| 14 |
async def client_endpoint(websocket: WebSocket):
|
| 15 |
-
global
|
| 16 |
await websocket.accept()
|
| 17 |
try:
|
| 18 |
while True:
|
| 19 |
-
data
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
if viewers:
|
| 22 |
await asyncio.gather(
|
| 23 |
-
*[viewer.
|
| 24 |
return_exceptions=True
|
| 25 |
)
|
| 26 |
except WebSocketDisconnect:
|
| 27 |
print("Client disconnected")
|
| 28 |
|
| 29 |
-
|
| 30 |
@app.websocket("/ws/admin")
|
| 31 |
async def admin_endpoint(websocket: WebSocket):
|
| 32 |
await websocket.accept()
|
| 33 |
viewers.add(websocket)
|
| 34 |
try:
|
| 35 |
-
if
|
| 36 |
-
await websocket.
|
| 37 |
while True:
|
|
|
|
| 38 |
await websocket.receive_text()
|
| 39 |
except WebSocketDisconnect:
|
| 40 |
viewers.discard(websocket)
|
| 41 |
print("Admin disconnected")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@app.get("/")
|
| 45 |
async def get():
|
| 46 |
with open("index.html", "r") as f:
|
| 47 |
return HTMLResponse(f.read())
|
| 48 |
|
| 49 |
-
|
| 50 |
if __name__ == "__main__":
|
| 51 |
port = int(os.environ.get("PORT", 7860))
|
| 52 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse, Response
|
| 3 |
import uvicorn
|
| 4 |
import asyncio
|
| 5 |
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# In-memory binary storage for performance
|
| 10 |
+
latest_frame_bytes = b""
|
| 11 |
viewers = set()
|
| 12 |
|
|
|
|
| 13 |
@app.websocket("/ws/client")
|
| 14 |
async def client_endpoint(websocket: WebSocket):
|
| 15 |
+
global latest_frame_bytes
|
| 16 |
await websocket.accept()
|
| 17 |
try:
|
| 18 |
while True:
|
| 19 |
+
# Receive image data as binary bytes (faster than base64)
|
| 20 |
+
data = await websocket.receive_bytes()
|
| 21 |
+
latest_frame_bytes = data
|
| 22 |
+
# Broadcast binary to all connected viewers
|
| 23 |
if viewers:
|
| 24 |
await asyncio.gather(
|
| 25 |
+
*[viewer.send_bytes(data) for viewer in viewers],
|
| 26 |
return_exceptions=True
|
| 27 |
)
|
| 28 |
except WebSocketDisconnect:
|
| 29 |
print("Client disconnected")
|
| 30 |
|
|
|
|
| 31 |
@app.websocket("/ws/admin")
|
| 32 |
async def admin_endpoint(websocket: WebSocket):
|
| 33 |
await websocket.accept()
|
| 34 |
viewers.add(websocket)
|
| 35 |
try:
|
| 36 |
+
if latest_frame_bytes:
|
| 37 |
+
await websocket.send_bytes(latest_frame_bytes)
|
| 38 |
while True:
|
| 39 |
+
# Just to keep the connection alive
|
| 40 |
await websocket.receive_text()
|
| 41 |
except WebSocketDisconnect:
|
| 42 |
viewers.discard(websocket)
|
| 43 |
print("Admin disconnected")
|
| 44 |
|
| 45 |
+
# --- REST API Endpoints ---
|
| 46 |
+
|
| 47 |
+
@app.post("/api/upload")
|
| 48 |
+
async def upload_frame(request: Request):
|
| 49 |
+
"""API endpoint to upload a frame via POST request"""
|
| 50 |
+
global latest_frame_bytes
|
| 51 |
+
data = await request.body()
|
| 52 |
+
latest_frame_bytes = data
|
| 53 |
+
if viewers:
|
| 54 |
+
for viewer in viewers:
|
| 55 |
+
try:
|
| 56 |
+
await viewer.send_bytes(data)
|
| 57 |
+
except:
|
| 58 |
+
pass
|
| 59 |
+
return {"status": "success"}
|
| 60 |
+
|
| 61 |
+
@app.get("/api/view")
|
| 62 |
+
async def view_frame():
|
| 63 |
+
"""API endpoint to view the latest frame as a direct image"""
|
| 64 |
+
if latest_frame_bytes:
|
| 65 |
+
return Response(content=latest_frame_bytes, media_type="image/jpeg")
|
| 66 |
+
return {"error": "no frame available"}
|
| 67 |
|
| 68 |
@app.get("/")
|
| 69 |
async def get():
|
| 70 |
with open("index.html", "r") as f:
|
| 71 |
return HTMLResponse(f.read())
|
| 72 |
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
port = int(os.environ.get("PORT", 7860))
|
| 75 |
uvicorn.run(app, host="0.0.0.0", port=port)
|