Repository navigation
Expand file tree
/
Copy pathmy_handler.py
More file actions
59 lines (49 loc) · 1.98 KB
/
Copy pathmy_handler.py
File metadata and controls
59 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import runpod
import json
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
import base64
import torch
model_id = "stabilityai/stable-diffusion-3.5-large"
# Define model path inside the container
MODEL_PATH = "/app/model"
# Load the pre-downloaded model instead of fetching it again
scheduler = EulerDiscreteScheduler.from_pretrained(MODEL_PATH, subfolder="scheduler")
pipeline = StableDiffusionPipeline.from_pretrained(MODEL_PATH, scheduler=scheduler, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
print("Model is ready for use")
def handler(event, context=None):
# Parse input
try:
body = event.get("input", {})
prompt = body.get("prompt", "A futuristic cityscape at sunset")
except json.JSONDecodeError:
return {"statusCode": 400, "body": "Invalid JSON input."}
if not prompt:
return {"statusCode": 400, "body": "Prompt is required."}
# Generate image
try:
print(f"Generating image for prompt: {prompt}")
images = pipeline(prompt, num_inference_steps=50, guidance_scale=7.5).images
image_data = []
print("No. of images genereated = ", len(images))
for idx, img in enumerate(images):
# Convert image to a format suitable for response
img_path = f"/tmp/image_{idx}.png"
print(f"Saving image - {img_path}")
img.save(img_path)
with open(img_path, "rb") as image_file:
image_bytes = image_file.read()
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
image_data.append(image_base64)
break
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": image_data[0]
}
except Exception as e:
print(f"Error generating image: {e}")
return {"statusCode": 500, "body": str(e)}
# Debugging locally
if __name__ == "__main__":
runpod.serverless.start({'handler': handler})