Spaces:
Running
Running
| import requests | |
| def inference(request): | |
| if request.method == 'OPTIONS': | |
| # Allows GET requests from any origin with the Content-Type | |
| # header and caches preflight response for an 3600s | |
| headers = { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Access-Control-Allow-Methods': 'GET', | |
| 'Access-Control-Allow-Headers': 'Content-Type', | |
| 'Access-Control-Max-Age': '3600' | |
| } | |
| return ('', 204, headers) | |
| # Set CORS headers for the main request | |
| headers = { | |
| 'Access-Control-Allow-Origin': '*' | |
| } | |
| request_json = request.get_json() | |
| prompt = request_json['prompt'] | |
| # Helper function to call HF Inference API | |
| def query(payload): | |
| model_id = "typeform/distilbert-base-uncased-mnli" | |
| API_URL = f'https://api-inference.huggingface.co/models/{model_id}' | |
| headers_hf = {"Authorization": "Bearer <YOUR_API_KEY>"} | |
| response = requests.post(API_URL, headers=headers_hf, json=payload) | |
| return response.text | |
| # Set of actions available for the NPC | |
| defined_actions = ["dance", "fight", "run", "text"] | |
| output = query({ | |
| "inputs": prompt, | |
| "parameters": {"candidate_labels": defined_actions}, | |
| "options": {"wait_for_model": True} | |
| }) | |
| return (output, 200, headers) | |