Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, WebSocket
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
+
from fastapi.responses import HTMLResponse
|
4 |
+
import subprocess
|
5 |
+
import asyncio
|
6 |
+
import os
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Mount static files
|
11 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
12 |
+
|
13 |
+
# HTML endpoint
|
14 |
+
@app.get("/", response_class=HTMLResponse)
|
15 |
+
async def read_root():
|
16 |
+
with open("static/index.html") as f:
|
17 |
+
return f.read()
|
18 |
+
|
19 |
+
# WebSocket for emulator interaction
|
20 |
+
@app.websocket("/ws")
|
21 |
+
async def websocket_endpoint(websocket: WebSocket):
|
22 |
+
await websocket.accept()
|
23 |
+
|
24 |
+
# Start emulator in headless mode
|
25 |
+
emulator_process = subprocess.Popen(
|
26 |
+
[
|
27 |
+
"bash", "-c",
|
28 |
+
"cd /opt/android-sdk/emulator && ./emulator -avd test -no-window -no-audio -gpu swiftshader_indirect -no-snapshot -qemu -vnc :0"
|
29 |
+
],
|
30 |
+
stdout=subprocess.PIPE,
|
31 |
+
stderr=subprocess.PIPE
|
32 |
+
)
|
33 |
+
|
34 |
+
# Start VNC to WebSocket proxy
|
35 |
+
vnc_proxy = subprocess.Popen(
|
36 |
+
["websockify", "6080", "localhost:5900"],
|
37 |
+
stdout=subprocess.PIPE,
|
38 |
+
stderr=subprocess.PIPE
|
39 |
+
)
|
40 |
+
|
41 |
+
try:
|
42 |
+
while True:
|
43 |
+
data = await websocket.receive_text()
|
44 |
+
# Handle input commands if needed
|
45 |
+
await websocket.send_text("Emulator is running at /vnc.html")
|
46 |
+
except Exception as e:
|
47 |
+
print(f"WebSocket error: {e}")
|
48 |
+
finally:
|
49 |
+
emulator_process.terminate()
|
50 |
+
vnc_proxy.terminate()
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
import uvicorn
|
54 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|