Spaces:
Build error
Build error
from fastapi import FastAPI, Request, WebSocket | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.responses import HTMLResponse | |
import subprocess | |
import asyncio | |
import os | |
app = FastAPI() | |
# Mount static files | |
app.mount("/static", StaticFiles(directory="static"), name="static") | |
# HTML endpoint | |
async def read_root(): | |
with open("static/index.html") as f: | |
return f.read() | |
# WebSocket for emulator interaction | |
async def websocket_endpoint(websocket: WebSocket): | |
await websocket.accept() | |
# Start emulator in headless mode | |
emulator_process = subprocess.Popen( | |
[ | |
"bash", "-c", | |
"cd /opt/android-sdk/emulator && ./emulator -avd test -no-window -no-audio -gpu swiftshader_indirect -no-snapshot -qemu -vnc :0" | |
], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE | |
) | |
# Start VNC to WebSocket proxy | |
vnc_proxy = subprocess.Popen( | |
["websockify", "6080", "localhost:5900"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE | |
) | |
try: | |
while True: | |
data = await websocket.receive_text() | |
# Handle input commands if needed | |
await websocket.send_text("Emulator is running at /vnc.html") | |
except Exception as e: | |
print(f"WebSocket error: {e}") | |
finally: | |
emulator_process.terminate() | |
vnc_proxy.terminate() | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) |