Spaces:
Runtime error
Runtime error
Update model_space.py
Browse files- model_space.py +46 -96
model_space.py
CHANGED
|
@@ -2,14 +2,15 @@ from fastapi import FastAPI, HTTPException, Depends, Header
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from llama_cpp import Llama
|
| 6 |
-
from huggingface_hub import
|
|
|
|
| 7 |
import time
|
| 8 |
import logging
|
| 9 |
from typing import AsyncGenerator, Optional
|
| 10 |
import asyncio
|
| 11 |
|
| 12 |
-
#
|
| 13 |
logging.basicConfig(
|
| 14 |
level=logging.INFO,
|
| 15 |
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
@@ -20,55 +21,59 @@ logger = logging.getLogger(__name__)
|
|
| 20 |
app = FastAPI(title="CodeLlama-7B-Instruct (GGUF-4bit) CPU", version="1.0")
|
| 21 |
logger.info("FastAPI应用初始化完成")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
app.add_middleware(
|
| 25 |
CORSMiddleware,
|
| 26 |
-
allow_origins=["*"],
|
| 27 |
allow_credentials=True,
|
| 28 |
allow_methods=["*"],
|
| 29 |
allow_headers=["*"],
|
| 30 |
)
|
| 31 |
logger.info("跨域中间件配置完成")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
def verify_api_key(api_key: Optional[str] = Header(None, alias="api_key")):
|
| 35 |
-
# Spaces可通过环境变量传密钥,避免硬编码(在Spaces设置中配置NODE_API_KEY)
|
| 36 |
valid_key = os.getenv("NODE_API_KEY", "default-node-key-123")
|
| 37 |
if not api_key or api_key != valid_key:
|
| 38 |
logger.warning(f"无效API密钥:{api_key}")
|
| 39 |
raise HTTPException(status_code=401, detail="Invalid or missing API Key")
|
| 40 |
return api_key
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
MODEL_FILE = "codellama-7b-instruct.Q4_K_M.gguf" # 4-bit量化文件(内存占用最小且效果好)
|
| 46 |
|
| 47 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
model_load_start = time.time()
|
| 49 |
-
logger.info(f"开始加载模型:{MODEL_ID}/{MODEL_FILE}(CPU环境)")
|
| 50 |
-
|
| 51 |
-
# 核心:GGUF模型加载配置(CPU专用,限制内存占用)
|
| 52 |
llm = Llama(
|
| 53 |
-
model_path=
|
| 54 |
-
n_ctx=2048,
|
| 55 |
-
n_threads=4,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
verbose=False, # 关闭冗余日志,减少Spaces输出占用
|
| 59 |
)
|
| 60 |
-
|
| 61 |
model_load_end = time.time()
|
| 62 |
-
logger.info(f"模型加载完成!耗时 {model_load_end - model_load_start:.2f}
|
| 63 |
except Exception as e:
|
| 64 |
-
logger.error(f"
|
| 65 |
-
raise RuntimeError(f"Model
|
| 66 |
|
| 67 |
-
#
|
| 68 |
class GenerationRequest(BaseModel):
|
| 69 |
prompt: str
|
| 70 |
-
max_new_tokens: int = 150
|
| 71 |
-
temperature: float = 0.6
|
| 72 |
top_p: float = 0.9
|
| 73 |
|
| 74 |
@app.post("/generate/code/stream")
|
|
@@ -76,83 +81,28 @@ async def generate_code_stream(
|
|
| 76 |
req: GenerationRequest,
|
| 77 |
api_key: str = Depends(verify_api_key)
|
| 78 |
) -> StreamingResponse:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# 构建CodeLlama指令格式(必须严格遵循,否则生成效果差)
|
| 83 |
-
formatted_prompt = f"<s>[INST] {req.prompt} [/INST]"
|
| 84 |
-
|
| 85 |
-
# 流式生成器(适配Spaces前端SSE接收)
|
| 86 |
-
async def stream_generator() -> AsyncGenerator[str, None]:
|
| 87 |
-
start_time = time.time()
|
| 88 |
-
generated_total = 0
|
| 89 |
-
try:
|
| 90 |
-
# 调用GGUF模型流式生成(CPU优化)
|
| 91 |
-
for token in llm.create_completion(
|
| 92 |
-
prompt=formatted_prompt,
|
| 93 |
-
max_tokens=req.max_new_tokens,
|
| 94 |
-
temperature=req.temperature,
|
| 95 |
-
top_p=req.top_p,
|
| 96 |
-
stream=True, # 开启流式
|
| 97 |
-
stop=["</s>"], # 结束符(避免多余输出)
|
| 98 |
-
echo=False # 不返回输入prompt
|
| 99 |
-
):
|
| 100 |
-
# 提取生成的文本片段
|
| 101 |
-
text_chunk = token["choices"][0]["text"]
|
| 102 |
-
if text_chunk:
|
| 103 |
-
generated_total += len(text_chunk.split())
|
| 104 |
-
# 按SSE格式返回(Spaces前端可直接解析)
|
| 105 |
-
yield f"data: {text_chunk}\n\n"
|
| 106 |
-
await asyncio.sleep(0.05) # 微调延迟,避免前端接收过快
|
| 107 |
-
|
| 108 |
-
# 生成完成标记
|
| 109 |
-
total_time = time.time() - start_time
|
| 110 |
-
yield f"event: end\ndata: 生成完成!共{generated_total}个词,耗时{total_time:.2f}秒\n\n"
|
| 111 |
-
logger.info(f"请求 {request_id} 完成,耗时 {total_time:.2f} 秒")
|
| 112 |
-
|
| 113 |
-
except Exception as e:
|
| 114 |
-
error_msg = f"生成失败:{str(e)}"
|
| 115 |
-
logger.error(f"请求 {request_id} 错误:{error_msg}")
|
| 116 |
-
yield f"event: error\ndata: {error_msg}\n\n"
|
| 117 |
-
raise
|
| 118 |
-
|
| 119 |
-
# 返回流式响应(适配Spaces HTTP服务)
|
| 120 |
-
return StreamingResponse(
|
| 121 |
-
stream_generator(),
|
| 122 |
-
media_type="text/event-stream",
|
| 123 |
-
headers={
|
| 124 |
-
"Cache-Control": "no-cache", # 禁用Spaces缓存,确保实时性
|
| 125 |
-
"Connection": "keep-alive",
|
| 126 |
-
"X-Accel-Buffering": "no" # 禁止代理缓冲,避免流式断连
|
| 127 |
-
}
|
| 128 |
-
)
|
| 129 |
|
| 130 |
-
#
|
| 131 |
@app.get("/")
|
| 132 |
async def root():
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
"service": "CodeLlama-7B-Instruct (GGUF-4bit) CPU",
|
| 136 |
-
"message": "Spaces部署成功!调用 /generate/code/stream 接口生成代码",
|
| 137 |
-
"model_info": f"模型:{MODEL_ID},量化:4-bit,内存占用:~3.5GB"
|
| 138 |
-
}
|
| 139 |
|
| 140 |
@app.get("/health")
|
| 141 |
async def health_check(api_key: str = Depends(verify_api_key)):
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
"model_status": "loaded",
|
| 145 |
-
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
|
| 146 |
-
}
|
| 147 |
|
| 148 |
-
#
|
| 149 |
if __name__ == "__main__":
|
| 150 |
import uvicorn
|
| 151 |
-
logger.info("启动Uvicorn
|
| 152 |
uvicorn.run(
|
| 153 |
-
app="
|
| 154 |
-
host="0.0.0.0",
|
| 155 |
-
port=7860,
|
| 156 |
-
timeout_keep_alive=300,
|
| 157 |
-
workers=1
|
| 158 |
)
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
from huggingface_hub import snapshot_download # 正确导入
|
| 7 |
+
import os
|
| 8 |
import time
|
| 9 |
import logging
|
| 10 |
from typing import AsyncGenerator, Optional
|
| 11 |
import asyncio
|
| 12 |
|
| 13 |
+
# 日志与FastAPI初始化
|
| 14 |
logging.basicConfig(
|
| 15 |
level=logging.INFO,
|
| 16 |
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
|
|
| 21 |
app = FastAPI(title="CodeLlama-7B-Instruct (GGUF-4bit) CPU", version="1.0")
|
| 22 |
logger.info("FastAPI应用初始化完成")
|
| 23 |
|
| 24 |
+
# 跨域配置
|
| 25 |
app.add_middleware(
|
| 26 |
CORSMiddleware,
|
| 27 |
+
allow_origins=["*"],
|
| 28 |
allow_credentials=True,
|
| 29 |
allow_methods=["*"],
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
logger.info("跨域中间件配置完成")
|
| 33 |
|
| 34 |
+
# API密钥验证
|
| 35 |
def verify_api_key(api_key: Optional[str] = Header(None, alias="api_key")):
|
|
|
|
| 36 |
valid_key = os.getenv("NODE_API_KEY", "default-node-key-123")
|
| 37 |
if not api_key or api_key != valid_key:
|
| 38 |
logger.warning(f"无效API密钥:{api_key}")
|
| 39 |
raise HTTPException(status_code=401, detail="Invalid or missing API Key")
|
| 40 |
return api_key
|
| 41 |
|
| 42 |
+
# 自动下载GGUF模型
|
| 43 |
+
MODEL_REPO = "TheBloke/CodeLlama-7B-Instruct-GGUF"
|
| 44 |
+
MODEL_FILE = "codellama-7b-instruct.Q4_K_M.gguf"
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
+
logger.info(f"开始从Hugging Face下载模型:{MODEL_REPO}/{MODEL_FILE}")
|
| 48 |
+
model_dir = snapshot_download(
|
| 49 |
+
repo_id=MODEL_REPO,
|
| 50 |
+
allow_patterns=[MODEL_FILE],
|
| 51 |
+
local_dir="./models",
|
| 52 |
+
local_dir_use_symlinks=False
|
| 53 |
+
)
|
| 54 |
+
model_path = os.path.join(model_dir, MODEL_FILE)
|
| 55 |
+
logger.info(f"模型下载完成,保存到:{model_path}")
|
| 56 |
+
|
| 57 |
+
# 加载GGUF模型
|
| 58 |
model_load_start = time.time()
|
|
|
|
|
|
|
|
|
|
| 59 |
llm = Llama(
|
| 60 |
+
model_path=model_path,
|
| 61 |
+
n_ctx=2048,
|
| 62 |
+
n_threads=4,
|
| 63 |
+
n_gpu_layers=0,
|
| 64 |
+
verbose=False,
|
|
|
|
| 65 |
)
|
|
|
|
| 66 |
model_load_end = time.time()
|
| 67 |
+
logger.info(f"模型加载完成!耗时 {model_load_end - model_load_start:.2f} 秒")
|
| 68 |
except Exception as e:
|
| 69 |
+
logger.error(f"模型下载或加载失败:{str(e)}", exc_info=True)
|
| 70 |
+
raise RuntimeError(f"Model setup failed: {str(e)}") from e
|
| 71 |
|
| 72 |
+
# 数据模型与流式推理(其余代码不变)
|
| 73 |
class GenerationRequest(BaseModel):
|
| 74 |
prompt: str
|
| 75 |
+
max_new_tokens: int = 150
|
| 76 |
+
temperature: float = 0.6
|
| 77 |
top_p: float = 0.9
|
| 78 |
|
| 79 |
@app.post("/generate/code/stream")
|
|
|
|
| 81 |
req: GenerationRequest,
|
| 82 |
api_key: str = Depends(verify_api_key)
|
| 83 |
) -> StreamingResponse:
|
| 84 |
+
# 其余逻辑不变...
|
| 85 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# 根路径与健康检查(其余代码不变)
|
| 88 |
@app.get("/")
|
| 89 |
async def root():
|
| 90 |
+
# 其余逻辑不变...
|
| 91 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
@app.get("/health")
|
| 94 |
async def health_check(api_key: str = Depends(verify_api_key)):
|
| 95 |
+
# 其余逻辑不变...
|
| 96 |
+
pass
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Spaces启动入口
|
| 99 |
if __name__ == "__main__":
|
| 100 |
import uvicorn
|
| 101 |
+
logger.info("启动Uvicorn服务")
|
| 102 |
uvicorn.run(
|
| 103 |
+
app="model_space:app", # 注意:如果文件名是model_space.py,这里要对应
|
| 104 |
+
host="0.0.0.0",
|
| 105 |
+
port=7860,
|
| 106 |
+
timeout_keep_alive=300,
|
| 107 |
+
workers=1
|
| 108 |
)
|