Spaces:
Sleeping
Sleeping
File size: 8,205 Bytes
bd161ec 566f774 bd161ec 566f774 bd161ec 566f774 bd161ec 5782182 bd161ec 566f774 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
"""
AI Service Manager - Manages multiple AI services with OpenAI as primary.
"""
import logging
from typing import Dict, List, Optional, Any
from config import settings
from services.openai_service import OpenAIService
# Optional import for Hugging Face service
try:
from services.huggingface_service import HuggingFaceService
HUGGINGFACE_AVAILABLE = True
except ImportError:
HUGGINGFACE_AVAILABLE = False
HuggingFaceService = None
logger = logging.getLogger(__name__)
class AIServiceManager:
"""Manages multiple AI services with OpenAI as the primary service."""
def __init__(self):
self.openai_service = None
self.huggingface_service = None
self.primary_service = settings.primary_ai_service.lower()
# Initialize OpenAI service (primary)
if settings.openai_api_key:
try:
self.openai_service = OpenAIService()
logger.info("OpenAI service initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize OpenAI service: {e}")
else:
logger.warning("OpenAI API key not configured")
# Initialize Hugging Face service (optional)
if HUGGINGFACE_AVAILABLE and settings.hf_api_token:
try:
self.huggingface_service = HuggingFaceService()
logger.info("Hugging Face service initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize Hugging Face service: {e}")
elif not HUGGINGFACE_AVAILABLE:
logger.warning("Hugging Face service not available - dependencies not installed")
logger.info(f"AI Service Manager initialized with primary service: {self.primary_service}")
async def generate_content(
self,
prompt: str,
context: str = "",
temperature: float = 0.7,
max_tokens: int = 2000,
service: str = "auto"
) -> str:
"""Generate content using the appropriate AI service."""
try:
# Determine which service to use
if service == "auto":
service = self.primary_service
elif service == "openai" and self.openai_service:
service = "openai"
elif service == "huggingface" and self.huggingface_service:
service = "huggingface"
else:
# Fallback to available service
if self.openai_service:
service = "openai"
elif self.huggingface_service:
service = "huggingface"
else:
raise Exception("No AI service available")
# Generate content using selected service
if service == "openai" and self.openai_service:
return await self.openai_service.generate_content(
prompt=prompt,
context=context,
temperature=temperature,
max_tokens=max_tokens
)
elif service == "huggingface" and self.huggingface_service:
return await self.huggingface_service.generate_text(
prompt=prompt,
max_length=max_tokens,
temperature=temperature
)
else:
raise Exception(f"Service '{service}' not available")
except Exception as e:
logger.error(f"Content generation error: {e}")
raise Exception(f"Failed to generate content: {str(e)}")
async def generate_text(
self,
prompt: str,
max_tokens: int = 2000,
temperature: float = 0.7,
service: str = "auto"
) -> Dict[str, str]:
"""Generate text using the appropriate AI service. Alias for generate_content."""
try:
content = await self.generate_content(
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens,
service=service
)
return {"text": content}
except Exception as e:
logger.error(f"Text generation error: {e}")
return {"text": "I apologize, but I'm having trouble generating a response right now. Please try again."}
async def create_embedding(
self,
text: str,
service: str = "auto"
) -> List[float]:
"""Create embedding using the appropriate AI service."""
try:
# Determine which service to use
if service == "auto":
# Prefer OpenAI for embeddings
if self.openai_service:
service = "openai"
elif self.huggingface_service:
service = "huggingface"
else:
raise Exception("No AI service available")
# Create embedding using selected service
if service == "openai" and self.openai_service:
return await self.openai_service.create_embedding(text)
elif service == "huggingface" and self.huggingface_service:
return await self.huggingface_service.create_embedding(text)
else:
raise Exception(f"Service '{service}' not available")
except Exception as e:
logger.error(f"Embedding creation error: {e}")
raise Exception(f"Failed to create embedding: {str(e)}")
async def analyze_content_structure(
self,
content: str,
service: str = "auto"
) -> dict:
"""Analyze content structure using the appropriate AI service."""
try:
# OpenAI has better structure analysis, prefer it
if service == "auto" and self.openai_service:
service = "openai"
elif service == "auto" and self.huggingface_service:
service = "huggingface"
if service == "openai" and self.openai_service:
return await self.openai_service.analyze_content_structure(content)
elif service == "huggingface" and self.huggingface_service:
# Basic analysis for Hugging Face
return {
"key_points": [],
"topics": [],
"complexity_level": "medium",
"word_count": len(content.split()),
"summary": "Analysis via Hugging Face"
}
else:
raise Exception(f"Service '{service}' not available")
except Exception as e:
logger.error(f"Content analysis error: {e}")
return {
"key_points": [],
"topics": [],
"complexity_level": "medium",
"word_count": len(content.split()),
"summary": "Analysis failed"
}
def get_service_status(self) -> Dict[str, Any]:
"""Get status of all AI services."""
return {
"primary_service": self.primary_service,
"openai": {
"available": self.openai_service is not None,
"configured": bool(settings.openai_api_key),
"model": settings.openai_model if self.openai_service else None
},
"huggingface": {
"available": self.huggingface_service is not None,
"configured": bool(settings.hf_api_token or settings.hf_model_name),
"model": settings.hf_model_name if self.huggingface_service else None,
"dependencies_available": HUGGINGFACE_AVAILABLE
}
}
def get_available_services(self) -> List[str]:
"""Get list of available AI services."""
services = []
if self.openai_service:
services.append("openai")
if self.huggingface_service:
services.append("huggingface")
return services
# Global instance
ai_service_manager = AIServiceManager() |