Spaces:
Sleeping
Sleeping
File size: 1,599 Bytes
f6174cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import sys
import os
# Forzar la prioridad del entorno virtual si existe (útil en Codespaces)
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
venv_site_packages = os.path.join(base_path, ".venv", "lib", "python3.12", "site-packages")
if os.path.exists(venv_site_packages) and venv_site_packages not in sys.path:
sys.path.insert(0, venv_site_packages)
try:
from google import genai
except ImportError:
# Fallback para entornos donde el namespace es problemático
try:
import google_genai as genai
except ImportError:
pass
# Inicializar el cliente con la nueva sintaxis oficial
def get_client():
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key or api_key.strip() == "":
return None
try:
return genai.Client(api_key=api_key)
except Exception:
return None
def ask_gemini(prompt: str):
client = get_client()
if not client:
return "⚠️ **Error**: No se configuró la variable `GEMINI_API_KEY`. Por favor, configúrala en los Secrets o terminal."
try:
response = client.models.generate_content(
model='gemini-1.5-flash',
contents=prompt
)
return response.text
except Exception as e:
# Intento de fallback automático a otro modelo común
try:
response = client.models.generate_content(
model='gemini-1.5-pro',
contents=prompt
)
return response.text
except:
return f"❌ **Error en la API de Gemini**: {e}"
|