Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,9 +20,12 @@ app.add_middleware(
|
|
| 20 |
|
| 21 |
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
|
| 22 |
|
| 23 |
-
RATE_LIMIT =
|
| 24 |
WINDOW_SECONDS = 60 * 60 * 24
|
| 25 |
ip_store = {} # { ip: { "count": int, "reset": timestamp } }
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
REASONING_KEYWORDS = [
|
| 28 |
# explicit reasoning requests
|
|
@@ -195,6 +198,29 @@ def extract_user_text(messages: list) -> str:
|
|
| 195 |
if m.get("role") == "user"
|
| 196 |
).lower()
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
def is_complex_reasoning(prompt: str) -> bool:
|
| 200 |
if len(prompt) > 800:
|
|
@@ -299,11 +325,14 @@ def check_chat_rate_limit(ip: str):
|
|
| 299 |
entry["count"] += 1
|
| 300 |
return entry["count"]
|
| 301 |
|
|
|
|
| 302 |
@app.get("/genimg/{prompt}")
|
| 303 |
-
async def generate_image(prompt: str, request: Request):
|
| 304 |
client_ip = request.client.host
|
| 305 |
check_rate_limit(client_ip)
|
| 306 |
-
|
|
|
|
|
|
|
| 307 |
if is_cinematic_image_prompt(prompt):
|
| 308 |
chosen_model = "flux"
|
| 309 |
else:
|
|
@@ -313,7 +342,7 @@ async def generate_image(prompt: str, request: Request):
|
|
| 313 |
|
| 314 |
url = f"https://gen.pollinations.ai/image/{prompt}?model={chosen_model}&key={PKEY}"
|
| 315 |
|
| 316 |
-
async with httpx.AsyncClient() as client:
|
| 317 |
response = await client.get(url)
|
| 318 |
|
| 319 |
if response.status_code != 200:
|
|
@@ -494,3 +523,16 @@ async def generate_text(request: Request):
|
|
| 494 |
)
|
| 495 |
|
| 496 |
raise HTTPException(500, "Unknown provider routing error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
OLLAMA_LIBRARY_URL = "https://ollama.com/library"
|
| 22 |
|
| 23 |
+
RATE_LIMIT = 50
|
| 24 |
WINDOW_SECONDS = 60 * 60 * 24
|
| 25 |
ip_store = {} # { ip: { "count": int, "reset": timestamp } }
|
| 26 |
+
AUDIO_RATE_LIMIT = 4
|
| 27 |
+
AUDIO_WINDOW_SECONDS = 60 * 60 * 24
|
| 28 |
+
audio_ip_store = {}
|
| 29 |
|
| 30 |
REASONING_KEYWORDS = [
|
| 31 |
# explicit reasoning requests
|
|
|
|
| 198 |
if m.get("role") == "user"
|
| 199 |
).lower()
|
| 200 |
|
| 201 |
+
def check_audio_rate_limit(ip: str):
|
| 202 |
+
now = time.time()
|
| 203 |
+
|
| 204 |
+
if ip not in audio_ip_store:
|
| 205 |
+
audio_ip_store[ip] = {
|
| 206 |
+
"count": 0,
|
| 207 |
+
"reset": now + AUDIO_WINDOW_SECONDS
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
entry = audio_ip_store[ip]
|
| 211 |
+
|
| 212 |
+
if now > entry["reset"]:
|
| 213 |
+
entry["count"] = 0
|
| 214 |
+
entry["reset"] = now + AUDIO_WINDOW_SECONDS
|
| 215 |
+
|
| 216 |
+
if entry["count"] >= AUDIO_RATE_LIMIT:
|
| 217 |
+
raise HTTPException(
|
| 218 |
+
status_code=429,
|
| 219 |
+
detail="Daily audio limit reached: 4 per IP"
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
entry["count"] += 1
|
| 223 |
+
|
| 224 |
|
| 225 |
def is_complex_reasoning(prompt: str) -> bool:
|
| 226 |
if len(prompt) > 800:
|
|
|
|
| 325 |
entry["count"] += 1
|
| 326 |
return entry["count"]
|
| 327 |
|
| 328 |
+
@app.post("/gen/image")
|
| 329 |
@app.get("/genimg/{prompt}")
|
| 330 |
+
async def generate_image(prompt: str = None, request: Request):
|
| 331 |
client_ip = request.client.host
|
| 332 |
check_rate_limit(client_ip)
|
| 333 |
+
timeout = httpx.Timeout(300.0, read=300.0)
|
| 334 |
+
if prompt is None:
|
| 335 |
+
prompt = (await request.json()).prompt
|
| 336 |
if is_cinematic_image_prompt(prompt):
|
| 337 |
chosen_model = "flux"
|
| 338 |
else:
|
|
|
|
| 342 |
|
| 343 |
url = f"https://gen.pollinations.ai/image/{prompt}?model={chosen_model}&key={PKEY}"
|
| 344 |
|
| 345 |
+
async with httpx.AsyncClient(timeout = timeout) as client:
|
| 346 |
response = await client.get(url)
|
| 347 |
|
| 348 |
if response.status_code != 200:
|
|
|
|
| 523 |
)
|
| 524 |
|
| 525 |
raise HTTPException(500, "Unknown provider routing error")
|
| 526 |
+
|
| 527 |
+
@app.post("/gen/sfx")
|
| 528 |
+
async def gensfx(request: Request):
|
| 529 |
+
client_ip = request.client.host
|
| 530 |
+
check_audio_rate_limit(client_ip)
|
| 531 |
+
prompt = (await request.json()).prompt
|
| 532 |
+
url = f"https://gen.pollinations.ai/audio/{prompt}?model=elevenaudio&key={PKEY}"
|
| 533 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
| 534 |
+
response = await client.get(url)
|
| 535 |
+
return Response(
|
| 536 |
+
response.content
|
| 537 |
+
media_type="audio/mpeg"
|
| 538 |
+
)
|