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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional, List
|
| 5 |
+
import uuid
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
import sys
|
| 9 |
+
import os
|
| 10 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 11 |
+
|
| 12 |
+
from core.ai_gateway import AIGateway, ModelProvider
|
| 13 |
+
from core.security import get_current_user, create_access_token, get_password_hash, verify_password
|
| 14 |
+
from database.models import User, ContentItem, ScheduledPost
|
| 15 |
+
from database.session import get_db
|
| 16 |
+
from services.content_studio import ContentStudioService
|
| 17 |
+
from utils.text_processing import optimize_content, extract_keywords
|
| 18 |
+
|
| 19 |
+
app = FastAPI(
|
| 20 |
+
title="AuraNexus AI Content Orchestration Platform",
|
| 21 |
+
description="Open-source platform for AI-powered content creation and distribution",
|
| 22 |
+
version="0.1.0"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Add CORS middleware
|
| 26 |
+
app.add_middleware(
|
| 27 |
+
CORSMiddleware,
|
| 28 |
+
allow_origins=["*"],
|
| 29 |
+
allow_credentials=True,
|
| 30 |
+
allow_methods=["*"],
|
| 31 |
+
allow_headers=["*"],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Initialize services
|
| 35 |
+
ai_gateway = AIGateway()
|
| 36 |
+
content_studio = ContentStudioService(ai_gateway)
|
| 37 |
+
|
| 38 |
+
class Token(BaseModel):
|
| 39 |
+
access_token: str
|
| 40 |
+
token_type: str
|
| 41 |
+
|
| 42 |
+
class TokenData(BaseModel):
|
| 43 |
+
username: Optional[str] = None
|
| 44 |
+
|
| 45 |
+
class UserCreate(BaseModel):
|
| 46 |
+
username: str
|
| 47 |
+
email: str
|
| 48 |
+
password: str
|
| 49 |
+
|
| 50 |
+
class UserOut(BaseModel):
|
| 51 |
+
id: str
|
| 52 |
+
username: str
|
| 53 |
+
email: str
|
| 54 |
+
created_at: datetime
|
| 55 |
+
|
| 56 |
+
class ContentGenerationRequest(BaseModel):
|
| 57 |
+
topic: str
|
| 58 |
+
content_type: str
|
| 59 |
+
platform: str
|
| 60 |
+
provider: Optional[ModelProvider] = ModelProvider.LOCAL_LLAMA
|
| 61 |
+
|
| 62 |
+
class ContentGenerationResponse(BaseModel):
|
| 63 |
+
id: str
|
| 64 |
+
title: str
|
| 65 |
+
content_type: str
|
| 66 |
+
generated_content: str
|
| 67 |
+
platform_specific_variants: dict
|
| 68 |
+
tags: List[str]
|
| 69 |
+
created_at: datetime
|
| 70 |
+
|
| 71 |
+
class OptimizationRequest(BaseModel):
|
| 72 |
+
content_id: str
|
| 73 |
+
optimization_type: str
|
| 74 |
+
|
| 75 |
+
@app.on_event("startup")
|
| 76 |
+
def startup_event():
|
| 77 |
+
from database.models import create_tables
|
| 78 |
+
create_tables()
|
| 79 |
+
|
| 80 |
+
@app.get("/")
|
| 81 |
+
async def root():
|
| 82 |
+
return {"message": "Welcome to AuraNexus AI Content Orchestration Platform"}
|
| 83 |
+
|
| 84 |
+
@app.post("/api/v1/auth/register", response_model=UserOut)
|
| 85 |
+
async def register(user: UserCreate, db=Depends(get_db)):
|
| 86 |
+
# Check if user already exists
|
| 87 |
+
existing_user = db.query(User).filter(User.username == user.username).first()
|
| 88 |
+
if existing_user:
|
| 89 |
+
raise HTTPException(status_code=400, detail="Username already registered")
|
| 90 |
+
|
| 91 |
+
existing_email = db.query(User).filter(User.email == user.email).first()
|
| 92 |
+
if existing_email:
|
| 93 |
+
raise HTTPException(status_code=400, detail="Email already registered")
|
| 94 |
+
|
| 95 |
+
# Create new user
|
| 96 |
+
hashed_password = get_password_hash(user.password)
|
| 97 |
+
db_user = User(
|
| 98 |
+
username=user.username,
|
| 99 |
+
email=user.email,
|
| 100 |
+
hashed_password=hashed_password
|
| 101 |
+
)
|
| 102 |
+
db.add(db_user)
|
| 103 |
+
db.commit()
|
| 104 |
+
db.refresh(db_user)
|
| 105 |
+
|
| 106 |
+
return UserOut(
|
| 107 |
+
id=db_user.id,
|
| 108 |
+
username=db_user.username,
|
| 109 |
+
email=db_user.email,
|
| 110 |
+
created_at=db_user.created_at
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
@app.post("/api/v1/auth/token", response_model=Token)
|
| 114 |
+
async def login(username: str, password: str, db=Depends(get_db)):
|
| 115 |
+
user = db.query(User).filter(User.username == username).first()
|
| 116 |
+
if not user or not verify_password(password, user.hashed_password):
|
| 117 |
+
raise HTTPException(
|
| 118 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 119 |
+
detail="Incorrect username or password",
|
| 120 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
access_token = create_access_token(data={"sub": user.id})
|
| 124 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
| 125 |
+
|
| 126 |
+
@app.post("/api/v1/content/generate", response_model=ContentGenerationResponse)
|
| 127 |
+
async def generate_content(
|
| 128 |
+
request: ContentGenerationRequest,
|
| 129 |
+
current_user: User = Depends(get_current_user),
|
| 130 |
+
db=Depends(get_db)
|
| 131 |
+
):
|
| 132 |
+
"""
|
| 133 |
+
Generate content using AI models
|
| 134 |
+
"""
|
| 135 |
+
try:
|
| 136 |
+
content_item = await content_studio.generate_content(
|
| 137 |
+
topic=request.topic,
|
| 138 |
+
content_type=request.content_type,
|
| 139 |
+
platform=request.platform,
|
| 140 |
+
user_id=current_user.id,
|
| 141 |
+
db=db
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
return ContentGenerationResponse(
|
| 145 |
+
id=content_item.id,
|
| 146 |
+
title=content_item.title,
|
| 147 |
+
content_type=content_item.content_type,
|
| 148 |
+
generated_content=content_item.generated_content,
|
| 149 |
+
platform_specific_variants=content_item.platform_specific_variants,
|
| 150 |
+
tags=content_item.tags,
|
| 151 |
+
created_at=content_item.created_at
|
| 152 |
+
)
|
| 153 |
+
except Exception as e:
|
| 154 |
+
raise HTTPException(status_code=500, detail=f"Content generation failed: {str(e)}")
|
| 155 |
+
|
| 156 |
+
@app.post("/api/v1/content/optimize")
|
| 157 |
+
async def optimize_existing_content(
|
| 158 |
+
request: OptimizationRequest,
|
| 159 |
+
current_user: User = Depends(get_current_user),
|
| 160 |
+
db=Depends(get_db)
|
| 161 |
+
):
|
| 162 |
+
"""
|
| 163 |
+
Optimize existing content
|
| 164 |
+
"""
|
| 165 |
+
try:
|
| 166 |
+
content_item = await content_studio.optimize_content(
|
| 167 |
+
content_id=request.content_id,
|
| 168 |
+
optimization_type=request.optimization_type,
|
| 169 |
+
db=db
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
return {
|
| 173 |
+
"id": content_item.id,
|
| 174 |
+
"optimized_content": content_item.generated_content,
|
| 175 |
+
"updated_at": content_item.updated_at
|
| 176 |
+
}
|
| 177 |
+
except Exception as e:
|
| 178 |
+
raise HTTPException(status_code=500, detail=f"Content optimization failed: {str(e)}")
|
| 179 |
+
|
| 180 |
+
@app.get("/api/v1/content/my", response_model=List[ContentGenerationResponse])
|
| 181 |
+
async def get_my_content(
|
| 182 |
+
current_user: User = Depends(get_current_user),
|
| 183 |
+
db=Depends(get_db)
|
| 184 |
+
):
|
| 185 |
+
"""
|
| 186 |
+
Get all content items for the current user
|
| 187 |
+
"""
|
| 188 |
+
content_items = db.query(ContentItem).filter(ContentItem.owner_id == current_user.id).all()
|
| 189 |
+
|
| 190 |
+
return [
|
| 191 |
+
ContentGenerationResponse(
|
| 192 |
+
id=item.id,
|
| 193 |
+
title=item.title,
|
| 194 |
+
content_type=item.content_type,
|
| 195 |
+
generated_content=item.generated_content,
|
| 196 |
+
platform_specific_variants=item.platform_specific_variants,
|
| 197 |
+
tags=item.tags,
|
| 198 |
+
created_at=item.created_at
|
| 199 |
+
)
|
| 200 |
+
for item in content_items
|
| 201 |
+
]
|
| 202 |
+
|
| 203 |
+
@app.post("/api/v1/generate/text")
|
| 204 |
+
async def generate_text_only(request: ContentGenerationRequest, current_user: User = Depends(get_current_user)):
|
| 205 |
+
"""
|
| 206 |
+
Generate text content only
|
| 207 |
+
"""
|
| 208 |
+
try:
|
| 209 |
+
prompt = content_studio._create_content_prompt(
|
| 210 |
+
request.topic,
|
| 211 |
+
request.content_type,
|
| 212 |
+
request.platform
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
generated_text = await ai_gateway.generate_text(
|
| 216 |
+
prompt=prompt,
|
| 217 |
+
provider=request.provider
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
return {"generated_text": generated_text}
|
| 221 |
+
except Exception as e:
|
| 222 |
+
raise HTTPException(status_code=500, detail=f"Text generation failed: {str(e)}")
|
| 223 |
+
|
| 224 |
+
@app.get("/api/v1/models")
|
| 225 |
+
async def list_available_models(current_user: User = Depends(get_current_user)):
|
| 226 |
+
"""
|
| 227 |
+
List available AI models
|
| 228 |
+
"""
|
| 229 |
+
available_models = [provider.value for provider in ai_gateway.models.keys()]
|
| 230 |
+
return {"available_models": available_models}
|
| 231 |
+
|
| 232 |
+
@app.get("/api/v1/health")
|
| 233 |
+
async def health_check():
|
| 234 |
+
return {"status": "healthy", "timestamp": datetime.utcnow()}
|
| 235 |
+
|
| 236 |
+
if __name__ == "__main__":
|
| 237 |
+
import uvicorn
|
| 238 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|