Skip to content

Commit 0597b5d

Browse files
committed
modified: README.md
new file: example.js new file: hug-1.js new file: hug.js
1 parent 63cf5e3 commit 0597b5d

4 files changed

Lines changed: 186 additions & 1 deletion

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# Huggingface-API
2-
An unofficial reverse engineered NodeJS package of Huggingface AI.
2+
Extremely lightweight and flexible ;)
3+
4+
# 😊 How To Use?
5+
```javascript
6+
const hug = require("./hug")
7+
const { env } = require("process")
8+
9+
env.TOKEN = "MY_TOKEN_HERE" // From cookies
10+
env.HFCHAT = "MY_HFCHAT_HERE" // From cookies
11+
12+
hug("Hello there")
13+
.then(data => console.log(data))
14+
```
15+
16+
```md
17+
> Hello! How can I assist you today?
18+
```
19+
20+
Get your `hf-chat` and `token` value from cookies and your'e good to go.
21+
22+
Note: Unofficial and reverse engineered Huggingface API. This software cannot be used for any unlawful activities and commercial purpose.

example.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const hug = require("./hug")
2+
const { env } = require("process")
3+
4+
env.TOKEN = "MY_TOKEN_HERE" // From cookies
5+
env.HFCHAT = "MY_HFCHAT_HERE" // From cookies
6+
7+
hug("Hello there")
8+
.then(data => console.log(data))

hug-1.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const { randomUUID } = require("crypto")
2+
const process = require("process")
3+
const readLine = require("readline")
4+
5+
const rl = readLine.createInterface({
6+
input: process.stdin,
7+
output: process.stdout
8+
})
9+
10+
const token = process.env.TOKEN
11+
const hfchat = process.env.HFCHAT
12+
const cookie = `token=${token}; hf-chat=${hfchat}`
13+
14+
function hug(msg) {
15+
return new Promise((resolve) => {
16+
if (!token) return "Huggingface token not provided"
17+
if (!hfchat) return "hfchat not provided"
18+
fetch("https://huggingface.co/chat/conversation", {
19+
"headers": {
20+
"content-type": "application/json",
21+
"cookie": cookie
22+
},
23+
"body": "{\"model\":\"OpenAssistant/oasst-sft-6-llama-30b-xor\"}",
24+
"method": "POST"
25+
})
26+
.then(data => data.json())
27+
.then(data => {
28+
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}/__data.json?x-sveltekit-invalidated=1_1`, {
29+
"headers": {
30+
"cookie": cookie
31+
},
32+
"body": null,
33+
"method": "GET"
34+
})
35+
.then(() => {
36+
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}`, {
37+
"headers": {
38+
"content-type": "application/json",
39+
"cookie": cookie
40+
},
41+
"body": JSON.stringify({
42+
"inputs": msg,
43+
"parameters": {
44+
"temperature": 0.2,
45+
"truncate": 1000,
46+
"max_new_tokens": 1024,
47+
"stop": [
48+
"</s>"
49+
],
50+
"top_p": 0.95,
51+
"repetition_penalty": 1.2,
52+
"top_k": 50,
53+
"return_full_text": false
54+
},
55+
"stream": true,
56+
"options": {
57+
"id": randomUUID(),
58+
"response_id": randomUUID(),
59+
"is_retry": false,
60+
"use_cache": false,
61+
"web_search_id": ""
62+
}
63+
}),
64+
"method": "POST"
65+
}).then(dataa => dataa.text())
66+
.then(dataa => {
67+
dataa = JSON.parse(dataa.slice(dataa.lastIndexOf('data:{"token":{') + 'data:'.length)).generated_text
68+
resolve(dataa)
69+
})
70+
})
71+
72+
73+
})
74+
})
75+
}
76+
77+
function ask() {
78+
rl.question("\033[35mYou: \033[34m> \033[36m", (msg) => {
79+
hug(msg).then(data => {
80+
console.log("😊 : \033[34m" + data)
81+
ask()
82+
})
83+
})
84+
}
85+
86+
ask()

hug.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { randomUUID } = require("crypto")
2+
const { env } = require("process")
3+
4+
const token = env.TOKEN
5+
const hfchat = env.HFCHAT
6+
const cookie = `token=${token}; hf-chat=${hfchat}`
7+
8+
function hug(msg) {
9+
return new Promise((resolve) => {
10+
if (!token) return "Huggingface token not provided"
11+
if (!hfchat) return "hfchat not provided"
12+
fetch("https://huggingface.co/chat/conversation", {
13+
"headers": {
14+
"content-type": "application/json",
15+
"cookie": cookie
16+
},
17+
"body": "{\"model\":\"OpenAssistant/oasst-sft-6-llama-30b-xor\"}",
18+
"method": "POST"
19+
})
20+
.then(data => data.json())
21+
.then(data => {
22+
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}/__data.json?x-sveltekit-invalidated=1_1`, {
23+
"headers": {
24+
"cookie": cookie
25+
},
26+
"body": null,
27+
"method": "GET"
28+
})
29+
.then(() => {
30+
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}`, {
31+
"headers": {
32+
"content-type": "application/json",
33+
"cookie": cookie
34+
},
35+
"body": JSON.stringify({
36+
"inputs": msg,
37+
"parameters": {
38+
"temperature": 0.2,
39+
"truncate": 1000,
40+
"max_new_tokens": 1024,
41+
"stop": [
42+
"</s>"
43+
],
44+
"top_p": 0.95,
45+
"repetition_penalty": 1.2,
46+
"top_k": 50,
47+
"return_full_text": false
48+
},
49+
"stream": true,
50+
"options": {
51+
"id": randomUUID(),
52+
"response_id": randomUUID(),
53+
"is_retry": false,
54+
"use_cache": false,
55+
"web_search_id": ""
56+
}
57+
}),
58+
"method": "POST"
59+
}).then(dataa => dataa.text())
60+
.then(dataa => {
61+
dataa = JSON.parse(dataa.slice(dataa.lastIndexOf('data:{"token":{') + 'data:'.length)).generated_text
62+
resolve(dataa)
63+
})
64+
})
65+
66+
67+
})
68+
})
69+
}
70+
71+
module.exports = hug

0 commit comments

Comments
 (0)