Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import psutil
|
| 4 |
-
|
| 5 |
-
try:
|
| 6 |
-
import readline
|
| 7 |
-
except ImportError:
|
| 8 |
-
try:
|
| 9 |
-
import pyreadline3 as readline
|
| 10 |
-
except ImportError:
|
| 11 |
-
readline = None # no history/autocomplete on Windows if missing
|
| 12 |
-
|
| 13 |
|
| 14 |
-
#
|
| 15 |
try:
|
| 16 |
from transformers import pipeline
|
| 17 |
ai_enabled = True
|
|
@@ -19,139 +11,88 @@ try:
|
|
| 19 |
except Exception:
|
| 20 |
ai_enabled = False
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
path = args[0] if args else "."
|
| 26 |
-
try:
|
| 27 |
-
for item in os.listdir(path):
|
| 28 |
-
print(item)
|
| 29 |
-
except FileNotFoundError:
|
| 30 |
-
print(f"ls: cannot access '{path}': No such file or directory")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def cmd_cd(args):
|
| 34 |
-
if not args:
|
| 35 |
-
print("cd: missing operand")
|
| 36 |
-
return
|
| 37 |
-
try:
|
| 38 |
-
os.chdir(args[0])
|
| 39 |
-
except FileNotFoundError:
|
| 40 |
-
print(f"cd: {args[0]}: No such file or directory")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def cmd_pwd(args):
|
| 44 |
-
print(os.getcwd())
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def cmd_mkdir(args):
|
| 48 |
-
if not args:
|
| 49 |
-
print("mkdir: missing operand")
|
| 50 |
-
return
|
| 51 |
-
try:
|
| 52 |
-
os.mkdir(args[0])
|
| 53 |
-
except FileExistsError:
|
| 54 |
-
print(f"mkdir: cannot create directory '{args[0]}': File exists")
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def cmd_rm(args):
|
| 58 |
-
if not args:
|
| 59 |
-
print("rm: missing operand")
|
| 60 |
-
return
|
| 61 |
-
target = args[0]
|
| 62 |
-
if os.path.isdir(target):
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
-
else:
|
| 68 |
-
try:
|
| 69 |
-
os.remove(target)
|
| 70 |
-
except FileNotFoundError:
|
| 71 |
-
print(f"rm: cannot remove '{target}': No such file or directory")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def cmd_monitor(args):
|
| 75 |
-
cpu = psutil.cpu_percent(interval=1)
|
| 76 |
-
mem = psutil.virtual_memory()
|
| 77 |
-
print(f"CPU Usage: {cpu}%")
|
| 78 |
-
print(f"Memory Usage: {mem.percent}%")
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
for cmd in commands.keys():
|
| 84 |
-
print(f" - {cmd}")
|
| 85 |
-
if ai_enabled:
|
| 86 |
-
print(" - ai (natural language queries)")
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
if not ai_enabled:
|
| 92 |
-
print("AI features not available (transformers not installed).")
|
| 93 |
-
return
|
| 94 |
-
if not args:
|
| 95 |
-
print("ai: missing query")
|
| 96 |
-
return
|
| 97 |
-
query = " ".join(args)
|
| 98 |
-
print(f"AI Query: {query}")
|
| 99 |
-
try:
|
| 100 |
-
result = nlp(query, max_length=100)[0]['generated_text']
|
| 101 |
-
print("AI Suggestion:", result)
|
| 102 |
-
except Exception as e:
|
| 103 |
-
print(f"AI error: {e}")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# ========== Command Map ==========
|
| 107 |
-
commands = {
|
| 108 |
-
"ls": cmd_ls,
|
| 109 |
-
"cd": cmd_cd,
|
| 110 |
-
"pwd": cmd_pwd,
|
| 111 |
-
"mkdir": cmd_mkdir,
|
| 112 |
-
"rm": cmd_rm,
|
| 113 |
-
"monitor": cmd_monitor,
|
| 114 |
-
"help": cmd_help,
|
| 115 |
-
"ai": cmd_ai,
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# ========== Autocomplete ==========
|
| 120 |
-
def completer(text, state):
|
| 121 |
-
options = [cmd for cmd in commands.keys() if cmd.startswith(text)]
|
| 122 |
-
if state < len(options):
|
| 123 |
-
return options[state]
|
| 124 |
-
return None
|
| 125 |
-
|
| 126 |
-
if readline:
|
| 127 |
-
readline.parse_and_bind("tab: complete")
|
| 128 |
-
readline.set_completer(completer)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# ========== Main Loop ==========
|
| 132 |
-
def main():
|
| 133 |
-
print("Python Terminal Emulator (type 'help' for commands, 'exit' to quit)")
|
| 134 |
-
while True:
|
| 135 |
try:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
print("Exiting terminal.")
|
| 141 |
-
break
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
else:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
print("\nUse 'exit' to quit.")
|
| 152 |
except Exception as e:
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import psutil
|
| 4 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# AI Module
|
| 7 |
try:
|
| 8 |
from transformers import pipeline
|
| 9 |
ai_enabled = True
|
|
|
|
| 11 |
except Exception:
|
| 12 |
ai_enabled = False
|
| 13 |
|
| 14 |
+
# Command Handlers
|
| 15 |
+
def run_command(cmd):
|
| 16 |
+
parts = cmd.strip().split()
|
| 17 |
+
if not parts:
|
| 18 |
+
return "No command entered."
|
| 19 |
+
c, args = parts[0], parts[1:]
|
| 20 |
|
| 21 |
+
if c == "ls":
|
| 22 |
+
path = args[0] if args else "."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
return "\n".join(os.listdir(path))
|
| 25 |
except Exception as e:
|
| 26 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
elif c == "pwd":
|
| 29 |
+
return os.getcwd()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
elif c == "cd":
|
| 32 |
+
if not args:
|
| 33 |
+
return "cd: missing operand"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
os.chdir(args[0])
|
| 36 |
+
return f"Changed directory to {os.getcwd()}"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return str(e)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
elif c == "mkdir":
|
| 41 |
+
if not args:
|
| 42 |
+
return "mkdir: missing operand"
|
| 43 |
+
try:
|
| 44 |
+
os.mkdir(args[0])
|
| 45 |
+
return f"Directory '{args[0]}' created."
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return str(e)
|
| 48 |
|
| 49 |
+
elif c == "rm":
|
| 50 |
+
if not args:
|
| 51 |
+
return "rm: missing operand"
|
| 52 |
+
target = args[0]
|
| 53 |
+
try:
|
| 54 |
+
if os.path.isdir(target):
|
| 55 |
+
shutil.rmtree(target)
|
| 56 |
else:
|
| 57 |
+
os.remove(target)
|
| 58 |
+
return f"Removed '{target}'."
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
return str(e)
|
| 61 |
+
|
| 62 |
+
elif c == "monitor":
|
| 63 |
+
cpu = psutil.cpu_percent(interval=1)
|
| 64 |
+
mem = psutil.virtual_memory()
|
| 65 |
+
return f"CPU Usage: {cpu}%\nMemory Usage: {mem.percent}%"
|
| 66 |
+
|
| 67 |
+
elif c == "ai":
|
| 68 |
+
if not ai_enabled:
|
| 69 |
+
return "AI not available."
|
| 70 |
+
if not args:
|
| 71 |
+
return "ai: missing query"
|
| 72 |
+
query = " ".join(args)
|
| 73 |
+
try:
|
| 74 |
+
result = nlp(query, max_length=100)[0]['generated_text']
|
| 75 |
+
return f"AI Suggestion: {result}"
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return str(e)
|
| 78 |
|
| 79 |
+
elif c == "help":
|
| 80 |
+
cmds = ["ls", "pwd", "cd", "mkdir", "rm", "monitor", "help"]
|
| 81 |
+
if ai_enabled:
|
| 82 |
+
cmds.append("ai")
|
| 83 |
+
return "Available commands:\n" + "\n".join(cmds)
|
| 84 |
|
| 85 |
+
else:
|
| 86 |
+
return f"{c}: command not found"
|
| 87 |
+
|
| 88 |
+
# Gradio UI
|
| 89 |
+
iface = gr.Interface(
|
| 90 |
+
fn=run_command,
|
| 91 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter command..."),
|
| 92 |
+
outputs="text",
|
| 93 |
+
title="Python Terminal Emulator",
|
| 94 |
+
description="A mini terminal emulator with optional AI commands."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
iface.launch()
|