Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,38 +3,36 @@ from fastapi import FastAPI, Request, HTTPException
|
|
3 |
import uvicorn
|
4 |
from gradio_client import Client
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
client = Client("Futuresony/Mr.Events"
|
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 |
-
#
|
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 |
-
#
|
31 |
result = client.predict(
|
32 |
query=user_message,
|
33 |
-
|
|
|
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)
|