File size: 2,556 Bytes
1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d 1243cd0 075292d |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import os
import shutil
import psutil
import gradio as gr
# AI Module
try:
from transformers import pipeline
ai_enabled = True
nlp = pipeline("text2text-generation", model="google/flan-t5-small")
except Exception:
ai_enabled = False
# Command Handlers
def run_command(cmd):
parts = cmd.strip().split()
if not parts:
return "No command entered."
c, args = parts[0], parts[1:]
if c == "ls":
path = args[0] if args else "."
try:
return "\n".join(os.listdir(path))
except Exception as e:
return str(e)
elif c == "pwd":
return os.getcwd()
elif c == "cd":
if not args:
return "cd: missing operand"
try:
os.chdir(args[0])
return f"Changed directory to {os.getcwd()}"
except Exception as e:
return str(e)
elif c == "mkdir":
if not args:
return "mkdir: missing operand"
try:
os.mkdir(args[0])
return f"Directory '{args[0]}' created."
except Exception as e:
return str(e)
elif c == "rm":
if not args:
return "rm: missing operand"
target = args[0]
try:
if os.path.isdir(target):
shutil.rmtree(target)
else:
os.remove(target)
return f"Removed '{target}'."
except Exception as e:
return str(e)
elif c == "monitor":
cpu = psutil.cpu_percent(interval=1)
mem = psutil.virtual_memory()
return f"CPU Usage: {cpu}%\nMemory Usage: {mem.percent}%"
elif c == "ai":
if not ai_enabled:
return "AI not available."
if not args:
return "ai: missing query"
query = " ".join(args)
try:
result = nlp(query, max_length=100)[0]['generated_text']
return f"AI Suggestion: {result}"
except Exception as e:
return str(e)
elif c == "help":
cmds = ["ls", "pwd", "cd", "mkdir", "rm", "monitor", "help"]
if ai_enabled:
cmds.append("ai")
return "Available commands:\n" + "\n".join(cmds)
else:
return f"{c}: command not found"
# Gradio UI
iface = gr.Interface(
fn=run_command,
inputs=gr.Textbox(lines=2, placeholder="Enter command..."),
outputs="text",
title="Python Terminal Emulator",
description="A mini terminal emulator with optional AI commands."
)
if __name__ == "__main__":
iface.launch()
|