| import gradio as gr |
| import subprocess |
| import os |
|
|
| |
| 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)}" |
|
|
| |
| 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() |