Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # β Secure HF API Token (Stored in Hugging Face Space Secrets) | |
| HF_API_TOKEN = os.getenv("HF_API_TOKEN") | |
| # β Private Inference Endpoint URL | |
| INFERENCE_API_URL = "https://z1lobj44dbdikcgy.eu-west-1.aws.endpoints.huggingface.cloud" | |
| # β Define a non-secret API key expected from the model | |
| VALID_MODEL_API_KEY = os.getenv("VALID_MODEL_API_KEY") | |
| def detect_objects(image, api_key): | |
| """Validates API key before processing request.""" | |
| # β Reject unauthorized requests | |
| if api_key != VALID_MODEL_API_KEY: | |
| return {"error": "Unauthorized request"} | |
| # β Forward request to the private inference endpoint with secret HF token | |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} | |
| files = {"image": image} | |
| response = requests.post(INFERENCE_API_URL, headers=headers, files=files) | |
| return response.json() | |
| # β Public Space, but API key is required for access | |
| iface = gr.Interface(fn=detect_objects, inputs=["image", "text"], outputs="json") | |
| iface.launch() | |