forked from CJackHwang/AIstudioProxyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
104 lines (83 loc) · 2.46 KB
/
server.py
File metadata and controls
104 lines (83 loc) · 2.46 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
from typing import (
Any,
)
# 新增: 导入 load_dotenv
from dotenv import load_dotenv
# 新增: 在所有其他导入之前加载 .env 文件
load_dotenv()
# --- 导入集中状态模块 ---
from api_utils.server_state import state
# --- 向后兼容:通过 __getattr__ 将属性访问转发到 state 对象 ---
# 这允许现有代码继续使用 `import server; server.page_instance`
# 同时保持状态的集中管理
# 定义需要转发到 state 的属性名称
_STATE_ATTRS = {
# Stream Queue
"STREAM_QUEUE",
"STREAM_PROCESS",
# Playwright/Browser State
"playwright_manager",
"browser_instance",
"page_instance",
"is_playwright_ready",
"is_browser_connected",
"is_page_ready",
"is_initializing",
# Proxy Configuration
"PLAYWRIGHT_PROXY_SETTINGS",
# Model State
"global_model_list_raw_json",
"parsed_model_list",
"model_list_fetch_event",
"current_ai_studio_model_id",
"model_switching_lock",
"excluded_model_ids",
# Request Processing State
"request_queue",
"processing_lock",
"worker_task",
# Parameter Cache
"page_params_cache",
"params_cache_lock",
# Debug Logging State
"console_logs",
"network_log",
# Logging
"logger",
"log_ws_manager",
# Control Flags
"should_exit",
}
def __getattr__(name: str) -> Any:
"""Forward attribute access to the state object for backward compatibility."""
if name in _STATE_ATTRS:
return getattr(state, name)
raise AttributeError(f"module 'server' has no attribute '{name}'")
def __setattr__(name: str, value: Any) -> None:
"""Forward attribute assignment to the state object for backward compatibility."""
if name in _STATE_ATTRS:
setattr(state, name, value)
else:
# For non-state attributes, use the module's __dict__
globals()[name] = value
def clear_debug_logs() -> None:
"""Clear console and network logs (called after each request)."""
state.clear_debug_logs()
# --- 配置模块导入 ---
# --- models模块导入 ---
# --- logging_utils模块导入 ---
# --- browser_utils模块导入 ---
# --- api_utils模块导入 ---
from api_utils import (
create_app,
)
# --- FastAPI App 定义 ---
app = create_app()
# --- Main Guard ---
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 2048))
uvicorn.run(
"server:app", host="0.0.0.0", port=port, log_level="info", access_log=False
)