Spaces:
Running
Running
Update api.py
Browse filesfeat: full backend api)
api.py
CHANGED
|
@@ -2,27 +2,29 @@ from fastapi import FastAPI, BackgroundTasks, HTTPException
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import Optional
|
| 4 |
|
| 5 |
-
|
| 6 |
-
# Por ejemplo, si luego copias una función desde app.py de Rivera:
|
| 7 |
-
# from generation import generate_image_from_prompt
|
| 8 |
|
| 9 |
app = FastAPI(title="Sofia AI Backend")
|
| 10 |
|
|
|
|
| 11 |
class MessageRequest(BaseModel):
|
| 12 |
platform: str
|
| 13 |
message: str
|
| 14 |
user_id: str
|
| 15 |
timestamp: Optional[str] = None
|
| 16 |
|
|
|
|
| 17 |
class ImageGenerationRequest(BaseModel):
|
| 18 |
prompt_type: Optional[str] = None
|
| 19 |
custom_prompt: Optional[str] = None
|
| 20 |
-
model: str = "FLUX.1-
|
|
|
|
| 21 |
|
| 22 |
@app.get("/health")
|
| 23 |
async def health():
|
| 24 |
return {"status": "ok", "service": "sofia-ai-backend"}
|
| 25 |
|
|
|
|
| 26 |
@app.post("/webhook/message")
|
| 27 |
async def webhook_message(body: MessageRequest, background_tasks: BackgroundTasks):
|
| 28 |
# Aquí luego conectarás lógica real (guardar en cola, n8n, etc.)
|
|
@@ -31,43 +33,86 @@ async def webhook_message(body: MessageRequest, background_tasks: BackgroundTask
|
|
| 31 |
)
|
| 32 |
return {"status": "queued"}
|
| 33 |
|
|
|
|
| 34 |
@app.post("/api/generate")
|
| 35 |
async def api_generate(body: ImageGenerationRequest):
|
| 36 |
# 1) Validar entrada
|
| 37 |
if not body.custom_prompt and not body.prompt_type:
|
| 38 |
raise HTTPException(status_code=400, detail="prompt_type or custom_prompt required")
|
| 39 |
|
| 40 |
-
# 2) Construir prompt final (
|
| 41 |
if body.custom_prompt:
|
| 42 |
final_prompt = body.custom_prompt
|
|
|
|
| 43 |
else:
|
| 44 |
-
# Aquí puedes mapear prompt_type → prompt predefinido igual que en app.py de Rivera
|
| 45 |
-
# p.ej. lifestyle, fitness, boudoir, fashion, beach, etc.
|
| 46 |
prompt_map = {
|
| 47 |
-
"lifestyle":
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
|
|
|
| 53 |
if body.prompt_type not in prompt_map:
|
| 54 |
raise HTTPException(status_code=400, detail=f"Unknown prompt_type: {body.prompt_type}")
|
| 55 |
-
final_prompt = prompt_map[body.prompt_type]
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
return {
|
| 65 |
"status": "completed",
|
| 66 |
"prompt": final_prompt,
|
| 67 |
"model": body.model,
|
| 68 |
-
"
|
|
|
|
| 69 |
}
|
| 70 |
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
import uvicorn
|
|
|
|
| 73 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import Optional
|
| 4 |
|
| 5 |
+
from generation import generate_image_from_prompt
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI(title="Sofia AI Backend")
|
| 8 |
|
| 9 |
+
|
| 10 |
class MessageRequest(BaseModel):
|
| 11 |
platform: str
|
| 12 |
message: str
|
| 13 |
user_id: str
|
| 14 |
timestamp: Optional[str] = None
|
| 15 |
|
| 16 |
+
|
| 17 |
class ImageGenerationRequest(BaseModel):
|
| 18 |
prompt_type: Optional[str] = None
|
| 19 |
custom_prompt: Optional[str] = None
|
| 20 |
+
model: str = "black-forest-labs/FLUX.1-dev"
|
| 21 |
+
|
| 22 |
|
| 23 |
@app.get("/health")
|
| 24 |
async def health():
|
| 25 |
return {"status": "ok", "service": "sofia-ai-backend"}
|
| 26 |
|
| 27 |
+
|
| 28 |
@app.post("/webhook/message")
|
| 29 |
async def webhook_message(body: MessageRequest, background_tasks: BackgroundTasks):
|
| 30 |
# Aquí luego conectarás lógica real (guardar en cola, n8n, etc.)
|
|
|
|
| 33 |
)
|
| 34 |
return {"status": "queued"}
|
| 35 |
|
| 36 |
+
|
| 37 |
@app.post("/api/generate")
|
| 38 |
async def api_generate(body: ImageGenerationRequest):
|
| 39 |
# 1) Validar entrada
|
| 40 |
if not body.custom_prompt and not body.prompt_type:
|
| 41 |
raise HTTPException(status_code=400, detail="prompt_type or custom_prompt required")
|
| 42 |
|
| 43 |
+
# 2) Construir prompt final (basado en tipos de Sofia Rivera)
|
| 44 |
if body.custom_prompt:
|
| 45 |
final_prompt = body.custom_prompt
|
| 46 |
+
negative_prompt = ""
|
| 47 |
else:
|
|
|
|
|
|
|
| 48 |
prompt_map = {
|
| 49 |
+
"lifestyle": {
|
| 50 |
+
"prompt": (
|
| 51 |
+
"foto selfie profesional con iPhone de Sofia Rivera, hermosa mujer latina "
|
| 52 |
+
"cubanoamericana de 25 años, cabello largo oscuro y ondulado, sonrisa cálida, "
|
| 53 |
+
"departamento de Miami con vista al mar, golden hour, estética influencer Instagram"
|
| 54 |
+
),
|
| 55 |
+
"negative": "borroso, de baja calidad, distorsionado, deformado, feo, mala anatomía",
|
| 56 |
+
},
|
| 57 |
+
"fitness": {
|
| 58 |
+
"prompt": (
|
| 59 |
+
"selfie de cuerpo completo en el espejo de Sofia Rivera, influencer latina fitness, "
|
| 60 |
+
"cuerpo atlético tonificado, sujetador deportivo negro, mallas de cintura alta, "
|
| 61 |
+
"gimnasio moderno con espejos, iluminación natural, estética fitness Instagram"
|
| 62 |
+
),
|
| 63 |
+
"negative": "borroso, de baja calidad, distorsionado, malas proporciones",
|
| 64 |
+
},
|
| 65 |
+
"premium_boudoir": {
|
| 66 |
+
"prompt": (
|
| 67 |
+
"selfie en el dormitorio de Sofia Rivera, influencer latina de 25 años, lencería de encaje "
|
| 68 |
+
"blanco, luz suave de la mañana a través de cortinas transparentes, cama lujosa con sábanas "
|
| 69 |
+
"de seda, expresión sensual y segura, estilo boudoir de buen gusto, fotografía profesional"
|
| 70 |
+
),
|
| 71 |
+
"negative": "explícito, borroso, de baja calidad, distorsionado",
|
| 72 |
+
},
|
| 73 |
+
"fashion": {
|
| 74 |
+
"prompt": (
|
| 75 |
+
"foto de estilo urbano de Sofia Rivera, influencer de moda latina, outfit moderno de Miami, "
|
| 76 |
+
"gafas de sol de diseñador, pose natural y segura, fondo urbano, golden hour, estética moda Instagram"
|
| 77 |
+
),
|
| 78 |
+
"negative": "borroso, de baja calidad, mala iluminación",
|
| 79 |
+
},
|
| 80 |
+
"beach": {
|
| 81 |
+
"prompt": (
|
| 82 |
+
"foto de estilo de vida de Sofia Rivera en la playa, influencer latina, Miami Beach al atardecer, "
|
| 83 |
+
"atuendo casual de playa, expresión natural feliz, vibraciones tropicales, contenido lifestyle Instagram"
|
| 84 |
+
),
|
| 85 |
+
"negative": "borroso, de baja calidad, distorsionado",
|
| 86 |
+
},
|
| 87 |
}
|
| 88 |
+
|
| 89 |
if body.prompt_type not in prompt_map:
|
| 90 |
raise HTTPException(status_code=400, detail=f"Unknown prompt_type: {body.prompt_type}")
|
|
|
|
| 91 |
|
| 92 |
+
final_prompt = prompt_map[body.prompt_type]["prompt"]
|
| 93 |
+
negative_prompt = prompt_map[body.prompt_type]["negative"]
|
| 94 |
+
|
| 95 |
+
# 3) Llamar al motor de generación REAL (generation.py)
|
| 96 |
+
image_path, status = generate_image_from_prompt(
|
| 97 |
+
prompt=final_prompt,
|
| 98 |
+
negative_prompt=negative_prompt,
|
| 99 |
+
model_name=body.model,
|
| 100 |
+
seed=None,
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
if image_path is None:
|
| 104 |
+
raise HTTPException(status_code=500, detail=status)
|
| 105 |
|
| 106 |
return {
|
| 107 |
"status": "completed",
|
| 108 |
"prompt": final_prompt,
|
| 109 |
"model": body.model,
|
| 110 |
+
"image_path": image_path,
|
| 111 |
+
"status_message": status,
|
| 112 |
}
|
| 113 |
|
| 114 |
+
|
| 115 |
if __name__ == "__main__":
|
| 116 |
import uvicorn
|
| 117 |
+
|
| 118 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|