Futuresony commited on
Commit
b8ad301
·
verified ·
1 Parent(s): 38f796a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -3,38 +3,36 @@ from fastapi import FastAPI, Request, HTTPException
3
  import uvicorn
4
  from gradio_client import Client
5
 
6
- # --- Load environment keys ---
7
- HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face token
8
- VALID_API_KEY = os.getenv("APP_API_KEY") # Your app security key
9
 
10
- # --- Connect to Hugging Face Space ---
11
- client = Client("Futuresony/Mr.Events", hf_token=HF_TOKEN)
12
 
13
- # --- Create FastAPI app ---
14
  app = FastAPI()
15
 
16
  @app.post("/chat")
17
  async def chat(request: Request):
18
  data = await request.json()
19
 
20
- # --- Check API Key from headers ---
21
- api_key = request.headers.get("X-API-Key")
22
  if api_key != VALID_API_KEY:
23
  raise HTTPException(status_code=403, detail="Invalid API Key")
24
 
25
- # --- Get user message ---
26
  user_message = data.get("message")
27
  if not user_message:
28
  raise HTTPException(status_code=400, detail="Message is required")
29
 
30
- # --- Call Hugging Face Space ---
31
  result = client.predict(
32
  query=user_message,
33
- api_name="/chat" # API name exposed by your Space
 
34
  )
35
 
36
  return {"response": result}
37
 
38
-
39
  if __name__ == "__main__":
40
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
3
  import uvicorn
4
  from gradio_client import Client
5
 
6
+ # Keys
7
+ VALID_API_KEY = os.getenv("APP_API_KEY") # for your FastAPI security
8
+ SPACE_API_KEY = os.getenv("SPACE_API_KEY") # for your hosted HF Space
9
 
10
+ # Connect to hosted space
11
+ client = Client("Futuresony/Mr.Events")
12
 
 
13
  app = FastAPI()
14
 
15
  @app.post("/chat")
16
  async def chat(request: Request):
17
  data = await request.json()
18
 
19
+ # API key check from headers or JSON
20
+ api_key = request.headers.get("X-API-Key") or data.get("api_key")
21
  if api_key != VALID_API_KEY:
22
  raise HTTPException(status_code=403, detail="Invalid API Key")
23
 
 
24
  user_message = data.get("message")
25
  if not user_message:
26
  raise HTTPException(status_code=400, detail="Message is required")
27
 
28
+ # Call your hosted Space
29
  result = client.predict(
30
  query=user_message,
31
+ api_key=SPACE_API_KEY,
32
+ api_name="/chat"
33
  )
34
 
35
  return {"response": result}
36
 
 
37
  if __name__ == "__main__":
38
  uvicorn.run(app, host="0.0.0.0", port=7860)