Spaces:
Runtime error
Runtime error
Axel-Student
commited on
Commit
·
c9fdad1
1
Parent(s):
6268c2b
change to api
Browse files- Dockerfile +2 -3
- app.py +32 -18
Dockerfile
CHANGED
@@ -2,8 +2,7 @@ FROM python:3.10
|
|
2 |
|
3 |
WORKDIR /app
|
4 |
|
5 |
-
RUN pip install --no-cache-dir torch torchvision torchaudio diffusers
|
6 |
-
RUN pip install -U diffusers
|
7 |
RUN pip install git+https://github.com/huggingface/diffusers.git
|
8 |
RUN pip install transformers
|
9 |
RUN pip install accelerate
|
@@ -14,4 +13,4 @@ COPY . /app
|
|
14 |
|
15 |
EXPOSE 7860
|
16 |
|
17 |
-
CMD ["
|
|
|
2 |
|
3 |
WORKDIR /app
|
4 |
|
5 |
+
RUN pip install --no-cache-dir torch torchvision torchaudio diffusers fastapi uvicorn
|
|
|
6 |
RUN pip install git+https://github.com/huggingface/diffusers.git
|
7 |
RUN pip install transformers
|
8 |
RUN pip install accelerate
|
|
|
13 |
|
14 |
EXPOSE 7860
|
15 |
|
16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,26 +1,40 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
import
|
4 |
-
from
|
|
|
|
|
5 |
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
model="black-forest-labs/FLUX.1-dev")
|
12 |
|
13 |
-
def generate_image(prompt):
|
14 |
-
image =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return image
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
|
|
|
|
|
|
|
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
-
|
|
|
|
1 |
import os
|
2 |
+
import io
|
3 |
+
import torch
|
4 |
+
from fastapi import FastAPI, HTTPException
|
5 |
+
from fastapi.responses import StreamingResponse
|
6 |
+
from diffusers import FluxPipeline # type: ignore
|
7 |
|
8 |
+
app = FastAPI()
|
9 |
|
10 |
+
# Récupération du token et authentification
|
11 |
+
token = os.getenv("HF_TOKEN")
|
12 |
+
login(token=token)
|
|
|
13 |
|
14 |
+
def generate_image(prompt: str):
|
15 |
+
image = pipe(
|
16 |
+
prompt,
|
17 |
+
height=1024,
|
18 |
+
width=1024,
|
19 |
+
guidance_scale=3.5,
|
20 |
+
num_inference_steps=50,
|
21 |
+
max_sequence_length=512,
|
22 |
+
generator=torch.Generator("cpu").manual_seed(0)
|
23 |
+
).images[0]
|
24 |
return image
|
25 |
|
26 |
+
@app.get("/generate")
|
27 |
+
def generate(prompt: str):
|
28 |
+
try:
|
29 |
+
image = generate_image(prompt)
|
30 |
+
# On sauvegarde l'image dans un buffer en mémoire
|
31 |
+
buf = io.BytesIO()
|
32 |
+
image.save(buf, format="PNG")
|
33 |
+
buf.seek(0)
|
34 |
+
return StreamingResponse(buf, media_type="image/png")
|
35 |
+
except Exception as e:
|
36 |
+
raise HTTPException(status_code=500, detail=str(e))
|
37 |
|
38 |
if __name__ == "__main__":
|
39 |
+
import uvicorn # type: ignore
|
40 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|