Spaces:
Paused
Paused
sachin
commited on
Commit
·
d70006d
1
Parent(s):
bf04d08
fix path
Browse files
server.py
CHANGED
@@ -6,6 +6,8 @@ from huggingface_hub import hf_hub_download, login
|
|
6 |
from safetensors.torch import load_file
|
7 |
from io import BytesIO
|
8 |
import os
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
@@ -74,6 +76,38 @@ async def generate_image(prompt: str):
|
|
74 |
except Exception as e:
|
75 |
return {"error": str(e)}
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
@app.get("/health")
|
78 |
async def health_check():
|
79 |
return {"status": "healthy"}
|
|
|
6 |
from safetensors.torch import load_file
|
7 |
from io import BytesIO
|
8 |
import os
|
9 |
+
import base64 # Added for encoding images as base64
|
10 |
+
from typing import List # Added for type hinting the list of prompts
|
11 |
|
12 |
app = FastAPI()
|
13 |
|
|
|
76 |
except Exception as e:
|
77 |
return {"error": str(e)}
|
78 |
|
79 |
+
# New endpoint to handle a list of prompts
|
80 |
+
@app.get("/generate_multiple")
|
81 |
+
async def generate_multiple_images(prompts: List[str]):
|
82 |
+
try:
|
83 |
+
# List to store base64-encoded images
|
84 |
+
generated_images = []
|
85 |
+
|
86 |
+
# Generate an image for each prompt
|
87 |
+
for prompt in prompts:
|
88 |
+
image = pipe(
|
89 |
+
prompt,
|
90 |
+
num_inference_steps=4,
|
91 |
+
guidance_scale=0
|
92 |
+
).images[0]
|
93 |
+
|
94 |
+
# Save image to buffer
|
95 |
+
buffer = BytesIO()
|
96 |
+
image.save(buffer, format="PNG")
|
97 |
+
buffer.seek(0)
|
98 |
+
|
99 |
+
# Encode the image as base64
|
100 |
+
image_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
101 |
+
generated_images.append({
|
102 |
+
"prompt": prompt,
|
103 |
+
"image_base64": image_base64
|
104 |
+
})
|
105 |
+
|
106 |
+
return {"images": generated_images}
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
return {"error": str(e)}
|
110 |
+
|
111 |
@app.get("/health")
|
112 |
async def health_check():
|
113 |
return {"status": "healthy"}
|