Spaces:
Sleeping
Sleeping
perf(cache): cache available models list for 24 hours
Browse files
services/genai_service.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
| 4 |
|
| 5 |
load_dotenv()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def configure_genai(model_name="gemini-2.5-flash"):
|
| 9 |
"""
|
|
@@ -28,11 +33,14 @@ def configure_genai(model_name="gemini-2.5-flash"):
|
|
| 28 |
return None
|
| 29 |
|
| 30 |
|
|
|
|
| 31 |
def list_available_models():
|
| 32 |
"""
|
| 33 |
Lista os modelos disponíveis na API dinamicamente.
|
| 34 |
Retorna uma lista de dicionários {'id': name, 'name': display_name}.
|
|
|
|
| 35 |
"""
|
|
|
|
| 36 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 37 |
if not api_key:
|
| 38 |
return []
|
|
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from cachetools import cached, TTLCache
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
# Configura cache para a lista de modelos: dura 24 horas (86400 segundos)
|
| 9 |
+
# maxsize=1 pois só armazenamos o resultado de uma única chamada
|
| 10 |
+
models_cache = TTLCache(maxsize=1, ttl=86400)
|
| 11 |
+
|
| 12 |
|
| 13 |
def configure_genai(model_name="gemini-2.5-flash"):
|
| 14 |
"""
|
|
|
|
| 33 |
return None
|
| 34 |
|
| 35 |
|
| 36 |
+
@cached(models_cache)
|
| 37 |
def list_available_models():
|
| 38 |
"""
|
| 39 |
Lista os modelos disponíveis na API dinamicamente.
|
| 40 |
Retorna uma lista de dicionários {'id': name, 'name': display_name}.
|
| 41 |
+
Resultado em cache por 24h.
|
| 42 |
"""
|
| 43 |
+
print("🔄 Consultando API do Google para atualizar lista de modelos...")
|
| 44 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 45 |
if not api_key:
|
| 46 |
return []
|