Aqso commited on
Commit
bfd563a
·
verified ·
1 Parent(s): b2c9a39

Update app.py

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