topic_modelling / check_keys.py
aadisawant2912's picture
Update check_keys.py
3412a0d verified
"""
check_keys.py β€” paste into a HF Space file and read the startup logs,
OR add as a temporary check tab in app.py.
Run: python check_keys.py
"""
import os
print("\n" + "="*50)
print("KEY & MODEL HEALTH CHECK")
print("="*50)
# ── 1. Check raw env vars ─────────────────────────────────────────────────────
keys = {
"MISTRAL_API_KEY": os.getenv("MISTRAL_API_KEY"),
"GROQ_API_KEY": os.getenv("GROQ_API_KEY"),
"GEMINI_API_KEY": os.getenv("GEMINI_API_KEY"),
"GOOGLE_API_KEY": os.getenv("GOOGLE_API_KEY"),
"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"), # should be absent now
}
print("\n── Secrets present ──")
for k, v in keys.items():
status = "βœ… set" if v else "❌ missing"
masked = (v[:6] + "..." + v[-3:]) if v and len(v) > 9 else "(empty)"
print(f" {k:25s} {status} {masked if v else ''}")
# remap for google
if not os.getenv("GOOGLE_API_KEY") and os.getenv("GEMINI_API_KEY"):
os.environ["GOOGLE_API_KEY"] = os.environ["GEMINI_API_KEY"]
print("\n ↳ Remapped GEMINI_API_KEY β†’ GOOGLE_API_KEY")
# ── 2. Test Mistral ───────────────────────────────────────────────────────────
print("\n── Mistral (mistral-small-latest) ──")
try:
from langchain_mistralai import ChatMistralAI
from langchain_core.messages import HumanMessage
llm = ChatMistralAI(model="mistral-small-latest", temperature=0)
r = llm.invoke([HumanMessage(content="Reply with just: OK")])
print(f" βœ… Response: {r.content.strip()[:60]}")
except Exception as e:
print(f" ❌ FAILED: {e}")
# ── 3. Test Gemini ────────────────────────────────────────────────────────────
print("\n── Gemini (gemini-2.5-flash) ──")
try:
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
r = llm.invoke([HumanMessage(content="Reply with just: OK")])
print(f" βœ… Response: {r.content.strip()[:60]}")
except Exception as e:
print(f" ❌ FAILED: {e}")
# ── 4. Test Groq ──────────────────────────────────────────────────────────────
print("\n── Groq (llama-3.3-70b-versatile) ──")
try:
from langchain_groq import ChatGroq
llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
r = llm.invoke([HumanMessage(content="Reply with just: OK")])
print(f" βœ… Response: {r.content.strip()[:60]}")
except Exception as e:
print(f" ❌ FAILED: {e}")
# ── 5. Test MiniLM ───────────────────────────────────────────────────────────
print("\n── MiniLM (all-MiniLM-L6-v2) ──")
try:
from sentence_transformers import SentenceTransformer
m = SentenceTransformer("all-MiniLM-L6-v2")
e = m.encode(["test sentence"], normalize_embeddings=True)
print(f" βœ… Embedding shape: {e.shape} (expected: (1, 384))")
except Exception as e:
print(f" ❌ FAILED: {e}")
print("\n" + "="*50 + "\n")