mishig HF staff commited on
Commit
9c66ceb
1 Parent(s): ca02766

Create cloud_function.py

Browse files
Files changed (1) hide show
  1. cloud_function.py +41 -0
cloud_function.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ def inference(request):
4
+ if request.method == 'OPTIONS':
5
+ # Allows GET requests from any origin with the Content-Type
6
+ # header and caches preflight response for an 3600s
7
+ headers = {
8
+ 'Access-Control-Allow-Origin': '*',
9
+ 'Access-Control-Allow-Methods': 'GET',
10
+ 'Access-Control-Allow-Headers': 'Content-Type',
11
+ 'Access-Control-Max-Age': '3600'
12
+ }
13
+
14
+ return ('', 204, headers)
15
+
16
+ # Set CORS headers for the main request
17
+ headers = {
18
+ 'Access-Control-Allow-Origin': '*'
19
+ }
20
+
21
+ request_json = request.get_json()
22
+ prompt = request_json['prompt']
23
+
24
+ # Helper function to call HF Inference API
25
+ def query(payload):
26
+ model_id = "typeform/distilbert-base-uncased-mnli"
27
+ API_URL = f'https://api-inference.huggingface.co/models/{model_id}'
28
+ headers_hf = {"Authorization": "Bearer <YOUR_API_KEY>"}
29
+ response = requests.post(API_URL, headers=headers_hf, json=payload)
30
+ return response.text
31
+
32
+ # Set of actions available for the NPC
33
+ defined_actions = ["dance", "fight", "run", "text"]
34
+
35
+ output = query({
36
+ "inputs": prompt,
37
+ "parameters": {"candidate_labels": defined_actions},
38
+ "options": {"wait_for_model": True}
39
+ })
40
+
41
+ return (output, 200, headers)