Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
+
import uvicorn
|
| 4 |
+
from gradio_client import Client
|
| 5 |
+
|
| 6 |
+
# Connect to your first Hugging Face Space
|
| 7 |
+
client = Client("Futuresony/Mr.Events")
|
| 8 |
+
|
| 9 |
+
# Get your secure API key from environment
|
| 10 |
+
VALID_API_KEY = os.getenv("API_KEY", "fs_ABnWISBWhOlqLIn6FuFqPWo8dFgy1wrk0r4EA")
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
@app.post("/chat")
|
| 15 |
+
async def chat(request: Request):
|
| 16 |
+
data = await request.json()
|
| 17 |
+
|
| 18 |
+
# API Key Check
|
| 19 |
+
api_key = data.get("api_key")
|
| 20 |
+
if api_key != VALID_API_KEY:
|
| 21 |
+
raise HTTPException(status_code=403, detail="Invalid API Key")
|
| 22 |
+
|
| 23 |
+
# Get user message
|
| 24 |
+
user_message = data.get("message")
|
| 25 |
+
if not user_message:
|
| 26 |
+
raise HTTPException(status_code=400, detail="Message is required")
|
| 27 |
+
|
| 28 |
+
# Call the model hosted on your first space
|
| 29 |
+
result = client.predict(
|
| 30 |
+
query=user_message,
|
| 31 |
+
api_key="****", # your hosted model's internal key if needed
|
| 32 |
+
api_name="/chat"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return {"response": result}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
uvicorn.run(app, host="0.0.0.0", port=7860) # HF default port
|