|
|
|
@ -1,44 +1,15 @@
|
|
|
|
|
import time
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
import uvicorn
|
|
|
|
|
from multiprocessing import Process, Event, freeze_support, Manager
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
from liveMan import DouyinLiveWebFetcher, DouyinLiveWebReply
|
|
|
|
|
from helper import PromptQueue, LiveChatConfig
|
|
|
|
|
from liveMan import DouyinLiveWebFetcher, DouyinLiveWebReply
|
|
|
|
|
|
|
|
|
|
logger.add('log.log', encoding='utf-8', rotation="500MB")
|
|
|
|
|
|
|
|
|
|
# FastAPI应用实例
|
|
|
|
|
app = FastAPI(title="直播状态控制接口")
|
|
|
|
|
|
|
|
|
|
# 请求体模型
|
|
|
|
|
class SpeakingStatus(BaseModel):
|
|
|
|
|
is_speaking: bool
|
|
|
|
|
|
|
|
|
|
# 全局变量,用于在FastAPI进程中存储共享字典引用
|
|
|
|
|
app.state.shared_config = None
|
|
|
|
|
|
|
|
|
|
# 接口:更新is_speaking状态
|
|
|
|
|
@app.post("/speaking-status", summary="更新说话状态")
|
|
|
|
|
def update_speaking_status(status: SpeakingStatus):
|
|
|
|
|
if app.state.shared_config is None:
|
|
|
|
|
return {"error": "服务未初始化"}
|
|
|
|
|
app.state.shared_config['is_speaking'] = status.is_speaking
|
|
|
|
|
# logger.info(f"通过API更新is_speaking状态为: {status.is_speaking}")
|
|
|
|
|
return {
|
|
|
|
|
"message": "状态更新成功",
|
|
|
|
|
"current_status": {
|
|
|
|
|
"is_speaking": app.state.shared_config['is_speaking'],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
logger.add("log.log", encoding="utf-8", rotation="500MB")
|
|
|
|
|
|
|
|
|
|
# 启动FastAPI服务的函数,接收共享字典作为参数
|
|
|
|
|
def start_fastapi_server(shared_config):
|
|
|
|
|
# 将共享字典存储到FastAPI应用的状态中
|
|
|
|
|
app.state.shared_config = shared_config
|
|
|
|
|
# 启动UVicorn服务器
|
|
|
|
|
from app import app
|
|
|
|
|
app.shared_config = shared_config
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
def fetch_user_chat_content(ws_open_event, queue):
|
|
|
|
@ -49,19 +20,24 @@ def reply_user_chat_content(queue, config):
|
|
|
|
|
reply = DouyinLiveWebReply(queue, config)
|
|
|
|
|
reply()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
freeze_support()
|
|
|
|
|
|
|
|
|
|
LiveChatConfig().update_chat_enable_status('启动中')
|
|
|
|
|
LiveChatConfig().update_chat_enable_status("启动中")
|
|
|
|
|
with Manager() as manager:
|
|
|
|
|
# 创建共享字典,包含所有需要的键
|
|
|
|
|
prediction_queue = manager.list() # 共享队列,用来存放预测时间(字符串)
|
|
|
|
|
|
|
|
|
|
shared_config = manager.dict({
|
|
|
|
|
'is_speaking': False
|
|
|
|
|
"is_speaking": False,
|
|
|
|
|
"predicted_time": None, # 最近一次用于对时差计算的预测时间
|
|
|
|
|
"speaking_false_time": None, # is_speaking 变为 False 的真实时间
|
|
|
|
|
"time_diff_seconds": None, # 真实时间 - 预测时间(秒)
|
|
|
|
|
"prediction_queue": prediction_queue # FIFO 队列,避免覆盖
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
queue = PromptQueue(10)
|
|
|
|
|
ws_open_event = Event()
|
|
|
|
|
|
|
|
|
|
# 创建进程时将共享字典传递给FastAPI服务
|
|
|
|
|
api_process = Process(
|
|
|
|
|
target=start_fastapi_server,
|
|
|
|
|
args=(shared_config,),
|
|
|
|
@ -69,20 +45,20 @@ if __name__ == '__main__':
|
|
|
|
|
)
|
|
|
|
|
fetch_process = Process(
|
|
|
|
|
target=fetch_user_chat_content,
|
|
|
|
|
args=(ws_open_event, queue)
|
|
|
|
|
args=(ws_open_event, queue),
|
|
|
|
|
name="Fetcher"
|
|
|
|
|
)
|
|
|
|
|
reply_process = Process(
|
|
|
|
|
target=reply_user_chat_content,
|
|
|
|
|
args=(queue, shared_config)
|
|
|
|
|
args=(queue, shared_config),
|
|
|
|
|
name="Replier"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 启动所有进程
|
|
|
|
|
api_process.start()
|
|
|
|
|
fetch_process.start()
|
|
|
|
|
ws_open_event.wait()
|
|
|
|
|
reply_process.start()
|
|
|
|
|
|
|
|
|
|
# 等待所有进程完成
|
|
|
|
|
fetch_process.join()
|
|
|
|
|
reply_process.join()
|
|
|
|
|
api_process.join()
|
|
|
|
|