Rx Codex AI commited on
Commit
d2b9f1c
·
verified ·
1 Parent(s): 959bae5

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +15 -0
  2. app.py +83 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /code
4
+
5
+ ENV HF_HOME /code/.cache
6
+
7
+ RUN mkdir -p /code/.cache && chmod -R 777 /code/.cache
8
+
9
+ COPY ./requirements.txt /code/requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ COPY ./app.py /code/app.py
13
+
14
+ EXPOSE 7860
15
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel
4
+ import torch
5
+ from diffusers import AutoPipelineForText2Image
6
+ from contextlib import asynccontextmanager
7
+ import io
8
+ import base64
9
+ import os
10
+
11
+ # --- Pydantic Models ---
12
+ class ImageRequest(BaseModel):
13
+ prompt: str
14
+ negative_prompt: str = ""
15
+ steps: int = 25
16
+
17
+ class ImageResponse(BaseModel):
18
+ image_base64: str
19
+
20
+ # --- App State and Lifespan ---
21
+ app_state = {}
22
+
23
+ @asynccontextmanager
24
+ async def lifespan(app: FastAPI):
25
+ # Load the model on startup
26
+ hf_token = os.getenv("HF_TOKEN")
27
+ if not hf_token:
28
+ raise RuntimeError("HF_TOKEN environment variable not set!")
29
+
30
+ model_id = "rxmha125/sdxl-base-1.0-private" # <-- YOUR PRIVATE MODEL ID
31
+ print(f"Loading model: {model_id}")
32
+
33
+ pipe = AutoPipelineForText2Image.from_pretrained(
34
+ model_id,
35
+ torch_dtype=torch.float16,
36
+ variant="fp16",
37
+ use_safetensors=True,
38
+ token=hf_token
39
+ ).to("cuda")
40
+
41
+ # Optimization for speed and memory
42
+ pipe.enable_model_cpu_offload()
43
+
44
+ app_state["pipe"] = pipe
45
+ print("Model loaded successfully.")
46
+ yield
47
+ # Clean up on shutdown
48
+ app_state.clear()
49
+ print("Resources cleaned up.")
50
+
51
+ # --- FastAPI App ---
52
+ app = FastAPI(lifespan=lifespan)
53
+
54
+ @app.get("/")
55
+ def root():
56
+ return {"status": "Text-to-Image API is running"}
57
+
58
+ @app.post("/generate-image", response_model=ImageResponse)
59
+ def generate_image(request: ImageRequest):
60
+ if "pipe" not in app_state:
61
+ raise HTTPException(status_code=503, detail="Model is not ready.")
62
+
63
+ pipe = app_state["pipe"]
64
+
65
+ print(f"Generating image for prompt: '{request.prompt}'")
66
+ try:
67
+ # Generate the image
68
+ image = pipe(
69
+ prompt=request.prompt,
70
+ negative_prompt=request.negative_prompt,
71
+ num_inference_steps=request.steps
72
+ ).images[0]
73
+
74
+ # Convert image to Base64
75
+ buffer = io.BytesIO()
76
+ image.save(buffer, format="PNG")
77
+ img_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
78
+
79
+ return ImageResponse(image_base64=img_str)
80
+
81
+ except Exception as e:
82
+ print(f"Error during image generation: {e}")
83
+ raise HTTPException(status_code=500, detail=str(e))
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ python-multipart
4
+ diffusers>=0.25.0
5
+ transformers
6
+ accelerate
7
+ torch --extra-index-url https://download.pytorch.org/whl/cu121