Spaces:
Sleeping
Sleeping
File size: 5,491 Bytes
bd161ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
"""
Hugging Face API router for model inference endpoints.
"""
from fastapi import APIRouter, HTTPException, Depends
from typing import Dict, List, Optional, Any
from pydantic import BaseModel
import logging
from services.huggingface_service import HuggingFaceService
from dependencies import get_current_user
from models import User
logger = logging.getLogger(__name__)
router = APIRouter()
hf_service = HuggingFaceService()
class TextGenerationRequest(BaseModel):
prompt: str
model_name: Optional[str] = None
max_length: int = 2048
temperature: float = 0.7
use_local: bool = False
class EmbeddingRequest(BaseModel):
text: str
model_name: str = "sentence-transformers/all-MiniLM-L6-v2"
class ClassificationRequest(BaseModel):
text: str
model_name: str = "distilbert-base-uncased-finetuned-sst-2-english"
class TranslationRequest(BaseModel):
text: str
source_lang: str = "en"
target_lang: str = "es"
model_name: str = "Helsinki-NLP/opus-mt-en-es"
@router.get("/status")
async def get_hf_status(current_user: User = Depends(get_current_user)):
"""Get Hugging Face service status."""
try:
status = hf_service.get_service_status()
return {
"success": True,
"status": status
}
except Exception as e:
logger.error(f"Error getting HF status: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/models")
async def get_available_models(current_user: User = Depends(get_current_user)):
"""Get available Hugging Face models."""
try:
models = hf_service.get_available_models()
return {
"success": True,
"models": models
}
except Exception as e:
logger.error(f"Error getting available models: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/generate")
async def generate_text(
request: TextGenerationRequest,
current_user: User = Depends(get_current_user)
):
"""Generate text using Hugging Face models."""
try:
result = await hf_service.generate_text(
prompt=request.prompt,
model_name=request.model_name,
max_length=request.max_length,
temperature=request.temperature,
use_local=request.use_local
)
return {
"success": True,
"generated_text": result,
"prompt": request.prompt,
"model_used": request.model_name or "default"
}
except Exception as e:
logger.error(f"Error generating text: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/embed")
async def create_embedding(
request: EmbeddingRequest,
current_user: User = Depends(get_current_user)
):
"""Create embedding using Hugging Face models."""
try:
embedding = await hf_service.create_embedding(
text=request.text,
model_name=request.model_name
)
return {
"success": True,
"embedding": embedding,
"text": request.text,
"model_used": request.model_name,
"embedding_dimension": len(embedding)
}
except Exception as e:
logger.error(f"Error creating embedding: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/classify")
async def classify_text(
request: ClassificationRequest,
current_user: User = Depends(get_current_user)
):
"""Classify text using Hugging Face models."""
try:
result = await hf_service.classify_text(
text=request.text,
model_name=request.model_name
)
return {
"success": True,
"classification": result,
"text": request.text,
"model_used": request.model_name
}
except Exception as e:
logger.error(f"Error classifying text: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/translate")
async def translate_text(
request: TranslationRequest,
current_user: User = Depends(get_current_user)
):
"""Translate text using Hugging Face models."""
try:
result = await hf_service.translate_text(
text=request.text,
source_lang=request.source_lang,
target_lang=request.target_lang,
model_name=request.model_name
)
return {
"success": True,
"translation": result,
"original_text": request.text,
"source_language": request.source_lang,
"target_language": request.target_lang,
"model_used": request.model_name
}
except Exception as e:
logger.error(f"Error translating text: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/health")
async def hf_health_check():
"""Health check for Hugging Face service."""
try:
status = hf_service.get_service_status()
return {
"status": "healthy" if (status["client_initialized"] or status["local_model_loaded"]) else "unhealthy",
"service": "huggingface",
"details": status
}
except Exception as e:
logger.error(f"HF health check failed: {e}")
return {
"status": "unhealthy",
"service": "huggingface",
"error": str(e)
} |