Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
def run_command(command):
|
| 5 |
+
try:
|
| 6 |
+
# Run the command and capture the output
|
| 7 |
+
result = subprocess.run(
|
| 8 |
+
command,
|
| 9 |
+
shell=True,
|
| 10 |
+
capture_output=True,
|
| 11 |
+
text=True,
|
| 12 |
+
timeout=60 # Prevent hanging commands
|
| 13 |
+
)
|
| 14 |
+
# Combine stdout and stderr
|
| 15 |
+
output = result.stdout
|
| 16 |
+
if result.stderr:
|
| 17 |
+
output += "\n--- STDERR ---\n" + result.stderr
|
| 18 |
+
|
| 19 |
+
if not output.strip():
|
| 20 |
+
return "Command executed successfully with no output."
|
| 21 |
+
|
| 22 |
+
return output
|
| 23 |
+
except subprocess.TimeoutExpired:
|
| 24 |
+
return "Error: Command timed out after 60 seconds."
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error executing command: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Create the Gradio interface
|
| 29 |
+
with gr.Blocks(title="Hugging Face Container Shell") as demo:
|
| 30 |
+
gr.Markdown("# Container Shell Access")
|
| 31 |
+
gr.Markdown("Run terminal commands directly inside the Hugging Face Space container.")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
command_input = gr.Textbox(
|
| 35 |
+
label="Terminal Command",
|
| 36 |
+
placeholder="e.g., ls -la, pwd, whoami",
|
| 37 |
+
lines=1
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
run_button = gr.Button("Run Command", variant="primary")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
command_output = gr.Textbox(
|
| 45 |
+
label="Output",
|
| 46 |
+
lines=20,
|
| 47 |
+
interactive=False
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Bind the button and enter key to the function
|
| 51 |
+
run_button.click(fn=run_command, inputs=command_input, outputs=command_output)
|
| 52 |
+
command_input.submit(fn=run_command, inputs=command_input, outputs=command_output)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|