init
Browse files- Dockerfile +15 -0
- main.py +84 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.10-slim
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 13 |
+
|
| 14 |
+
COPY --chown=user . /app
|
| 15 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from aiortc import RTCPeerConnection, RTCSessionDescription
|
| 5 |
+
import json
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Enable CORS for frontend access
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # Or restrict to ["http://localhost:5173"]
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
@app.post("/signal")
|
| 19 |
+
async def signal(request: Request):
|
| 20 |
+
body = await request.json()
|
| 21 |
+
offer = body["offer"]
|
| 22 |
+
pc = RTCPeerConnection()
|
| 23 |
+
|
| 24 |
+
@pc.on("datachannel")
|
| 25 |
+
def on_datachannel(channel):
|
| 26 |
+
@channel.on("message")
|
| 27 |
+
def on_message(message):
|
| 28 |
+
rpc = json.loads(message)
|
| 29 |
+
result = handle_rpc(rpc)
|
| 30 |
+
response = json.dumps({ "jsonrpc": "2.0", "id": rpc["id"], "result": result })
|
| 31 |
+
channel.send(response)
|
| 32 |
+
|
| 33 |
+
await pc.setRemoteDescription(RTCSessionDescription(sdp=offer["sdp"], type=offer["type"]))
|
| 34 |
+
answer = await pc.createAnswer()
|
| 35 |
+
await pc.setLocalDescription(answer)
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"answer": {
|
| 39 |
+
"sdp": pc.localDescription.sdp,
|
| 40 |
+
"type": pc.localDescription.type
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def handle_rpc(rpc):
|
| 45 |
+
method = rpc.get("method")
|
| 46 |
+
params = rpc.get("params", {})
|
| 47 |
+
|
| 48 |
+
if method == "echo":
|
| 49 |
+
return params.get("message", "")
|
| 50 |
+
|
| 51 |
+
elif method == "process":
|
| 52 |
+
message = params.get("message", "").lower()
|
| 53 |
+
|
| 54 |
+
# Add
|
| 55 |
+
match = re.match(r"add (\d+) and (\d+)", message)
|
| 56 |
+
if match:
|
| 57 |
+
x, y = map(int, match.groups())
|
| 58 |
+
return x + y
|
| 59 |
+
|
| 60 |
+
# Subtract
|
| 61 |
+
match = re.match(r"subtract (\d+) from (\d+)", message)
|
| 62 |
+
if match:
|
| 63 |
+
y, x = map(int, match.groups()) # reverse order
|
| 64 |
+
return x - y
|
| 65 |
+
|
| 66 |
+
# Multiply
|
| 67 |
+
match = re.match(r"multiply (\d+) and (\d+)", message)
|
| 68 |
+
if match:
|
| 69 |
+
x, y = map(int, match.groups())
|
| 70 |
+
return x * y
|
| 71 |
+
|
| 72 |
+
# Divide
|
| 73 |
+
match = re.match(r"divide (\d+) by (\d+)", message)
|
| 74 |
+
if match:
|
| 75 |
+
x, y = map(int, match.groups())
|
| 76 |
+
return round(x / y, 2) if y != 0 else "Division by zero"
|
| 77 |
+
|
| 78 |
+
return "Unknown command"
|
| 79 |
+
|
| 80 |
+
else:
|
| 81 |
+
return "Unknown method"
|
| 82 |
+
|
| 83 |
+
# Optional: print to confirm startup
|
| 84 |
+
print("✅ FastAPI WebRTC backend is ready")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
aiortc
|
| 3 |
+
uvicorn
|