Spaces:
Build error
Build error
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() | |
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) | |