Spaces:
Running
Running
apply python formatting
Browse files- app/main.py +6 -21
- app/model.py +3 -8
app/main.py
CHANGED
|
@@ -11,19 +11,15 @@ app = FastAPI(
|
|
| 11 |
docs_url="/docs",
|
| 12 |
redoc_url="/redoc",
|
| 13 |
openapi_url="/openapi.json",
|
| 14 |
-
swagger_ui_parameters={
|
| 15 |
-
"tryItOutEnabled": True
|
| 16 |
-
}
|
| 17 |
)
|
| 18 |
|
| 19 |
|
| 20 |
@app.get("/")
|
| 21 |
def root():
|
| 22 |
-
return {
|
| 23 |
-
|
| 24 |
-
}
|
| 25 |
|
| 26 |
-
|
| 27 |
@app.get("/generate")
|
| 28 |
def generate(prompt: str):
|
| 29 |
image = generate_image(prompt)
|
|
@@ -31,23 +27,12 @@ def generate(prompt: str):
|
|
| 31 |
output_dir = Path("outputs")
|
| 32 |
output_dir.mkdir(exist_ok=True)
|
| 33 |
|
| 34 |
-
safe_prompt = re.sub(
|
| 35 |
-
r'[\\/*?:"<>|]',
|
| 36 |
-
"_",
|
| 37 |
-
prompt
|
| 38 |
-
)[:40]
|
| 39 |
|
| 40 |
-
filename = (
|
| 41 |
-
f"{safe_prompt}-"
|
| 42 |
-
f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
|
| 43 |
-
)
|
| 44 |
|
| 45 |
path = output_dir / filename
|
| 46 |
|
| 47 |
image.save(path)
|
| 48 |
|
| 49 |
-
return FileResponse(
|
| 50 |
-
path=path,
|
| 51 |
-
media_type="image/png",
|
| 52 |
-
filename=filename
|
| 53 |
-
)
|
|
|
|
| 11 |
docs_url="/docs",
|
| 12 |
redoc_url="/redoc",
|
| 13 |
openapi_url="/openapi.json",
|
| 14 |
+
swagger_ui_parameters={"tryItOutEnabled": True},
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
def root():
|
| 20 |
+
return {"message": "Tiny SD API is running"}
|
| 21 |
+
|
|
|
|
| 22 |
|
|
|
|
| 23 |
@app.get("/generate")
|
| 24 |
def generate(prompt: str):
|
| 25 |
image = generate_image(prompt)
|
|
|
|
| 27 |
output_dir = Path("outputs")
|
| 28 |
output_dir.mkdir(exist_ok=True)
|
| 29 |
|
| 30 |
+
safe_prompt = re.sub(r'[\\/*?:"<>|]', "_", prompt)[:40]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
filename = f"{safe_prompt}-" f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
path = output_dir / filename
|
| 35 |
|
| 36 |
image.save(path)
|
| 37 |
|
| 38 |
+
return FileResponse(path=path, media_type="image/png", filename=filename)
|
|
|
|
|
|
|
|
|
|
|
|
app/model.py
CHANGED
|
@@ -8,7 +8,7 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 8 |
cache_dir="./models",
|
| 9 |
# local_files_only=True,
|
| 10 |
torch_dtype=torch.float32,
|
| 11 |
-
safety_checker=None
|
| 12 |
)
|
| 13 |
|
| 14 |
pipe.enable_attention_slicing()
|
|
@@ -19,11 +19,6 @@ pipe.to("cpu")
|
|
| 19 |
@torch.inference_mode()
|
| 20 |
def generate_image(prompt):
|
| 21 |
|
| 22 |
-
image = pipe(
|
| 23 |
-
prompt,
|
| 24 |
-
height=256,
|
| 25 |
-
width=256,
|
| 26 |
-
num_inference_steps=10
|
| 27 |
-
).images[0]
|
| 28 |
|
| 29 |
-
return image
|
|
|
|
| 8 |
cache_dir="./models",
|
| 9 |
# local_files_only=True,
|
| 10 |
torch_dtype=torch.float32,
|
| 11 |
+
safety_checker=None,
|
| 12 |
)
|
| 13 |
|
| 14 |
pipe.enable_attention_slicing()
|
|
|
|
| 19 |
@torch.inference_mode()
|
| 20 |
def generate_image(prompt):
|
| 21 |
|
| 22 |
+
image = pipe(prompt, height=256, width=256, num_inference_steps=10).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
return image
|