Spaces:
Sleeping
Sleeping
Update planmate/config.py
#1
by Memoona648 - opened
12234545678910111213141516171819202122232425262717 unmodified lines4546472948495031513352535455373839405657585960612 unmodified lines64656667686970717273747576775078798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130import osfrom dotenv import load_dotenvimport timeimport requestsfrom itertools import cycle
# Load .env if present (local dev convenience)load_dotenv()# Try to load .env (local dev only, ignored in production)try: from dotenv import load_dotenv load_dotenv()except ImportError: pass
# Streamlit-safe loggingdef log_message(msg, level="info"): try: import streamlit as st if level == "error": st.error(msg) elif level == "warning": st.warning(msg) else: st.info(msg) except ImportError: print(f"[{level.upper()}] {msg}")
APP_TITLE = "PlanMate"APP_TAGLINE = "AI Powered smart trip planner"17 unmodified lines# ---------- Hugging Face Secrets Configuration ----------def get_secret(key, default=None): """ Get secret from Hugging Face Spaces environment or fallback to local .env Get secret from environment first (production), fallback to .env for local dev. """ # Try to get from environment first (Hugging Face Spaces) value = os.getenv(key)
if value is None and default is not None: return default elif value is None: st.error(f"Missing required secret: {key}") st.info("Please add this secret in your Hugging Face Space settings.") st.stop() log_message(f"Missing required secret: {key}", level="error") raise RuntimeError(f"Missing required secret: {key}")
return value
def get_env(key: str) -> str:2 unmodified lines raise RuntimeError(f"Missing required environment variable: {key}") return val
# ---------- Gemini Key Rotation ----------gemini_keys = os.getenv("GEMINI_API_KEYS", "").split(",")gemini_keys = [k.strip() for k in gemini_keys if k.strip()]
if not gemini_keys: raise RuntimeError("No GEMINI_API_KEYS found in environment!")
gemini_cycle = cycle(gemini_keys)_current_gemini_key = next(gemini_cycle)
def get_gemini_key(): return get_secret("GEMINI_API_KEY") """Get the current Gemini API key""" return _current_gemini_key
def rotate_gemini_key(): """Rotate to the next Gemini API key (when quota exceeded)""" global _current_gemini_key _current_gemini_key = next(gemini_cycle) log_message(f"Rotated to new Gemini API key: {_current_gemini_key}", level="warning") return _current_gemini_key
# ---------- Gemini Helper Function ----------def make_gemini_request(prompt, model="gemini-2.0-flash-lite", max_retries=3): """ Make a Gemini API request with automatic key rotation on quota errors. """ import google.generativeai as genai
for attempt in range(max_retries): try: # Configure SDK with current key genai.configure(api_key=get_gemini_key())
# Create the model model_instance = genai.GenerativeModel(model)
# Generate response response = model_instance.generate_content(prompt) return response.text # or response.candidates, depending on usage
except Exception as e: error_message = str(e)
# Case 1: Quota exceeded (429) if "429" in error_message or "quota" in error_message.lower(): rotate_gemini_key() time.sleep(2) # short delay before retry continue
# Case 2: Network/HTTP error elif isinstance(e, requests.exceptions.RequestException): log_message(f"Network issue: {e}. Retrying...", level="warning") time.sleep(2) continue
# Any other error → stop immediately raise e
raise RuntimeError("Gemini request failed after rotating all keys")
# ---------- Other API Keys ----------def get_amadeus_credentials(): return get_secret("AMADEUS_CLIENT_ID"), get_secret("AMADEUS_CLIENT_SECRET")
planmate/config.py+87-11