wop's picture
Update app.py
8eb72ef verified
raw
history blame
2.34 kB
import gradio as gr
import json
import time
status_file = "status.json"
command_file = "command.json"
# Initialize storage files
with open(status_file, "w") as f:
json.dump({}, f)
with open(command_file, "w") as f:
json.dump({}, f)
# Register PC as online
def register_pc(name, password):
with open(status_file, "r") as f:
status = json.load(f)
status[name] = {"password": password, "timestamp": time.time()}
with open(status_file, "w") as f:
json.dump(status, f)
return f"PC '{name}' registered as online."
# Check if PC is online
def check_pc(name):
with open(status_file, "r") as f:
status = json.load(f)
if name in status and time.time() - status[name]["timestamp"] < 60:
return "Online"
return "Offline"
# Send command or register if command is "REGISTER"
def send_command(name, password, command):
if command == "REGISTER":
return register_pc(name, password)
with open(status_file, "r") as f:
status = json.load(f)
if name in status and status[name]["password"] == password:
with open(command_file, "w") as f:
json.dump({"command": command}, f)
return f"Command '{command}' sent to {name}."
return "Authentication failed."
# Get command (for PC side polling)
def get_command():
with open(command_file, "r") as f:
cmd_data = json.load(f)
return cmd_data.get("command", "")
with gr.Blocks() as ui:
with gr.Row():
name_input = gr.Textbox(label="PC Name")
pass_input = gr.Textbox(label="Password", type="password")
check_btn = gr.Button("Check Online Status")
check_output = gr.Textbox(label="Status")
check_btn.click(check_pc, inputs=name_input, outputs=check_output)
with gr.Row():
cmd_input = gr.Textbox(label="Command")
send_btn = gr.Button("Send Command")
send_output = gr.Textbox(label="Response")
send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output)
# Hidden components to expose the get_command API endpoint.
hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False)
hidden_cmd_output = gr.Textbox(visible=False)
hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command")
ui.launch(share=True)