Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
fe717e9
1
Parent(s):
8961298
add react
Browse files- requirements.txt +1 -0
- requirements_gpu.txt +1 -0
- run_server.py +40 -19
requirements.txt
CHANGED
|
@@ -52,3 +52,4 @@ fastapi>=0.104.0
|
|
| 52 |
uvicorn>=0.23.0
|
| 53 |
python-multipart>=0.0.6
|
| 54 |
pyngrok>=7.0.0
|
|
|
|
|
|
| 52 |
uvicorn>=0.23.0
|
| 53 |
python-multipart>=0.0.6
|
| 54 |
pyngrok>=7.0.0
|
| 55 |
+
cloudflared
|
requirements_gpu.txt
CHANGED
|
@@ -59,3 +59,4 @@ fastapi>=0.104.0
|
|
| 59 |
uvicorn>=0.23.0
|
| 60 |
python-multipart>=0.0.6
|
| 61 |
pyngrok>=7.0.0
|
|
|
|
|
|
| 59 |
uvicorn>=0.23.0
|
| 60 |
python-multipart>=0.0.6
|
| 61 |
pyngrok>=7.0.0
|
| 62 |
+
cloudflared
|
run_server.py
CHANGED
|
@@ -8,6 +8,8 @@ import sys
|
|
| 8 |
import subprocess
|
| 9 |
import time
|
| 10 |
import threading
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def install_ngrok():
|
| 13 |
"""安装 pyngrok"""
|
|
@@ -20,41 +22,59 @@ def run_server():
|
|
| 20 |
subprocess.Popen([sys.executable, "server.py"])
|
| 21 |
|
| 22 |
def start_ngrok():
|
| 23 |
-
"""启动 ngrok 穿透"""
|
| 24 |
try:
|
| 25 |
from pyngrok import ngrok
|
| 26 |
-
|
| 27 |
-
# 尝试读取 token
|
| 28 |
token = os.environ.get("NGROK_AUTHTOKEN")
|
| 29 |
if not token:
|
| 30 |
print("\n⚠️ 警告: 未设置 NGROK_AUTHTOKEN 环境变量")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# token = input("请输入 ngrok authtoken (可选): ")
|
| 34 |
-
|
| 35 |
-
if token:
|
| 36 |
-
ngrok.set_auth_token(token)
|
| 37 |
-
|
| 38 |
-
# 建立隧道
|
| 39 |
public_url = ngrok.connect(8000).public_url
|
| 40 |
-
|
| 41 |
print("\n" + "="*60)
|
| 42 |
-
print(
|
| 43 |
print(f"👉 {public_url}")
|
| 44 |
print("="*60 + "\n")
|
| 45 |
-
|
| 46 |
-
# 保持运行
|
| 47 |
try:
|
| 48 |
while True:
|
| 49 |
time.sleep(1)
|
| 50 |
except KeyboardInterrupt:
|
| 51 |
-
print("正在关闭...")
|
| 52 |
ngrok.kill()
|
| 53 |
-
|
| 54 |
except ImportError:
|
| 55 |
print("❌ pyngrok 导入失败,请确保已安装")
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
print(f"❌ ngrok 启动失败: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
# 1. 安装依赖
|
|
@@ -78,5 +98,6 @@ if __name__ == "__main__":
|
|
| 78 |
# 等待服务器启动
|
| 79 |
time.sleep(3)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
| 8 |
import subprocess
|
| 9 |
import time
|
| 10 |
import threading
|
| 11 |
+
import re
|
| 12 |
+
import shutil
|
| 13 |
|
| 14 |
def install_ngrok():
|
| 15 |
"""安装 pyngrok"""
|
|
|
|
| 22 |
subprocess.Popen([sys.executable, "server.py"])
|
| 23 |
|
| 24 |
def start_ngrok():
|
|
|
|
| 25 |
try:
|
| 26 |
from pyngrok import ngrok
|
|
|
|
|
|
|
| 27 |
token = os.environ.get("NGROK_AUTHTOKEN")
|
| 28 |
if not token:
|
| 29 |
print("\n⚠️ 警告: 未设置 NGROK_AUTHTOKEN 环境变量")
|
| 30 |
+
return False
|
| 31 |
+
ngrok.set_auth_token(token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
public_url = ngrok.connect(8000).public_url
|
|
|
|
| 33 |
print("\n" + "="*60)
|
| 34 |
+
print("✅ 成功穿透! 公网访问地址:")
|
| 35 |
print(f"👉 {public_url}")
|
| 36 |
print("="*60 + "\n")
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
while True:
|
| 39 |
time.sleep(1)
|
| 40 |
except KeyboardInterrupt:
|
|
|
|
| 41 |
ngrok.kill()
|
| 42 |
+
return True
|
| 43 |
except ImportError:
|
| 44 |
print("❌ pyngrok 导入失败,请确保已安装")
|
| 45 |
+
return False
|
| 46 |
except Exception as e:
|
| 47 |
print(f"❌ ngrok 启动失败: {e}")
|
| 48 |
+
return False
|
| 49 |
+
|
| 50 |
+
def start_cloudflared():
|
| 51 |
+
try:
|
| 52 |
+
if not shutil.which("cloudflared"):
|
| 53 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "cloudflared"])
|
| 54 |
+
proc = subprocess.Popen([
|
| 55 |
+
"cloudflared", "tunnel", "--url", "http://localhost:8000", "--no-autoupdate"
|
| 56 |
+
], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
| 57 |
+
url = None
|
| 58 |
+
while True:
|
| 59 |
+
line = proc.stdout.readline()
|
| 60 |
+
if not line:
|
| 61 |
+
break
|
| 62 |
+
if "trycloudflare.com" in line:
|
| 63 |
+
m = re.search(r"https?://[\w\.-]+trycloudflare\.com[\S]*", line)
|
| 64 |
+
if m:
|
| 65 |
+
url = m.group(0)
|
| 66 |
+
print("\n" + "="*60)
|
| 67 |
+
print("✅ 成功穿透! 公网访问地址:")
|
| 68 |
+
print(f"👉 {url}")
|
| 69 |
+
print("="*60 + "\n")
|
| 70 |
+
break
|
| 71 |
+
try:
|
| 72 |
+
while True:
|
| 73 |
+
time.sleep(1)
|
| 74 |
+
except KeyboardInterrupt:
|
| 75 |
+
proc.terminate()
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"❌ Cloudflare Tunnel 启动失败: {e}")
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
# 1. 安装依赖
|
|
|
|
| 98 |
# 等待服务器启动
|
| 99 |
time.sleep(3)
|
| 100 |
|
| 101 |
+
ok = start_ngrok()
|
| 102 |
+
if not ok:
|
| 103 |
+
start_cloudflared()
|