| import os |
| import shutil |
| import psutil |
| import gradio as gr |
|
|
| |
| try: |
| from transformers import pipeline |
| ai_enabled = True |
| nlp = pipeline("text2text-generation", model="google/flan-t5-small") |
| except Exception: |
| ai_enabled = False |
|
|
| |
| 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" |
|
|
| |
| 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() |
|
|