Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -594,10 +594,8 @@ async def get_completion(payload: Payload, request: Request, authenticated: bool
|
|
| 594 |
@lru_cache(maxsize=256)
|
| 595 |
def cached_url(url: str):
|
| 596 |
return url
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
@router.get("/images/{prompt:path}")
|
| 601 |
async def create_image(
|
| 602 |
prompt: str,
|
| 603 |
request: Request,
|
|
@@ -606,7 +604,6 @@ async def create_image(
|
|
| 606 |
if not server_status:
|
| 607 |
raise HTTPException(status_code=503, detail="Server is under maintenance.")
|
| 608 |
|
| 609 |
-
# forward all GET params
|
| 610 |
query = request.url.query
|
| 611 |
|
| 612 |
base = "https://image.pollinations.ai/prompt/"
|
|
@@ -614,12 +611,11 @@ async def create_image(
|
|
| 614 |
if query:
|
| 615 |
final_url += f"?{query}"
|
| 616 |
|
| 617 |
-
# caching
|
| 618 |
final_url = cached_url(final_url)
|
| 619 |
|
| 620 |
try:
|
| 621 |
-
|
| 622 |
-
|
| 623 |
|
| 624 |
if resp.status_code != 200:
|
| 625 |
raise HTTPException(status_code=resp.status_code, detail="Image generation failed.")
|
|
@@ -634,7 +630,6 @@ async def create_image(
|
|
| 634 |
|
| 635 |
except Exception as e:
|
| 636 |
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
|
| 637 |
-
|
| 638 |
@app.get("/usage")
|
| 639 |
async def get_usage_json(days: int = 7):
|
| 640 |
return usage_tracker.get_usage_summary(days)
|
|
|
|
| 594 |
@lru_cache(maxsize=256)
|
| 595 |
def cached_url(url: str):
|
| 596 |
return url
|
| 597 |
+
|
| 598 |
+
@app.get("/images/{prompt:path}")
|
|
|
|
|
|
|
| 599 |
async def create_image(
|
| 600 |
prompt: str,
|
| 601 |
request: Request,
|
|
|
|
| 604 |
if not server_status:
|
| 605 |
raise HTTPException(status_code=503, detail="Server is under maintenance.")
|
| 606 |
|
|
|
|
| 607 |
query = request.url.query
|
| 608 |
|
| 609 |
base = "https://image.pollinations.ai/prompt/"
|
|
|
|
| 611 |
if query:
|
| 612 |
final_url += f"?{query}"
|
| 613 |
|
|
|
|
| 614 |
final_url = cached_url(final_url)
|
| 615 |
|
| 616 |
try:
|
| 617 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 618 |
+
resp = await client.get(final_url)
|
| 619 |
|
| 620 |
if resp.status_code != 200:
|
| 621 |
raise HTTPException(status_code=resp.status_code, detail="Image generation failed.")
|
|
|
|
| 630 |
|
| 631 |
except Exception as e:
|
| 632 |
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
|
|
|
|
| 633 |
@app.get("/usage")
|
| 634 |
async def get_usage_json(days: int = 7):
|
| 635 |
return usage_tracker.get_usage_summary(days)
|