Spaces:
Sleeping
Sleeping
File size: 1,083 Bytes
150833d |
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 40 |
import os
from fastapi import FastAPI, Request, HTTPException
import uvicorn
from gradio_client import Client
# Connect to your first Hugging Face Space
client = Client("Futuresony/Mr.Events")
# Get your secure API key from environment
VALID_API_KEY = os.getenv("API_KEY", "fs_ABnWISBWhOlqLIn6FuFqPWo8dFgy1wrk0r4EA")
app = FastAPI()
@app.post("/chat")
async def chat(request: Request):
data = await request.json()
# API Key Check
api_key = data.get("api_key")
if api_key != VALID_API_KEY:
raise HTTPException(status_code=403, detail="Invalid API Key")
# Get user message
user_message = data.get("message")
if not user_message:
raise HTTPException(status_code=400, detail="Message is required")
# Call the model hosted on your first space
result = client.predict(
query=user_message,
api_key="****", # your hosted model's internal key if needed
api_name="/chat"
)
return {"response": result}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) # HF default port
|