Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
if __name__ == "__main__":
|
| 2 |
child_pid, fd = pty.fork()
|
| 3 |
if child_pid == 0:
|
| 4 |
-
# This runs inside the Ubuntu shell
|
| 5 |
os.environ["TERM"] = "xterm-256color"
|
| 6 |
subprocess.run(["bash"])
|
| 7 |
else:
|
| 8 |
-
|
| 9 |
-
socketio.
|
| 10 |
-
socketio.run(app, host="0.0.0.0", port=7860, allow_unsafe_werkzeug=True)
|
|
|
|
| 1 |
+
import os, pty, subprocess, select, requests, eventlet
|
| 2 |
+
eventlet.monkey_patch() # This fixes the "Runtime Error" crash
|
| 3 |
+
from flask import Flask, render_template_string
|
| 4 |
+
from flask_socketio import SocketIO
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
|
| 8 |
+
fd = None
|
| 9 |
+
|
| 10 |
+
# Terminal UI with Connection Fix
|
| 11 |
+
html_code = """
|
| 12 |
+
<!DOCTYPE html>
|
| 13 |
+
<html>
|
| 14 |
+
<head>
|
| 15 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net" />
|
| 16 |
+
<script src="https://cdn.jsdelivr.net"></script>
|
| 17 |
+
<script src="https://cdnjs.cloudflare.com"></script>
|
| 18 |
+
</head>
|
| 19 |
+
<body style="background: #000; margin: 0;"><div id="terminal" style="height: 100vh;"></div>
|
| 20 |
+
<script>
|
| 21 |
+
const term = new Terminal({ cursorBlink: true, theme: { background: '#1e1e1e' } });
|
| 22 |
+
const socket = io({ transports: ['websocket', 'polling'] });
|
| 23 |
+
term.open(document.getElementById('terminal'));
|
| 24 |
+
term.onData(data => socket.emit("pty-input", { input: data }));
|
| 25 |
+
socket.on("pty-output", data => term.write(data.output));
|
| 26 |
+
</script>
|
| 27 |
+
</body></html>"""
|
| 28 |
+
|
| 29 |
+
def suggest_fix(error_msg):
|
| 30 |
+
api_key = os.getenv("NVIDIA_API_KEY")
|
| 31 |
+
if not api_key: return
|
| 32 |
+
try:
|
| 33 |
+
r = requests.post("https://integrate.api.nvidia.com",
|
| 34 |
+
headers={"Authorization": f"Bearer {api_key}"},
|
| 35 |
+
json={"model": "moonshotai/kimi-k2.5", "messages": [{"role": "user", "content": f"Ubuntu error: '{error_msg}'. Give a 1-line fix."}]})
|
| 36 |
+
fix = r.json()['choices'][0]['message']['content']
|
| 37 |
+
socketio.emit("pty-output", {"output": f"\\r\\n\\033[1;33mAI Fix: {fix}\\033[0m\\r\\n"})
|
| 38 |
+
except: pass
|
| 39 |
+
|
| 40 |
+
def read_pty():
|
| 41 |
+
global fd
|
| 42 |
+
while True:
|
| 43 |
+
socketio.sleep(0.01)
|
| 44 |
+
if fd and select.select([fd], [], [], 0)[0]:
|
| 45 |
+
out = os.read(fd, 1024).decode(errors='ignore')
|
| 46 |
+
socketio.emit("pty-output", {"output": out})
|
| 47 |
+
if "not found" in out.lower(): socketio.start_background_task(suggest_fix, out)
|
| 48 |
+
|
| 49 |
+
@socketio.on("pty-input")
|
| 50 |
+
def on_input(data):
|
| 51 |
+
if fd: os.write(fd, data["input"].encode())
|
| 52 |
+
|
| 53 |
+
@app.route("/")
|
| 54 |
+
def index(): return render_template_string(html_code)
|
| 55 |
+
|
| 56 |
if __name__ == "__main__":
|
| 57 |
child_pid, fd = pty.fork()
|
| 58 |
if child_pid == 0:
|
|
|
|
| 59 |
os.environ["TERM"] = "xterm-256color"
|
| 60 |
subprocess.run(["bash"])
|
| 61 |
else:
|
| 62 |
+
socketio.start_background_task(read_pty)
|
| 63 |
+
socketio.run(app, host="0.0.0.0", port=7860, log_output=True)
|
|
|