kreemyyyy commited on
Commit
b64f151
·
verified ·
1 Parent(s): 997c64e

Upload deepseek_client.py

Browse files
Files changed (1) hide show
  1. deepseek_client.py +11 -4
deepseek_client.py CHANGED
@@ -6,15 +6,22 @@ load_dotenv()
6
 
7
  # Get API key from Streamlit secrets or environment
8
  def get_api_key():
9
- if hasattr(st, 'secrets') and "DEEPSEEK_API_KEY" in st.secrets:
10
- return st.secrets["DEEPSEEK_API_KEY"]
 
 
 
11
  return os.getenv("DEEPSEEK_API_KEY")
12
 
13
- DEEPSEEK_API_KEY = get_api_key()
 
14
  BASE = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
15
 
16
  def chat(messages, model="deepseek-chat", temperature=0.9):
17
- headers = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"}
 
 
 
18
  payload = {"model": model, "messages": messages, "temperature": temperature}
19
  r = requests.post(f"{BASE}/chat/completions", headers=headers, data=json.dumps(payload), timeout=60)
20
  r.raise_for_status()
 
6
 
7
  # Get API key from Streamlit secrets or environment
8
  def get_api_key():
9
+ try:
10
+ if hasattr(st, 'secrets') and "DEEPSEEK_API_KEY" in st.secrets:
11
+ return st.secrets["DEEPSEEK_API_KEY"]
12
+ except:
13
+ pass
14
  return os.getenv("DEEPSEEK_API_KEY")
15
 
16
+ # Don't call get_api_key() at import time - call it when needed
17
+ DEEPSEEK_API_KEY = None
18
  BASE = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
19
 
20
  def chat(messages, model="deepseek-chat", temperature=0.9):
21
+ api_key = get_api_key()
22
+ if not api_key:
23
+ raise ValueError("DEEPSEEK_API_KEY not found. Please set it in Hugging Face Space secrets or environment variables.")
24
+ headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
25
  payload = {"model": model, "messages": messages, "temperature": temperature}
26
  r = requests.post(f"{BASE}/chat/completions", headers=headers, data=json.dumps(payload), timeout=60)
27
  r.raise_for_status()