rr19tech commited on
Commit
845cd8b
·
verified ·
1 Parent(s): d5b8c61

Created a basic ping api to invoke proxy api call

Browse files
Files changed (1) hide show
  1. pb_hooks/ping_api.js +44 -0
pb_hooks/ping_api.js ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // pb_hooks/ping_api.js
2
+ module.exports = {
3
+ invokeGradioSpace: function() {
4
+
5
+ const url = `https://rr19tech-my-proxy.hf.space/api/predict`;
6
+
7
+ // 2. Prepare your payload based on the Gradio "Use via API" schema
8
+ const payload = {
9
+ data: [
10
+ "What is the color of the sky" // Your input string or parameters go here
11
+ ]
12
+ };
13
+
14
+ try {
15
+ // 3. Make the synchronous HTTP request via PocketBase global $http
16
+ const res = $http.send({
17
+ url: url,
18
+ method: "POST",
19
+ body: JSON.stringify(payload),
20
+ headers: {
21
+ "content-type": "application/json",
22
+ // Keep this if your Space is private or requires authorization
23
+ // "Authorization": "Bearer hf_YOUR_HF_TOKEN"
24
+ },
25
+ timeout: 120 // Timeout in seconds (give it time if waking up)
26
+ });
27
+
28
+ if (res.statusCode !== 200) {
29
+ throw new Error(`Gradio responded with status: ${res.statusCode}`);
30
+ }
31
+
32
+ // 4. Process your response
33
+ const responseData = res.json;
34
+ console.log("Gradio Output Data:", JSON.stringify(responseData.data));
35
+
36
+ // Optional: Save the output back to a PocketBase collection
37
+ // $app.dao().saveRecord(record);
38
+
39
+ } catch (err) {
40
+ console.log("Error invoking Gradio Space:", err.message);
41
+ }
42
+ }
43
+ };
44
+