jawwad1234 commited on
Commit
6d31d5e
·
verified ·
1 Parent(s): aeccf68

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -5
main.py CHANGED
@@ -1,14 +1,17 @@
1
- from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  import os, requests
4
 
5
  app = FastAPI()
6
 
7
- # Pull your secret from the HF Spaces Secrets
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
 
9
  if not GROQ_API_KEY:
10
- # If this triggers, check Settings Secrets → GROQ_API_KEY
11
- raise RuntimeError("GROQ_API_KEY environment variable not set")
 
12
 
13
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
14
 
@@ -17,7 +20,11 @@ class ChatRequest(BaseModel):
17
  message: str
18
 
19
  @app.post("/chat")
20
- def chat(req: ChatRequest):
 
 
 
 
21
  headers = {
22
  "Authorization": f"Bearer {GROQ_API_KEY}",
23
  "Content-Type": "application/json"
 
1
+ from fastapi import FastAPI, HTTPException, Header
2
  from pydantic import BaseModel
3
  import os, requests
4
 
5
  app = FastAPI()
6
 
7
+ # Get both secrets from HF settings
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
9
+ ACCESS_KEY = os.getenv("API_key") # Your custom VirtuComm API key
10
+
11
  if not GROQ_API_KEY:
12
+ raise RuntimeError("GROQ_API_KEY not set in Hugging Face secrets.")
13
+ if not ACCESS_KEY:
14
+ raise RuntimeError("API_key not set in Hugging Face secrets.")
15
 
16
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
17
 
 
20
  message: str
21
 
22
  @app.post("/chat")
23
+ def chat(req: ChatRequest, x_api_key: str = Header(None)):
24
+ # Check that client included the correct API key
25
+ if x_api_key != ACCESS_KEY:
26
+ raise HTTPException(status_code=401, detail="Invalid API key.")
27
+
28
  headers = {
29
  "Authorization": f"Bearer {GROQ_API_KEY}",
30
  "Content-Type": "application/json"