Spaces:
Running
Running
File size: 1,057 Bytes
150833d b8ad301 150833d b8ad301 a3ea6c9 150833d b8ad301 150833d b8ad301 150833d b8ad301 150833d 38f796a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import os
from fastapi import FastAPI, Request, HTTPException
import uvicorn
from gradio_client import Client
# Keys
VALID_API_KEY = os.getenv("APP_API_KEY") # for your FastAPI security
SPACE_API_KEY = os.getenv("SPACE_API_KEY") # for your hosted HF Space
# Connect to hosted space
client = Client("Futuresony/ABSA_Test_Space")
app = FastAPI()
@app.post("/chat")
async def chat(request: Request):
data = await request.json()
# API key check from headers or JSON
api_key = request.headers.get("X-API-Key") or data.get("api_key")
if api_key != VALID_API_KEY:
raise HTTPException(status_code=403, detail="Invalid API Key")
user_message = data.get("message")
if not user_message:
raise HTTPException(status_code=400, detail="Message is required")
# Call your hosted Space
result = client.predict(
query=user_message,
api_key=SPACE_API_KEY,
api_name="/chat"
)
return {"response": result}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|