Instructions to use MoYoYoTech/VoiceDialogue with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MoYoYoTech/VoiceDialogue with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-to-speech", model="MoYoYoTech/VoiceDialogue") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("MoYoYoTech/VoiceDialogue", dtype="auto") - llama-cpp-python
How to use MoYoYoTech/VoiceDialogue with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="MoYoYoTech/VoiceDialogue", filename="assets/models/llm/qwen/Qwen3-8B-Q6_K.gguf", )
llm.create_chat_completion( messages = "\"The answer to the universe is 42\"" )
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use MoYoYoTech/VoiceDialogue with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K # Run inference directly in the terminal: llama-cli -hf MoYoYoTech/VoiceDialogue:Q6_K
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K # Run inference directly in the terminal: llama-cli -hf MoYoYoTech/VoiceDialogue:Q6_K
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K # Run inference directly in the terminal: ./llama-cli -hf MoYoYoTech/VoiceDialogue:Q6_K
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K # Run inference directly in the terminal: ./build/bin/llama-cli -hf MoYoYoTech/VoiceDialogue:Q6_K
Use Docker
docker model run hf.co/MoYoYoTech/VoiceDialogue:Q6_K
- LM Studio
- Jan
- Ollama
How to use MoYoYoTech/VoiceDialogue with Ollama:
ollama run hf.co/MoYoYoTech/VoiceDialogue:Q6_K
- Unsloth Studio
How to use MoYoYoTech/VoiceDialogue with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for MoYoYoTech/VoiceDialogue to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for MoYoYoTech/VoiceDialogue to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for MoYoYoTech/VoiceDialogue to start chatting
- Pi
How to use MoYoYoTech/VoiceDialogue with Pi:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K
Configure the model in Pi
# Install Pi: npm install -g @mariozechner/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "llama-cpp": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "MoYoYoTech/VoiceDialogue:Q6_K" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Hermes Agent new
How to use MoYoYoTech/VoiceDialogue with Hermes Agent:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama-server -hf MoYoYoTech/VoiceDialogue:Q6_K
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default MoYoYoTech/VoiceDialogue:Q6_K
Run Hermes
hermes
- Docker Model Runner
How to use MoYoYoTech/VoiceDialogue with Docker Model Runner:
docker model run hf.co/MoYoYoTech/VoiceDialogue:Q6_K
- Lemonade
How to use MoYoYoTech/VoiceDialogue with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull MoYoYoTech/VoiceDialogue:Q6_K
Run and chat with the model
lemonade run user.VoiceDialogue-Q6_K
List all available models
lemonade list
Refactor WebSocket handling with connection manager
Browse files- Introduce `WebSocketConnectionManager` for centralized connection tracking and session management.
- Add support for session-specific connection handling and message broadcasting.
- Implement `websocket_connection_context` to streamline connection lifecycle management.
- Update WebSocket endpoint to utilize the connection manager for improved reliability and efficiency.
|
@@ -1,6 +1,10 @@
|
|
|
|
|
|
|
|
| 1 |
from queue import Empty
|
|
|
|
| 2 |
|
| 3 |
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
|
|
|
| 4 |
|
| 5 |
from voice_dialogue.core.constants import websocket_message_queue, session_manager
|
| 6 |
from voice_dialogue.utils.logger import logger
|
|
@@ -8,25 +12,124 @@ from voice_dialogue.utils.logger import logger
|
|
| 8 |
ws = APIRouter()
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@ws.websocket("/api/v1/ws")
|
| 12 |
async def websocket_endpoint(websocket: WebSocket):
|
| 13 |
"""WebSocket连接端点"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
except WebSocketDisconnect:
|
| 30 |
-
pass
|
| 31 |
-
except Exception as e:
|
| 32 |
-
logger.error(f"WebSocket连接异常: {e}")
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from contextlib import asynccontextmanager
|
| 3 |
from queue import Empty
|
| 4 |
+
from typing import Set, Dict
|
| 5 |
|
| 6 |
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
| 7 |
+
from fastapi.websockets import WebSocketState
|
| 8 |
|
| 9 |
from voice_dialogue.core.constants import websocket_message_queue, session_manager
|
| 10 |
from voice_dialogue.utils.logger import logger
|
|
|
|
| 12 |
ws = APIRouter()
|
| 13 |
|
| 14 |
|
| 15 |
+
class WebSocketConnectionManager:
|
| 16 |
+
"""WebSocket 连接管理器 - 管理所有活跃连接"""
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
# 使用 WeakSet 避免内存泄漏
|
| 20 |
+
self._connections: Set[WebSocket] = set()
|
| 21 |
+
# 会话ID到连接的映射
|
| 22 |
+
self._session_connections: Dict[str, Set[WebSocket]] = {}
|
| 23 |
+
self._lock = asyncio.Lock()
|
| 24 |
+
|
| 25 |
+
async def connect(self, websocket: WebSocket, session_id: str = None):
|
| 26 |
+
"""建立新连接"""
|
| 27 |
+
async with self._lock:
|
| 28 |
+
await websocket.accept()
|
| 29 |
+
self._connections.add(websocket)
|
| 30 |
+
|
| 31 |
+
# 如果指定了会话ID,建立映射关系
|
| 32 |
+
if session_id:
|
| 33 |
+
if session_id not in self._session_connections:
|
| 34 |
+
self._session_connections[session_id] = set()
|
| 35 |
+
self._session_connections[session_id].add(websocket)
|
| 36 |
+
|
| 37 |
+
logger.info(f"WebSocket连接已建立,当前活跃连接数: {len(self._connections)}")
|
| 38 |
+
|
| 39 |
+
async def disconnect(self, websocket: WebSocket):
|
| 40 |
+
"""断开连接"""
|
| 41 |
+
async with self._lock:
|
| 42 |
+
self._connections.discard(websocket)
|
| 43 |
+
|
| 44 |
+
# 从会话映射中移除
|
| 45 |
+
for session_id, connections in list(self._session_connections.items()):
|
| 46 |
+
connections.discard(websocket)
|
| 47 |
+
if not connections: # 如果该会话没有活跃连接,清理映射
|
| 48 |
+
del self._session_connections[session_id]
|
| 49 |
+
|
| 50 |
+
logger.info(f"WebSocket连接已断开,当前活跃连接数: {len(self._connections)}")
|
| 51 |
+
|
| 52 |
+
async def close_session_connections(self, session_id: str):
|
| 53 |
+
"""关闭指定会话的所有连接"""
|
| 54 |
+
async with self._lock:
|
| 55 |
+
if session_id in self._session_connections:
|
| 56 |
+
connections_to_close = list(self._session_connections[session_id])
|
| 57 |
+
for connection in connections_to_close:
|
| 58 |
+
try:
|
| 59 |
+
await connection.close()
|
| 60 |
+
logger.info(f"已关闭会话 {session_id} 的一个连接")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.warning(f"关闭连接时出错: {e}")
|
| 63 |
+
|
| 64 |
+
# 清理映射
|
| 65 |
+
del self._session_connections[session_id]
|
| 66 |
+
|
| 67 |
+
async def send_to_session(self, session_id: str, message: dict):
|
| 68 |
+
"""向指定会话的所有连接发送消息"""
|
| 69 |
+
async with self._lock:
|
| 70 |
+
if session_id in self._session_connections:
|
| 71 |
+
connections = list(self._session_connections[session_id])
|
| 72 |
+
disconnected_connections = []
|
| 73 |
+
|
| 74 |
+
for connection in connections:
|
| 75 |
+
try:
|
| 76 |
+
await connection.send_json(message)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logger.warning(f"发送消息失败,标记连接为断开: {e}")
|
| 79 |
+
disconnected_connections.append(connection)
|
| 80 |
+
|
| 81 |
+
# 清理断开的连接
|
| 82 |
+
for connection in disconnected_connections:
|
| 83 |
+
await self.disconnect(connection)
|
| 84 |
+
|
| 85 |
+
@property
|
| 86 |
+
def connection_count(self) -> int:
|
| 87 |
+
"""获取当前连接数"""
|
| 88 |
+
return len(self._connections)
|
| 89 |
+
|
| 90 |
+
def get_session_connection_count(self, session_id: str) -> int:
|
| 91 |
+
"""获取指定会话的连接数"""
|
| 92 |
+
return len(self._session_connections.get(session_id, set()))
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# 全局连接管理器实例
|
| 96 |
+
connection_manager = WebSocketConnectionManager()
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@asynccontextmanager
|
| 100 |
+
async def websocket_connection_context(websocket: WebSocket):
|
| 101 |
+
"""WebSocket连接上下文管理器"""
|
| 102 |
+
current_session_id = session_manager.current_id
|
| 103 |
+
|
| 104 |
+
# 关闭同一会话的旧连接
|
| 105 |
+
if connection_manager.get_session_connection_count(current_session_id) > 0:
|
| 106 |
+
logger.info(f"检测到会话 {current_session_id} 已有连接,关闭旧连接")
|
| 107 |
+
await connection_manager.close_session_connections(current_session_id)
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
# 建立新连接
|
| 111 |
+
await connection_manager.connect(websocket, current_session_id)
|
| 112 |
+
yield websocket
|
| 113 |
+
finally:
|
| 114 |
+
# 确保连接被正确清理
|
| 115 |
+
await connection_manager.disconnect(websocket)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
@ws.websocket("/api/v1/ws")
|
| 119 |
async def websocket_endpoint(websocket: WebSocket):
|
| 120 |
"""WebSocket连接端点"""
|
| 121 |
+
async with websocket_connection_context(websocket):
|
| 122 |
+
try:
|
| 123 |
+
# 保持连接活跃
|
| 124 |
+
while websocket.client_state == WebSocketState.CONNECTED:
|
| 125 |
+
try:
|
| 126 |
+
message = await websocket_message_queue.get()
|
| 127 |
+
except Empty:
|
| 128 |
+
continue
|
| 129 |
+
|
| 130 |
+
await connection_manager.send_to_session(message.session_id, message.model_dump())
|
| 131 |
+
|
| 132 |
+
except WebSocketDisconnect:
|
| 133 |
+
logger.info("WebSocket连接断开")
|
| 134 |
+
except Exception as e:
|
| 135 |
+
logger.error(f"WebSocket连接异常: {e}")
|
|
|
|
|
|
|
|
|
|
|
|