Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files19 unmodified lines20212223242512 unmodified lines3839404142434424 unmodified lines697071727374757677787980818219 unmodified lines# ── HuggingFace Inference API ─────────────────────────────_hf_client = Nonedef _init_hf():global _hf_clienttoken = os.getenv("HF_TOKEN")12 unmodified lineslogger.error(f"HF Inference API init failed: {e}")return False# ── Groq fallback (works locally, may be blocked on HF Spaces) ──_groq_client = Nonedef _init_groq():24 unmodified lines)return response.choices[0].message.contentdef _call_groq(messages: list) -> str:"""Call Groq as fallback."""response = _groq_client.chat.completions.create(model="llama-3.3-70b-versatile",messages=messages,temperature=0.3,max_tokens=1500)return response.choices[0].message.content19 unmodified lines20212223242526272812 unmodified lines4142434445464748495051525354555657585960616263646524 unmodified lines9091929394959697989910010110210310410510610710810911011111211311411511611711811912019 unmodified lines# ── HuggingFace Inference API ─────────────────────────────_hf_client = None# ── OpenRouter (free tier, reliable fallback) ──────────────_openrouter_client = Nonedef _init_hf():global _hf_clienttoken = os.getenv("HF_TOKEN")12 unmodified lineslogger.error(f"HF Inference API init failed: {e}")return Falsedef _init_openrouter():global _openrouter_clientapi_key = os.getenv("OPENROUTER_API_KEY")if not api_key:return Falsetry:from openai import OpenAI_openrouter_client = OpenAI(base_url="https://openrouter.ai/api/v1",api_key=api_key,)logger.info("OpenRouter ready as fallback")return Trueexcept Exception as e:logger.error(f"OpenRouter init failed: {e}")return False# ── Groq fallback (works locally, may be blocked on HF Spaces) ──_openrouter_ready = _init_openrouter()_groq_client = Nonedef _init_groq():24 unmodified lines)return response.choices[0].message.contentopenrouter(messages: list) -> str:"""Call OpenRouter free tier."""response = _openrouter_client.chat.completions.create(model="meta-llama/llama-3.3-70b-instruct:free",messages=messages,max_tokens=1500,temperature=0.3,)return response.choices[0].message.contentdef _call_def _call_groq(messages: list) -> str:"""Call Groq as fthen OpenRouter, then Groq."""if _hf_ready and _hf_client:try:return _call_hf(messages)except Exception as e:logger.warning(f"HF Inference failed: {e}, trying OpenRouter")if _openrouter_ready and _openrouter_client:try:return _call_openrouter(messages)except Exception as e:logger.warning(f"OpenRouter)return response.choices[0].message.content
src/llm.py+44-6