Spaces:
Sleeping

ELegendzRepo / app.py
tahsinkhondokar's picture
Update app.py
d4c4fb5 verified
Raw History Blame Contribute Delete
2.28 kB
import gradio as gr
import subprocess
import os
def check_package_status():
status_report = ""
# 1. Read your packages.txt automatically
if os.path.exists("packages.txt"):
with open("packages.txt", "r") as f:
packages = [line.strip() for line in f if line.strip()]
else:
return "⚠️ packages.txt not found!"
# 2. Loop through each package and check version
for pkg in packages:
try:
# Handle specific command names for certain packages
if pkg == "default-jdk":
cmd = "java -version"
elif pkg == "python3-pip":
cmd = "pip --version"
else:
cmd = f"{pkg} --version"
# Execute and capture output
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
v_info = output.decode('utf-8').split('\n')[0]
status_report += f"✅ **{pkg}**: {v_info}\n\n"
except Exception:
status_report += f"❌ **{pkg}**: Not found or check failed.\n\n"
return status_report
def run_universal_command(command):
try:
# Runs any code: Python, Node, Java, Git, FFmpeg...
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
return result.decode('utf-8')
except subprocess.CalledProcessError as e:
return f"Error Output:\n{e.output.decode('utf-8')}"
# --- Build the UI ---
with gr.Blocks() as demo:
gr.Markdown("# Virtual Terminal and Universal Script Executor")
with gr.Accordion("📦 System Package Status", open=True):
status_box = gr.Markdown("Checking system packages...")
with gr.Tab("Terminal"):
cmd_input = gr.Textbox(label="Enter Command", placeholder="node -v OR python my_script.py")
run_btn = gr.Button("Execute", variant="primary")
# Changed 'bash' to 'shell' to fix the Gradio 6.0 error
output_box = gr.Code(label="Console Output", language="shell")
# Connect the logic
demo.load(fn=check_package_status, outputs=status_box)
run_btn.click(fn=run_universal_command, inputs=cmd_input, outputs=output_box)
# Moved theme here to fix the UserWarning
demo.launch(theme=gr.themes.Soft())