Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from functools import partial
|
| 2 |
from os import environ
|
| 3 |
from typing import Callable, Coroutine
|
|
@@ -8,7 +9,6 @@ from fastapi.responses import HTMLResponse
|
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from httpx import AsyncClient, RequestError, Timeout
|
| 10 |
from starlette.types import Receive, Scope, Send
|
| 11 |
-
from datetime import datetime, timedelta
|
| 12 |
|
| 13 |
API_KEYS = [line for line in environ['API_KEYS'].strip().split('\n') if line and line.startswith('sk-')]
|
| 14 |
COMPLETIONS_URL = 'https://openrouter.ai/api/v1/chat/completions'
|
|
@@ -19,12 +19,12 @@ class Cache:
|
|
| 19 |
def __init__(self, expire: timedelta):
|
| 20 |
self.expire = expire
|
| 21 |
self.cache = {}
|
| 22 |
-
self.timestamp = datetime.
|
| 23 |
|
| 24 |
async def get(self, key):
|
| 25 |
-
if datetime.
|
| 26 |
self.cache.clear()
|
| 27 |
-
self.timestamp = datetime.
|
| 28 |
return self.cache.get(key)
|
| 29 |
|
| 30 |
async def set(self, key, value):
|
|
|
|
| 1 |
+
from datetime import datetime, timedelta
|
| 2 |
from functools import partial
|
| 3 |
from os import environ
|
| 4 |
from typing import Callable, Coroutine
|
|
|
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from httpx import AsyncClient, RequestError, Timeout
|
| 11 |
from starlette.types import Receive, Scope, Send
|
|
|
|
| 12 |
|
| 13 |
API_KEYS = [line for line in environ['API_KEYS'].strip().split('\n') if line and line.startswith('sk-')]
|
| 14 |
COMPLETIONS_URL = 'https://openrouter.ai/api/v1/chat/completions'
|
|
|
|
| 19 |
def __init__(self, expire: timedelta):
|
| 20 |
self.expire = expire
|
| 21 |
self.cache = {}
|
| 22 |
+
self.timestamp = datetime.now()
|
| 23 |
|
| 24 |
async def get(self, key):
|
| 25 |
+
if datetime.now() - self.timestamp > self.expire:
|
| 26 |
self.cache.clear()
|
| 27 |
+
self.timestamp = datetime.now()
|
| 28 |
return self.cache.get(key)
|
| 29 |
|
| 30 |
async def set(self, key, value):
|