File size: 1,342 Bytes
9c66ceb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
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)