Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,56 @@
|
|
| 1 |
import uvicorn
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 5 |
import os
|
| 6 |
import uuid
|
| 7 |
-
from min_dalle import MinDalle
|
| 8 |
-
import torch
|
| 9 |
|
| 10 |
-
# --- CONFIGURATION ---
|
| 11 |
app = FastAPI()
|
| 12 |
-
device = "cpu" # On force le CPU car c'est ce qu'on a
|
| 13 |
-
print(f"🚀 Démarrage de l'API Image (DALL-E Mini) sur {device}...")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model = MinDalle(
|
| 19 |
-
models_root='./pretrained',
|
| 20 |
-
dtype=torch.float32,
|
| 21 |
-
device=device,
|
| 22 |
-
is_mega=False, # False = DALL-E Mini (plus léger), True = DALL-E Mega
|
| 23 |
-
is_reusable=True
|
| 24 |
-
)
|
| 25 |
-
print("✅ Modèle DALL-E Mini chargé.")
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"❌ Erreur chargement modèle: {e}")
|
| 28 |
-
model = None
|
| 29 |
|
| 30 |
class ImageRequest(BaseModel):
|
| 31 |
prompt: str
|
| 32 |
|
| 33 |
@app.post("/generate")
|
| 34 |
async def generate_image(request: ImageRequest):
|
| 35 |
-
print(f"🎨 Génération
|
| 36 |
|
| 37 |
-
if not
|
| 38 |
-
raise HTTPException(status_code=500, detail="
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
# grid_size=1 pour générer une seule image (plus rapide)
|
| 43 |
-
images = model.generate_image(
|
| 44 |
-
text=request.prompt,
|
| 45 |
-
seed=-1,
|
| 46 |
-
grid_size=1,
|
| 47 |
-
is_seamless=False,
|
| 48 |
-
temperature=1,
|
| 49 |
-
top_k=256,
|
| 50 |
-
supercondition_factor=16,
|
| 51 |
-
is_verbose=True
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"❌ Erreur
|
| 66 |
raise HTTPException(status_code=500, detail=str(e))
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
app.mount("/", StaticFiles(directory=".", html=True), name="static")
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.responses import FileResponse
|
| 5 |
from pydantic import BaseModel
|
| 6 |
+
import requests
|
| 7 |
import os
|
| 8 |
import uuid
|
|
|
|
|
|
|
| 9 |
|
|
|
|
| 10 |
app = FastAPI()
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Token HF récupéré depuis les variables d'environnement (Secrets)
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
class ImageRequest(BaseModel):
|
| 17 |
prompt: str
|
| 18 |
|
| 19 |
@app.post("/generate")
|
| 20 |
async def generate_image(request: ImageRequest):
|
| 21 |
+
print(f"🎨 Génération (HF API) : {request.prompt}")
|
| 22 |
|
| 23 |
+
if not HF_TOKEN:
|
| 24 |
+
raise HTTPException(status_code=500, detail="Token HF manquant. Configurez le secret HF_TOKEN.")
|
| 25 |
|
| 26 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 27 |
+
payload = {"inputs": request.prompt}
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if response.status_code != 200:
|
| 33 |
+
raise Exception(f"Erreur API HF: {response.text}")
|
| 34 |
+
|
| 35 |
+
# L'API retourne l'image binaire
|
| 36 |
+
image_bytes = response.content
|
| 37 |
|
| 38 |
+
# Sauvegarde locale pour servir l'image
|
| 39 |
+
filename = f"gen_{uuid.uuid4()}.png"
|
| 40 |
+
with open(filename, "wb") as f:
|
| 41 |
+
f.write(image_bytes)
|
| 42 |
+
|
| 43 |
+
# URL absolue pour le frontend
|
| 44 |
+
return {"image_url": f"/{filename}"}
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"❌ Erreur : {str(e)}")
|
| 48 |
raise HTTPException(status_code=500, detail=str(e))
|
| 49 |
|
| 50 |
+
@app.get("/")
|
| 51 |
+
async def read_index():
|
| 52 |
+
return FileResponse('index.html')
|
| 53 |
+
|
| 54 |
app.mount("/", StaticFiles(directory=".", html=True), name="static")
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|