File size: 1,674 Bytes
2536afb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import subprocess

def run_command(command):
    try:
        # Run the command and capture the output
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
            timeout=60 # Prevent hanging commands
        )
        # Combine stdout and stderr
        output = result.stdout
        if result.stderr:
            output += "\n--- STDERR ---\n" + result.stderr

        if not output.strip():
            return "Command executed successfully with no output."

        return output
    except subprocess.TimeoutExpired:
        return "Error: Command timed out after 60 seconds."
    except Exception as e:
        return f"Error executing command: {str(e)}"

# Create the Gradio interface
with gr.Blocks(title="Hugging Face Container Shell") as demo:
    gr.Markdown("# Container Shell Access")
    gr.Markdown("Run terminal commands directly inside the Hugging Face Space container.")

    with gr.Row():
        command_input = gr.Textbox(
            label="Terminal Command",
            placeholder="e.g., ls -la, pwd, whoami",
            lines=1
        )

    with gr.Row():
        run_button = gr.Button("Run Command", variant="primary")

    with gr.Row():
        command_output = gr.Textbox(
            label="Output",
            lines=20,
            interactive=False
        )

    # Bind the button and enter key to the function
    run_button.click(fn=run_command, inputs=command_input, outputs=command_output)
    command_input.submit(fn=run_command, inputs=command_input, outputs=command_output)

if __name__ == "__main__":
    demo.launch()