Spaces:
Sleeping
Sleeping
feat: iniit project
Browse files- Dockerfile +18 -0
- app.py +73 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 使用官方Python镜像作为基础
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# 设置工作目录
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# 复制依赖文件并安装
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# 复制应用代码
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# 暴露端口
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# 启动应用
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Response
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import asyncio
|
| 5 |
+
import logging
|
| 6 |
+
from contextlib import asynccontextmanager
|
| 7 |
+
|
| 8 |
+
# 配置日志
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# 共享状态,用于存储模型和加载状态
|
| 14 |
+
state = {
|
| 15 |
+
"translator": None,
|
| 16 |
+
"model_loaded": False,
|
| 17 |
+
"model_loading": False
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
async def load_model():
|
| 21 |
+
"""异步加载模型"""
|
| 22 |
+
if state["model_loaded"] or state["model_loading"]:
|
| 23 |
+
return
|
| 24 |
+
state["model_loading"] = True
|
| 25 |
+
logger.info("开始加载模型...")
|
| 26 |
+
try:
|
| 27 |
+
state["translator"] = pipeline("translation_en_to_zh", model="Helsinki-NLP/opus-mt-en-zh")
|
| 28 |
+
state["model_loaded"] = True
|
| 29 |
+
logger.info("模型加载成功。")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
logger.error(f"模型加载失败: {e}")
|
| 32 |
+
finally:
|
| 33 |
+
state["model_loading"] = False
|
| 34 |
+
|
| 35 |
+
@asynccontextmanager
|
| 36 |
+
async def lifespan(app: FastAPI):
|
| 37 |
+
# Load the model on startup
|
| 38 |
+
asyncio.create_task(load_model())
|
| 39 |
+
yield
|
| 40 |
+
# Clean up the model and release the resources
|
| 41 |
+
state["translator"] = None
|
| 42 |
+
state["model_loaded"] = False
|
| 43 |
+
logger.info("模型已卸载。")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
app = FastAPI(lifespan=lifespan)
|
| 47 |
+
|
| 48 |
+
class TranslationRequest(BaseModel):
|
| 49 |
+
text: str
|
| 50 |
+
|
| 51 |
+
class TranslationResponse(BaseModel):
|
| 52 |
+
translated_text: str
|
| 53 |
+
|
| 54 |
+
@app.post("/translate", response_model=TranslationResponse)
|
| 55 |
+
async def translate_text(request: TranslationRequest):
|
| 56 |
+
if not state["model_loaded"]:
|
| 57 |
+
return Response(content={"message": "Model is not loaded yet"}, status_code=503)
|
| 58 |
+
|
| 59 |
+
result = state["translator"](request.text)
|
| 60 |
+
return {"translated_text": result[0]["translation_text"]}
|
| 61 |
+
|
| 62 |
+
@app.get("/health")
|
| 63 |
+
async def health_check():
|
| 64 |
+
status_code = 200 if state["model_loaded"] else 503
|
| 65 |
+
return Response(content={"model_loaded": state["model_loaded"]}, status_code=status_code)
|
| 66 |
+
|
| 67 |
+
@app.get("/")
|
| 68 |
+
async def read_root():
|
| 69 |
+
return {"message": "Welcome to the translation API"}
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
import uvicorn
|
| 73 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
sentencepiece
|