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