good version for 算法注册

This commit is contained in:
2026-02-15 21:23:28 +08:00
parent 3c03777b97
commit 62ea5d36a5
115 changed files with 9566 additions and 1576 deletions

View File

@@ -0,0 +1,16 @@
FROM python:3.9-slim
WORKDIR /app
# 安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制代码
COPY . .
# 暴露端口
EXPOSE 8000
# 启动服务
CMD ["python", "main.py"]

View File

@@ -0,0 +1,71 @@
import logging
import base64
from io import BytesIO
from typing import List, Dict, Any
logger = logging.getLogger(__name__)
class ImageRecognizer:
"""图像识别器"""
def __init__(self):
"""初始化图像识别器"""
logger.info("初始化图像识别器")
# 这里可以加载预训练模型
# 示例中使用简单的规则识别
def recognize(self, images: List[str], params: Dict[str, Any] = None) -> List[Dict[str, Any]]:
"""识别图像
Args:
images: 图像列表每个图像为base64编码字符串
params: 识别参数
Returns:
识别结果列表
"""
if params is None:
params = {}
threshold = params.get("threshold", 0.5)
results = []
for image_base64 in images:
# 简单的规则识别示例
recognition = self._simple_recognize(image_base64)
results.append({
"image": image_base64[:100] + "..." if len(image_base64) > 100 else image_base64,
"label": recognition["label"],
"confidence": recognition["confidence"]
})
return results
def _simple_recognize(self, image_base64: str) -> Dict[str, Any]:
"""简单的图像识别实现
Args:
image_base64: base64编码的图像
Returns:
识别结果
"""
# 简单的规则识别(基于图像大小和内容特征)
try:
# 解码base64
image_data = base64.b64decode(image_base64)
# 计算图像大小特征
image_size = len(image_data)
# 基于大小的简单分类
if image_size < 10240: # 小于10KB
return {"label": "小图像", "confidence": 0.8}
elif image_size < 102400: # 小于100KB
return {"label": "中等图像", "confidence": 0.85}
else: # 大于100KB
return {"label": "大图像", "confidence": 0.9}
except Exception as e:
logger.error(f"Image recognition error: {str(e)}")
return {"label": "未知", "confidence": 0.5}

View File

@@ -0,0 +1,27 @@
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
"""服务配置"""
# 服务基本配置
HOST: str = "0.0.0.0"
PORT: int = 8002
DEBUG: bool = True
# 服务名称
SERVICE_NAME: str = "image-recognition"
# 日志配置
LOG_LEVEL: str = "info"
# 算法配置
ALGORITHM_THRESHOLD: float = 0.5
class Config:
env_file = ".env"
case_sensitive = True
# 创建全局配置实例
settings = Settings()

View File

@@ -0,0 +1,108 @@
from fastapi import FastAPI, HTTPException, UploadFile, File
from pydantic import BaseModel
import uvicorn
import json
import logging
import base64
from io import BytesIO
from .ai_algorithm import ImageRecognizer
from .config import settings
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# 初始化FastAPI应用
app = FastAPI(
title="图像识别服务",
description="提供图像识别功能的AI服务",
version="1.0.0"
)
# 初始化识别器
recognizer = ImageRecognizer()
# 定义请求模型
class PredictRequest(BaseModel):
input_data: list
params: dict = {}
# 定义响应模型
class PredictResponse(BaseModel):
predictions: list
status: str
@app.post("/predict", response_model=PredictResponse)
async def predict(request: PredictRequest):
"""算法预测接口"""
try:
logger.info(f"Received prediction request for {len(request.input_data)} images")
predictions = recognizer.recognize(request.input_data, request.params)
logger.info(f"Prediction completed: {predictions}")
return PredictResponse(
predictions=predictions,
status="success"
)
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/predict/file")
async def predict_file(file: UploadFile = File(...)):
"""通过文件上传进行预测"""
try:
logger.info(f"Received file upload: {file.filename}")
# 读取文件内容
contents = await file.read()
# 转换为base64
image_base64 = base64.b64encode(contents).decode('utf-8')
# 调用识别器
predictions = recognizer.recognize([image_base64])
logger.info(f"File prediction completed: {predictions}")
return {
"predictions": predictions,
"status": "success",
"filename": file.filename
}
except Exception as e:
logger.error(f"File prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""健康检查接口"""
return {
"status": "healthy",
"service": "image-recognition",
"version": "1.0.0"
}
@app.get("/info")
async def service_info():
"""服务信息接口"""
return {
"name": "图像识别服务",
"description": "提供图像识别功能的AI服务",
"version": "1.0.0",
"endpoints": {
"/predict": "POST - 图像识别预测",
"/predict/file": "POST - 通过文件上传进行预测",
"/health": "GET - 健康检查",
"/info": "GET - 服务信息"
}
}
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG
)

View File

@@ -0,0 +1,5 @@
fastapi==0.104.1
uvicorn==0.24.0.post1
pydantic==2.5.2
pydantic-settings==2.1.0
python-multipart==0.0.6

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# 启动图像识别服务
# 进入服务目录
cd "$(dirname "$0")"
# 检查虚拟环境是否存在
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate
# 安装依赖
echo "安装依赖..."
pip install --no-cache-dir -r requirements.txt
# 启动服务
echo "启动图像识别服务..."
python main.py

View File

@@ -0,0 +1,16 @@
FROM python:3.9-slim
WORKDIR /app
# 安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制代码
COPY . .
# 暴露端口
EXPOSE 8000
# 启动服务
CMD ["python", "main.py"]

View File

@@ -0,0 +1,174 @@
import logging
import os
from typing import List, Dict, Any, Optional
import openai
from .config import settings
logger = logging.getLogger(__name__)
class OpenAIProxy:
"""OpenAI代理"""
def __init__(self):
"""初始化OpenAI代理"""
logger.info("初始化OpenAI代理")
# 设置API密钥
openai.api_key = settings.API_KEY
if settings.API_BASE:
openai.api_base = settings.API_BASE
def complete(self, model: str, messages: list, temperature: float = 0.7,
max_tokens: int = 1000) -> Dict[str, Any]:
"""完成聊天请求
Args:
model: 模型名称
messages: 消息列表
temperature: 温度参数
max_tokens: 最大令牌数
Returns:
完成结果
"""
try:
response = openai.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens
)
# 转换为字典格式
return {
"id": response.id,
"object": response.object,
"created": response.created,
"model": response.model,
"choices": [
{
"index": choice.index,
"message": {
"role": choice.message.role,
"content": choice.message.content
},
"finish_reason": choice.finish_reason
}
for choice in response.choices
],
"usage": {
"prompt_tokens": response.usage.prompt_tokens,
"completion_tokens": response.usage.completion_tokens,
"total_tokens": response.usage.total_tokens
}
}
except Exception as e:
logger.error(f"OpenAI completion error: {str(e)}")
# 返回模拟响应,用于演示
return self._mock_completion(messages, model)
def generate_simulation_input(self, prompt: str, input_type: str = "text") -> Dict[str, Any]:
"""生成仿真输入数据
Args:
prompt: 用户描述的场景
input_type: 输入类型,支持 "text", "image", "table"
Returns:
生成的仿真输入数据
"""
try:
# 根据输入类型构建不同的提示词
if input_type == "text":
system_prompt = "你是一个文本数据生成器,根据用户描述生成相应的文本数据"
user_prompt = f"请根据以下描述生成文本数据:{prompt}"
elif input_type == "image":
system_prompt = "你是一个图像描述生成器,根据用户描述生成详细的图像描述"
user_prompt = f"请根据以下描述生成详细的图像描述:{prompt}"
elif input_type == "table":
system_prompt = "你是一个表格数据生成器,根据用户描述生成结构化的表格数据"
user_prompt = f"请根据以下描述生成结构化的表格数据:{prompt}"
else:
system_prompt = "你是一个数据生成器,根据用户描述生成相应的数据"
user_prompt = f"请根据以下描述生成数据:{prompt}"
# 调用OpenAI API
response = openai.chat.completions.create(
model=settings.MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=settings.TEMPERATURE,
max_tokens=settings.MAX_TOKENS
)
# 处理响应
generated_content = response.choices[0].message.content
return {
"success": True,
"data": generated_content,
"input_type": input_type
}
except Exception as e:
logger.error(f"OpenAI simulation input generation error: {str(e)}")
# 返回模拟响应,用于演示
return self._mock_simulation_input(prompt, input_type)
def _mock_completion(self, messages: list, model: str) -> Dict[str, Any]:
"""模拟完成响应,用于演示
Args:
messages: 消息列表
model: 模型名称
Returns:
模拟的完成结果
"""
return {
"id": "chat-mock-123",
"object": "chat.completion",
"created": 1677825464,
"model": model,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "这是一个模拟的响应用于演示OpenAI代理服务"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}
def _mock_simulation_input(self, prompt: str, input_type: str) -> Dict[str, Any]:
"""模拟生成仿真输入数据,用于演示
Args:
prompt: 用户描述的场景
input_type: 输入类型
Returns:
模拟的生成结果
"""
if input_type == "text":
data = f"这是根据描述生成的文本数据:{prompt}"
elif input_type == "image":
data = f"这是根据描述生成的图像描述:{prompt}"
elif input_type == "table":
data = f"这是根据描述生成的表格数据:{prompt}"
else:
data = f"这是根据描述生成的数据:{prompt}"
return {
"success": True,
"data": data,
"input_type": input_type
}

View File

@@ -0,0 +1,31 @@
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
"""服务配置"""
# 服务基本配置
HOST: str = "0.0.0.0"
PORT: int = 8004
DEBUG: bool = True
# 服务名称
SERVICE_NAME: str = "openai-proxy"
# 日志配置
LOG_LEVEL: str = "info"
# OpenAI配置
API_KEY: Optional[str] = None
API_BASE: str = "https://api.openai.com/v1"
MODEL: str = "gpt-3.5-turbo"
TEMPERATURE: float = 0.7
MAX_TOKENS: int = 1000
class Config:
env_file = ".env"
case_sensitive = True
# 创建全局配置实例
settings = Settings()

View File

@@ -0,0 +1,109 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
import json
import logging
from .ai_algorithm import OpenAIProxy
from .config import settings
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# 初始化FastAPI应用
app = FastAPI(
title="OpenAI代理服务",
description="提供OpenAI API代理功能的服务",
version="1.0.0"
)
# 初始化代理
openai_proxy = OpenAIProxy()
# 定义请求模型
class CompletionRequest(BaseModel):
model: str = "gpt-3.5-turbo"
messages: list
temperature: float = 0.7
max_tokens: int = 1000
# 定义响应模型
class CompletionResponse(BaseModel):
id: str
object: str
created: int
model: str
choices: list
usage: dict
# 定义生成仿真输入请求模型
class GenerateSimulationInputRequest(BaseModel):
prompt: str
input_type: str = "text"
@app.post("/v1/chat/completions")
async def chat_completions(request: CompletionRequest):
"""OpenAI聊天完成接口"""
try:
logger.info(f"Received chat completion request for model: {request.model}")
response = openai_proxy.complete(
model=request.model,
messages=request.messages,
temperature=request.temperature,
max_tokens=request.max_tokens
)
logger.info(f"Chat completion completed")
return response
except Exception as e:
logger.error(f"Chat completion error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/generate-simulation-input")
async def generate_simulation_input(request: GenerateSimulationInputRequest):
"""生成仿真输入数据"""
try:
logger.info(f"Received simulation input generation request")
response = openai_proxy.generate_simulation_input(
prompt=request.prompt,
input_type=request.input_type
)
logger.info(f"Simulation input generation completed")
return response
except Exception as e:
logger.error(f"Simulation input generation error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""健康检查接口"""
return {
"status": "healthy",
"service": "openai-proxy",
"version": "1.0.0"
}
@app.get("/info")
async def service_info():
"""服务信息接口"""
return {
"name": "OpenAI代理服务",
"description": "提供OpenAI API代理功能的服务",
"version": "1.0.0",
"endpoints": {
"/v1/chat/completions": "POST - OpenAI聊天完成",
"/generate-simulation-input": "POST - 生成仿真输入数据",
"/health": "GET - 健康检查",
"/info": "GET - 服务信息"
}
}
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG
)

View File

@@ -0,0 +1,6 @@
fastapi==0.104.1
uvicorn==0.24.0.post1
pydantic==2.5.2
pydantic-settings==2.1.0
openai==1.3.5
python-dotenv==1.0.0

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# 启动OpenAI代理服务
# 进入服务目录
cd "$(dirname "$0")"
# 检查虚拟环境是否存在
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate
# 安装依赖
echo "安装依赖..."
pip install --no-cache-dir -r requirements.txt
# 启动服务
echo "启动OpenAI代理服务..."
python main.py

View File

@@ -0,0 +1,16 @@
FROM python:3.9-slim
WORKDIR /app
# 安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制代码
COPY . .
# 暴露端口
EXPOSE 8000
# 启动服务
CMD ["python", "main.py"]

View File

@@ -0,0 +1,89 @@
import logging
import base64
from io import BytesIO
from typing import List, Dict, Any
logger = logging.getLogger(__name__)
class SpeechToTextConverter:
"""语音转文字转换器"""
def __init__(self):
"""初始化语音转文字转换器"""
logger.info("初始化语音转文字转换器")
# 这里可以加载预训练模型
# 示例中使用简单的规则转换
def convert(self, audios: List[str], params: Dict[str, Any] = None) -> List[Dict[str, Any]]:
"""转换语音为文字
Args:
audios: 音频列表每个音频为base64编码字符串
params: 转换参数
Returns:
转换结果列表
"""
if params is None:
params = {}
language = params.get("language", "zh")
results = []
for audio_base64 in audios:
# 简单的规则转换示例
transcription = self._simple_convert(audio_base64, language)
results.append({
"audio": audio_base64[:100] + "..." if len(audio_base64) > 100 else audio_base64,
"text": transcription["text"],
"confidence": transcription["confidence"]
})
return results
def _simple_convert(self, audio_base64: str, language: str) -> Dict[str, Any]:
"""简单的语音转文字实现
Args:
audio_base64: base64编码的音频
language: 语言
Returns:
转换结果
"""
# 简单的规则转换(基于音频大小和内容特征)
try:
# 解码base64
audio_data = base64.b64decode(audio_base64)
# 计算音频大小特征
audio_size = len(audio_data)
# 基于大小的简单转换
if audio_size < 10240: # 小于10KB
text = "这是一段短音频"
elif audio_size < 102400: # 小于100KB
text = "这是一段中等长度的音频"
else: # 大于100KB
text = "这是一段长音频"
# 根据语言调整文本
if language == "en":
if audio_size < 10240:
text = "This is a short audio"
elif audio_size < 102400:
text = "This is a medium length audio"
else:
text = "This is a long audio"
return {
"text": text,
"confidence": 0.85
}
except Exception as e:
logger.error(f"Speech to text conversion error: {str(e)}")
return {
"text": "",
"confidence": 0.0
}

View File

@@ -0,0 +1,27 @@
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
"""服务配置"""
# 服务基本配置
HOST: str = "0.0.0.0"
PORT: int = 8003
DEBUG: bool = True
# 服务名称
SERVICE_NAME: str = "speech-to-text"
# 日志配置
LOG_LEVEL: str = "info"
# 算法配置
DEFAULT_LANGUAGE: str = "zh"
class Config:
env_file = ".env"
case_sensitive = True
# 创建全局配置实例
settings = Settings()

View File

@@ -0,0 +1,108 @@
from fastapi import FastAPI, HTTPException, UploadFile, File
from pydantic import BaseModel
import uvicorn
import json
import logging
import base64
from io import BytesIO
from .ai_algorithm import SpeechToTextConverter
from .config import settings
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# 初始化FastAPI应用
app = FastAPI(
title="语音转文字服务",
description="提供语音转文字功能的AI服务",
version="1.0.0"
)
# 初始化转换器
converter = SpeechToTextConverter()
# 定义请求模型
class PredictRequest(BaseModel):
input_data: list
params: dict = {}
# 定义响应模型
class PredictResponse(BaseModel):
predictions: list
status: str
@app.post("/predict", response_model=PredictResponse)
async def predict(request: PredictRequest):
"""算法预测接口"""
try:
logger.info(f"Received prediction request for {len(request.input_data)} audio files")
predictions = converter.convert(request.input_data, request.params)
logger.info(f"Prediction completed: {predictions}")
return PredictResponse(
predictions=predictions,
status="success"
)
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/predict/file")
async def predict_file(file: UploadFile = File(...)):
"""通过文件上传进行预测"""
try:
logger.info(f"Received file upload: {file.filename}")
# 读取文件内容
contents = await file.read()
# 转换为base64
audio_base64 = base64.b64encode(contents).decode('utf-8')
# 调用转换器
predictions = converter.convert([audio_base64])
logger.info(f"File prediction completed: {predictions}")
return {
"predictions": predictions,
"status": "success",
"filename": file.filename
}
except Exception as e:
logger.error(f"File prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""健康检查接口"""
return {
"status": "healthy",
"service": "speech-to-text",
"version": "1.0.0"
}
@app.get("/info")
async def service_info():
"""服务信息接口"""
return {
"name": "语音转文字服务",
"description": "提供语音转文字功能的AI服务",
"version": "1.0.0",
"endpoints": {
"/predict": "POST - 语音转文字预测",
"/predict/file": "POST - 通过文件上传进行预测",
"/health": "GET - 健康检查",
"/info": "GET - 服务信息"
}
}
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG
)

View File

@@ -0,0 +1,5 @@
fastapi==0.104.1
uvicorn==0.24.0.post1
pydantic==2.5.2
pydantic-settings==2.1.0
python-multipart==0.0.6

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# 启动语音转文字服务
# 进入服务目录
cd "$(dirname "$0")"
# 检查虚拟环境是否存在
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate
# 安装依赖
echo "安装依赖..."
pip install --no-cache-dir -r requirements.txt
# 启动服务
echo "启动语音转文字服务..."
python main.py

View File

@@ -0,0 +1,16 @@
FROM python:3.9-slim
WORKDIR /app
# 安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制代码
COPY . .
# 暴露端口
EXPOSE 8000
# 启动服务
CMD ["python", "main.py"]

View File

@@ -0,0 +1,66 @@
import logging
from typing import List, Dict, Any
logger = logging.getLogger(__name__)
class TextClassifier:
"""文本分类器"""
def __init__(self):
"""初始化文本分类器"""
logger.info("初始化文本分类器")
# 这里可以加载预训练模型
# 示例中使用简单的规则分类
def classify(self, texts: List[str], params: Dict[str, Any] = None) -> List[Dict[str, Any]]:
"""分类文本
Args:
texts: 文本列表
params: 分类参数
Returns:
分类结果列表
"""
if params is None:
params = {}
threshold = params.get("threshold", 0.5)
results = []
for text in texts:
# 简单的规则分类示例
classification = self._simple_classify(text)
results.append({
"text": text,
"label": classification["label"],
"confidence": classification["confidence"]
})
return results
def _simple_classify(self, text: str) -> Dict[str, Any]:
"""简单的文本分类实现
Args:
text: 待分类的文本
Returns:
分类结果
"""
# 简单的规则分类
text_lower = text.lower()
if any(keyword in text_lower for keyword in ["技术", "科技", "编程", "代码"]):
return {"label": "技术", "confidence": 0.9}
elif any(keyword in text_lower for keyword in ["体育", "足球", "篮球", "运动"]):
return {"label": "体育", "confidence": 0.85}
elif any(keyword in text_lower for keyword in ["电影", "音乐", "娱乐", "游戏"]):
return {"label": "娱乐", "confidence": 0.8}
elif any(keyword in text_lower for keyword in ["美食", "餐厅", "烹饪", "食物"]):
return {"label": "美食", "confidence": 0.85}
elif any(keyword in text_lower for keyword in ["政治", "新闻", "政府", "政策"]):
return {"label": "政治", "confidence": 0.9}
else:
return {"label": "其他", "confidence": 0.7}

View File

@@ -0,0 +1,27 @@
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
"""服务配置"""
# 服务基本配置
HOST: str = "0.0.0.0"
PORT: int = 8001
DEBUG: bool = True
# 服务名称
SERVICE_NAME: str = "text-classification"
# 日志配置
LOG_LEVEL: str = "info"
# 算法配置
ALGORITHM_THRESHOLD: float = 0.5
class Config:
env_file = ".env"
case_sensitive = True
# 创建全局配置实例
settings = Settings()

View File

@@ -0,0 +1,80 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
import json
import logging
from .ai_algorithm import TextClassifier
from .config import settings
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# 初始化FastAPI应用
app = FastAPI(
title="文本分类服务",
description="提供文本分类功能的AI服务",
version="1.0.0"
)
# 初始化分类器
classifier = TextClassifier()
# 定义请求模型
class PredictRequest(BaseModel):
input_data: list
params: dict = {}
# 定义响应模型
class PredictResponse(BaseModel):
predictions: list
status: str
@app.post("/predict", response_model=PredictResponse)
async def predict(request: PredictRequest):
"""算法预测接口"""
try:
logger.info(f"Received prediction request: {request.input_data}")
predictions = classifier.classify(request.input_data, request.params)
logger.info(f"Prediction completed: {predictions}")
return PredictResponse(
predictions=predictions,
status="success"
)
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""健康检查接口"""
return {
"status": "healthy",
"service": "text-classification",
"version": "1.0.0"
}
@app.get("/info")
async def service_info():
"""服务信息接口"""
return {
"name": "文本分类服务",
"description": "提供文本分类功能的AI服务",
"version": "1.0.0",
"endpoints": {
"/predict": "POST - 文本分类预测",
"/health": "GET - 健康检查",
"/info": "GET - 服务信息"
}
}
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.DEBUG
)

View File

@@ -0,0 +1,5 @@
fastapi==0.104.1
uvicorn==0.24.0.post1
pydantic==2.5.2
pydantic-settings==2.1.0
python-multipart==0.0.6

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# 启动文本分类服务
# 进入服务目录
cd "$(dirname "$0")"
# 检查虚拟环境是否存在
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate
# 安装依赖
echo "安装依赖..."
pip install --no-cache-dir -r requirements.txt
# 启动服务
echo "启动文本分类服务..."
python main.py