File size: 1,974 Bytes
6860273 e5ebe55 6860273 e5ebe55 6860273 e5ebe55 6860273 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import gradio as gr
import subprocess
import os
# Default working directory
current_dir = "/tmp" # Temp folder yang bisa ditulis
def run_command(cmd):
global current_dir
try:
# Split command buat cek kalau ada "cd"
parts = cmd.strip().split("&&")
for part in parts:
part = part.strip()
if part.startswith("cd "):
# Handle cd command
path = part[3:].strip()
if path == "~":
current_dir = os.path.expanduser("~")
elif path.startswith("/"):
current_dir = path
else:
current_dir = os.path.abspath(os.path.join(current_dir, path))
else:
# Jalankan command lain
result = subprocess.run(
part,
shell=True,
capture_output=True,
text=True,
cwd=current_dir,
timeout=300 # timeout 5 menit
)
output = result.stdout + "\n" + result.stderr
if result.returncode != 0:
return f"❌ Gagal di command: `{part}`\n\n{output}"
return f"✅ Berhasil!\n\n{output}"
except subprocess.TimeoutExpired:
return "⏰ Timeout, command terlalu lama!"
except Exception as e:
return f"⚠️ Error: {str(e)}"
# Interface Gradio
with gr.Blocks() as demo:
gr.Markdown("## **Terminal Web (V2)** \nJalanin perintah bash langsung di server!")
with gr.Row():
cmd_input = gr.Textbox(label="Command", placeholder="Contoh: git clone https://github.com/user/botwa.git", scale=7)
submit_btn = gr.Button("Jalankan", scale=1)
output = gr.Textbox(label="Output", lines=20)
submit_btn.click(run_command, inputs=cmd_input, outputs=output)
cmd_input.submit(run_command, inputs=cmd_input, outputs=output)
demo.launch() |