import asyncio, gradio as gr
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
# WebSocket terminal
@app.websocket("/ws")
async def ws_terminal(ws: WebSocket):
await ws.accept()
await ws.send_text("Connected to terminal\n")
try:
while True:
cmd = await ws.receive_text()
proc = await asyncio.create_subprocess_shell(cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
out, err = await proc.communicate()
msg = (out + err).decode()
await ws.send_text(msg)
except WebSocketDisconnect:
print("Client disconnected")
# Gradio UI
JS = """
"""
with gr.Blocks() as demo:
gr.Markdown("## 🖥️ Interactive Terminal")
gr.HTML(JS)
# Launch Gradio on port 7861
gr_app = gr.mount_gradio_app(app, demo, path="/gradio", server_port=7861)