Spaces:
Sleeping
Sleeping
File size: 2,335 Bytes
6fb06ea 2e33767 6fb06ea 2e33767 6fb06ea 2e33767 6fb06ea 8eb72ef 6fb06ea |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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)
|