Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -146,89 +146,6 @@ available_model_ids = [
|
|
146 |
"llama-3.1-8b", "gemini-1.5-flash", "mixtral-8x7b" , "command-r","gemini-pro",
|
147 |
"gpt-3.5-turbo", "command"
|
148 |
]
|
149 |
-
@app.get("/images/generations")
|
150 |
-
async def generate_image(
|
151 |
-
prompt: str,
|
152 |
-
model: str = "flux", # Default model
|
153 |
-
seed: Optional[int] = None,
|
154 |
-
width: Optional[int] = None,
|
155 |
-
height: Optional[int] = None,
|
156 |
-
nologo: Optional[bool] = True,
|
157 |
-
private: Optional[bool] = None,
|
158 |
-
enhance: Optional[bool] = None,
|
159 |
-
):
|
160 |
-
"""
|
161 |
-
Generate an image using the Image Generation API.
|
162 |
-
|
163 |
-
Example usage:
|
164 |
-
- /images/generations?prompt=beautiful+sunset&model=flux&width=1024&height=768
|
165 |
-
"""
|
166 |
-
# Ensure the IMAGE_ENDPOINT is configured
|
167 |
-
if not image_endpoint:
|
168 |
-
raise HTTPException(
|
169 |
-
status_code=500,
|
170 |
-
detail="Image endpoint not configured in environment variables."
|
171 |
-
)
|
172 |
-
|
173 |
-
# Check if prompt is valid
|
174 |
-
if not prompt or len(prompt.strip()) == 0:
|
175 |
-
raise HTTPException(
|
176 |
-
status_code=400,
|
177 |
-
detail="Prompt is required and cannot be empty. Example: prompt=beautiful+sunset"
|
178 |
-
)
|
179 |
-
|
180 |
-
# Construct the URL with the prompt
|
181 |
-
url = f"{image_endpoint}/{prompt.strip()}"
|
182 |
-
|
183 |
-
# Prepare query parameters
|
184 |
-
params = {
|
185 |
-
"model": model,
|
186 |
-
"seed": seed,
|
187 |
-
"width": width,
|
188 |
-
"height": height,
|
189 |
-
"nologo": nologo,
|
190 |
-
"private": private,
|
191 |
-
"enhance": enhance,
|
192 |
-
}
|
193 |
-
# Remove keys with `None` values to avoid invalid params
|
194 |
-
params = {k: v for k, v in params.items() if v is not None}
|
195 |
-
|
196 |
-
try:
|
197 |
-
# Send GET request to the image generation endpoint
|
198 |
-
async with httpx.AsyncClient() as client:
|
199 |
-
response = await client.get(url, params=params)
|
200 |
-
|
201 |
-
# Handle non-successful HTTP status codes
|
202 |
-
if response.status_code == 400:
|
203 |
-
raise HTTPException(
|
204 |
-
status_code=400,
|
205 |
-
detail="Invalid request to the image generation API. Please check your prompt and parameters."
|
206 |
-
)
|
207 |
-
elif response.status_code == 500:
|
208 |
-
raise HTTPException(
|
209 |
-
status_code=500,
|
210 |
-
detail="Server error occurred while generating the image. Please try again later."
|
211 |
-
)
|
212 |
-
elif response.status_code != 200:
|
213 |
-
raise HTTPException(
|
214 |
-
status_code=response.status_code,
|
215 |
-
detail=f"Error generating image: {response.text}"
|
216 |
-
)
|
217 |
-
|
218 |
-
# Return the image as a file response
|
219 |
-
return StreamingResponse(response.aiter_bytes(), media_type="image/jpeg")
|
220 |
-
|
221 |
-
except httpx.TimeoutException:
|
222 |
-
raise HTTPException(
|
223 |
-
status_code=504,
|
224 |
-
detail="The request to the image generation API timed out. Please try again later."
|
225 |
-
)
|
226 |
-
except httpx.RequestError as e:
|
227 |
-
# Handle other request errors
|
228 |
-
raise HTTPException(
|
229 |
-
status_code=500,
|
230 |
-
detail=f"Error contacting the image endpoint: {str(e)}"
|
231 |
-
)
|
232 |
@app.post("/chat/completions")
|
233 |
@app.post("/v1/chat/completions")
|
234 |
async def get_completion(payload: Payload,request: Request):
|
@@ -284,6 +201,111 @@ async def get_completion(payload: Payload,request: Request):
|
|
284 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
285 |
|
286 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
@app.get("/playground", response_class=HTMLResponse)
|
288 |
async def playground():
|
289 |
# Open and read the content of playground.html (in the same folder as the app)
|
|
|
146 |
"llama-3.1-8b", "gemini-1.5-flash", "mixtral-8x7b" , "command-r","gemini-pro",
|
147 |
"gpt-3.5-turbo", "command"
|
148 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
@app.post("/chat/completions")
|
150 |
@app.post("/v1/chat/completions")
|
151 |
async def get_completion(payload: Payload,request: Request):
|
|
|
201 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
202 |
|
203 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|
204 |
+
@app.get("/images/generations")
|
205 |
+
async def generate_image(
|
206 |
+
prompt: str,
|
207 |
+
model: str = "flux", # Default model
|
208 |
+
seed: Optional[int] = None,
|
209 |
+
width: Optional[int] = None,
|
210 |
+
height: Optional[int] = None,
|
211 |
+
nologo: Optional[bool] = None,
|
212 |
+
private: Optional[bool] = None,
|
213 |
+
enhance: Optional[bool] = None,
|
214 |
+
):
|
215 |
+
"""
|
216 |
+
Generate an image using the Image Generation API.
|
217 |
+
"""
|
218 |
+
# Ensure the IMAGE_ENDPOINT is configured
|
219 |
+
image_endpoint = os.getenv("IMAGE_ENDPOINT", "https://image.pollinations.ai/prompt")
|
220 |
+
if not image_endpoint:
|
221 |
+
raise HTTPException(status_code=500, detail="Image endpoint not configured in environment variables.")
|
222 |
+
|
223 |
+
# Construct the URL with the prompt
|
224 |
+
url = f"{image_endpoint}/{prompt}"
|
225 |
+
|
226 |
+
# Prepare query parameters
|
227 |
+
params = {
|
228 |
+
"model": model,
|
229 |
+
"seed": seed,
|
230 |
+
"width": width,
|
231 |
+
"height": height,
|
232 |
+
"nologo": nologo,
|
233 |
+
"private": private,
|
234 |
+
"enhance": enhance,
|
235 |
+
}
|
236 |
+
# Remove keys with `None` values to avoid invalid params
|
237 |
+
params = {k: v for k, v in params.items() if v is not None}
|
238 |
+
|
239 |
+
try:
|
240 |
+
# Send GET request to the image generation endpoint
|
241 |
+
async with httpx.AsyncClient() as client:
|
242 |
+
response = await client.get(url, params=params)
|
243 |
+
|
244 |
+
# If the response is not successful, raise an error
|
245 |
+
if response.status_code != 200:
|
246 |
+
raise HTTPException(
|
247 |
+
status_code=response.status_code,
|
248 |
+
detail=f"Error generating image: {response.text}"
|
249 |
+
)
|
250 |
+
|
251 |
+
# Return the image as a file response
|
252 |
+
return StreamingResponse(response.iter_content(), media_type="image/jpeg")
|
253 |
+
|
254 |
+
except httpx.RequestError as e:
|
255 |
+
# Handle request errors
|
256 |
+
raise HTTPException(status_code=500, detail=f"Error contacting the image endpoint: {str(e)}")
|
257 |
+
@app.get("/images/generations")
|
258 |
+
async def generate_image(
|
259 |
+
prompt: str,
|
260 |
+
model: str = "flux", # Default model
|
261 |
+
seed: Optional[int] = None,
|
262 |
+
width: Optional[int] = None,
|
263 |
+
height: Optional[int] = None,
|
264 |
+
nologo: Optional[bool] = True,
|
265 |
+
private: Optional[bool] = None,
|
266 |
+
enhance: Optional[bool] = None,
|
267 |
+
):
|
268 |
+
"""
|
269 |
+
Generate an image using the Image Generation API.
|
270 |
+
"""
|
271 |
+
# Ensure the IMAGE_ENDPOINT is configured
|
272 |
+
if not image_endpoint:
|
273 |
+
raise HTTPException(status_code=500, detail="Image endpoint not configured in environment variables.")
|
274 |
+
|
275 |
+
# Construct the URL with the prompt
|
276 |
+
url = f"{image_endpoint}/{prompt}"
|
277 |
+
|
278 |
+
# Prepare query parameters
|
279 |
+
params = {
|
280 |
+
"model": model,
|
281 |
+
"seed": seed,
|
282 |
+
"width": width,
|
283 |
+
"height": height,
|
284 |
+
"nologo": nologo,
|
285 |
+
"private": private,
|
286 |
+
"enhance": enhance,
|
287 |
+
}
|
288 |
+
# Remove keys with None values to avoid invalid params
|
289 |
+
params = {k: v for k, v in params.items() if v is not None}
|
290 |
+
|
291 |
+
try:
|
292 |
+
# Send GET request to the image generation endpoint
|
293 |
+
async with httpx.AsyncClient() as client:
|
294 |
+
response = await client.get(url, params=params)
|
295 |
+
|
296 |
+
# If the response is not successful, raise an error
|
297 |
+
if response.status_code != 200:
|
298 |
+
raise HTTPException(
|
299 |
+
status_code=response.status_code,
|
300 |
+
detail=f"Error generating image: {response.text}"
|
301 |
+
)
|
302 |
+
|
303 |
+
# Return the image as a file response
|
304 |
+
return StreamingResponse(response.iter_content(), media_type="image/jpeg")
|
305 |
+
|
306 |
+
except httpx.RequestError as e:
|
307 |
+
# Handle request errors
|
308 |
+
raise HTTPException(status_code=500, detail=f"Error contacting the image endpoint: {str(e)}")
|
309 |
@app.get("/playground", response_class=HTMLResponse)
|
310 |
async def playground():
|
311 |
# Open and read the content of playground.html (in the same folder as the app)
|