Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import httpx
|
| 5 |
from bs4 import BeautifulSoup
|
|
@@ -16,6 +17,66 @@ app.add_middleware(
|
|
| 16 |
|
| 17 |
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@app.get("/models")
|
| 20 |
async def get_models() -> List[Dict]:
|
| 21 |
async with httpx.AsyncClient() as client:
|
|
@@ -45,6 +106,4 @@ async def get_models() -> List[Dict]:
|
|
| 45 |
"link": link.get("href") if link else None,
|
| 46 |
})
|
| 47 |
|
| 48 |
-
print(models)
|
| 49 |
-
|
| 50 |
return models
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import httpx
|
| 6 |
from bs4 import BeautifulSoup
|
|
|
|
| 17 |
|
| 18 |
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
|
| 19 |
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# RATE LIMITING (25 req/day/IP)
|
| 22 |
+
# -----------------------------
|
| 23 |
+
RATE_LIMIT = 25
|
| 24 |
+
WINDOW_SECONDS = 60 * 60 * 24 # 24 hours
|
| 25 |
+
ip_store = {} # { ip: { "count": int, "reset": timestamp } }
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def check_rate_limit(ip: str):
|
| 29 |
+
now = time.time()
|
| 30 |
+
|
| 31 |
+
if ip not in ip_store:
|
| 32 |
+
ip_store[ip] = {"count": 0, "reset": now + WINDOW_SECONDS}
|
| 33 |
+
|
| 34 |
+
entry = ip_store[ip]
|
| 35 |
+
|
| 36 |
+
if now > entry["reset"]:
|
| 37 |
+
entry["count"] = 0
|
| 38 |
+
entry["reset"] = now + WINDOW_SECONDS
|
| 39 |
+
|
| 40 |
+
if entry["count"] >= RATE_LIMIT:
|
| 41 |
+
raise HTTPException(
|
| 42 |
+
status_code=429,
|
| 43 |
+
detail="Daily limit reached: 25 images per IP"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
entry["count"] += 1
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# -----------------------------
|
| 50 |
+
# IMAGE GENERATION ENDPOINT
|
| 51 |
+
# -----------------------------
|
| 52 |
+
PKEY = os.getenv("POLLINATIONS_KEY", "") # ensure this is set in your environment
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.get("/genimg/{prompt}")
|
| 56 |
+
async def generate_image(prompt: str, request: Request):
|
| 57 |
+
client_ip = request.client.host
|
| 58 |
+
check_rate_limit(client_ip)
|
| 59 |
+
|
| 60 |
+
url = f"https://gen.pollinations.ai/image/{prompt}?model=zimage&key={PKEY}"
|
| 61 |
+
|
| 62 |
+
async with httpx.AsyncClient() as client:
|
| 63 |
+
response = await client.get(url)
|
| 64 |
+
|
| 65 |
+
if response.status_code != 200:
|
| 66 |
+
raise HTTPException(
|
| 67 |
+
status_code=500,
|
| 68 |
+
detail=f"Pollinations error: {response.status_code}"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return Response(
|
| 72 |
+
content=response.content,
|
| 73 |
+
media_type=response.headers.get("content-type", "image/png")
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# EXISTING MODELS SCRAPER
|
| 79 |
+
# -----------------------------
|
| 80 |
@app.get("/models")
|
| 81 |
async def get_models() -> List[Dict]:
|
| 82 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 106 |
"link": link.get("href") if link else None,
|
| 107 |
})
|
| 108 |
|
|
|
|
|
|
|
| 109 |
return models
|