aistudio_TTS / activate_port.py
Qi28's picture
Update activate_port.py
64aadcd verified
import os
import subprocess
import json
import sys
class Config:
args = {
"仅激活端口": {
"deploy_path": "",
"code": "",
"port": 9880,
"type": ""
}
}
class Func:
@staticmethod
def register_port(*ports):
os.makedirs(".webide", exist_ok=True)
path = os.path.join(".webide", "proxy_config.json")
data = Func.load_json(path)
if "gradio" not in data:
data["gradio"] = {}
for port in ports:
data["gradio"][str(port)] = int(port)
Func.write_json(path, data)
@staticmethod
def load_json(path):
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as file:
return json.load(file)
return {}
@staticmethod
def write_json(path, data):
with open(path, "w") as file:
json.dump(data, file, ensure_ascii=False)
def activate_port(port, env=None):
try:
Func.register_port(port)
print(f'访问 /gradio/{port}')
except Exception as e:
print(f"激活端口 {port} 失败: {e}")
if __name__ == "__main__":
env = os.environ.copy()
ports = [int(arg) for arg in sys.argv[1:]]
for port in ports:
activate_port(port, env)