Spaces:
Running
Running
Setup Hugging Face InferenceClient using REACT_APP_HF_TOKEN
Browse filesmodified: README.md
modified: package-lock.json
modified: package.json
modified: src/components/ChatInterface.jsx
new file: src/utils/hfClient.js
- README.md +14 -0
- package-lock.json +29 -0
- package.json +1 -0
- src/components/ChatInterface.jsx +14 -1
- src/utils/hfClient.js +5 -0
README.md
CHANGED
|
@@ -17,11 +17,25 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
|
|
| 17 |
|
| 18 |
## Features
|
| 19 |
|
|
|
|
|
|
|
| 20 |
- Support UI to send user input and get dummy response.
|
| 21 |
- Keep user input and response in chat history.
|
| 22 |
|
| 23 |
## Usage
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
### Development mode
|
| 26 |
|
| 27 |
- `npm start`
|
|
|
|
| 17 |
|
| 18 |
## Features
|
| 19 |
|
| 20 |
+
- Get Hugging Face Access Token from environment variable.
|
| 21 |
+
- Test dummy user input and show response in console log.
|
| 22 |
- Support UI to send user input and get dummy response.
|
| 23 |
- Keep user input and response in chat history.
|
| 24 |
|
| 25 |
## Usage
|
| 26 |
|
| 27 |
+
### Prerequisite
|
| 28 |
+
|
| 29 |
+
- Create a test token on Hugging Face with read access only.
|
| 30 |
+
- Export it in your shell:\
|
| 31 |
+
`export REACT_APP_HF_TOKEN=your_test_token`
|
| 32 |
+
|
| 33 |
+
### Security note
|
| 34 |
+
|
| 35 |
+
- The token is bundled into client-side JS and visible in browser DevTools.
|
| 36 |
+
- For testing, create a temporary token with read-only permission.
|
| 37 |
+
- After testing, revoke or delete the token to prevent exposure.
|
| 38 |
+
|
| 39 |
### Development mode
|
| 40 |
|
| 41 |
- `npm start`
|
package-lock.json
CHANGED
|
@@ -8,6 +8,7 @@
|
|
| 8 |
"name": "react-template",
|
| 9 |
"version": "0.1.0",
|
| 10 |
"dependencies": {
|
|
|
|
| 11 |
"@testing-library/dom": "^10.4.0",
|
| 12 |
"@testing-library/jest-dom": "^6.6.3",
|
| 13 |
"@testing-library/react": "^16.3.0",
|
|
@@ -2443,6 +2444,34 @@
|
|
| 2443 |
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
| 2444 |
}
|
| 2445 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2446 |
"node_modules/@humanwhocodes/config-array": {
|
| 2447 |
"version": "0.13.0",
|
| 2448 |
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.13.0.tgz",
|
|
|
|
| 8 |
"name": "react-template",
|
| 9 |
"version": "0.1.0",
|
| 10 |
"dependencies": {
|
| 11 |
+
"@huggingface/inference": "^4.8.0",
|
| 12 |
"@testing-library/dom": "^10.4.0",
|
| 13 |
"@testing-library/jest-dom": "^6.6.3",
|
| 14 |
"@testing-library/react": "^16.3.0",
|
|
|
|
| 2444 |
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
| 2445 |
}
|
| 2446 |
},
|
| 2447 |
+
"node_modules/@huggingface/inference": {
|
| 2448 |
+
"version": "4.8.0",
|
| 2449 |
+
"resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-4.8.0.tgz",
|
| 2450 |
+
"integrity": "sha512-Eq98EAXqYn4rKMfrbEXuhc3IjKfaeIO6eXNOZk9xk6v5akrIWRtd6d1h0fjAWyX4zRbdUpXRh6MvsqXnzGvXCA==",
|
| 2451 |
+
"license": "MIT",
|
| 2452 |
+
"dependencies": {
|
| 2453 |
+
"@huggingface/jinja": "^0.5.1",
|
| 2454 |
+
"@huggingface/tasks": "^0.19.45"
|
| 2455 |
+
},
|
| 2456 |
+
"engines": {
|
| 2457 |
+
"node": ">=18"
|
| 2458 |
+
}
|
| 2459 |
+
},
|
| 2460 |
+
"node_modules/@huggingface/jinja": {
|
| 2461 |
+
"version": "0.5.1",
|
| 2462 |
+
"resolved": "https://registry.npmjs.org/@huggingface/jinja/-/jinja-0.5.1.tgz",
|
| 2463 |
+
"integrity": "sha512-yUZLld4lrM9iFxHCwFQ7D1HW2MWMwSbeB7WzWqFYDWK+rEb+WldkLdAJxUPOmgICMHZLzZGVcVjFh3w/YGubng==",
|
| 2464 |
+
"license": "MIT",
|
| 2465 |
+
"engines": {
|
| 2466 |
+
"node": ">=18"
|
| 2467 |
+
}
|
| 2468 |
+
},
|
| 2469 |
+
"node_modules/@huggingface/tasks": {
|
| 2470 |
+
"version": "0.19.46",
|
| 2471 |
+
"resolved": "https://registry.npmjs.org/@huggingface/tasks/-/tasks-0.19.46.tgz",
|
| 2472 |
+
"integrity": "sha512-c6F/r7zRQjmyo6Ji8c2TUbdeeu6WAdZxYLRd+G7Xxvfbadi6iDwk2szt/oinC5v5Ljyc2sjzesaqGB6hLWy/DA==",
|
| 2473 |
+
"license": "MIT"
|
| 2474 |
+
},
|
| 2475 |
"node_modules/@humanwhocodes/config-array": {
|
| 2476 |
"version": "0.13.0",
|
| 2477 |
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.13.0.tgz",
|
package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
"version": "0.1.0",
|
| 4 |
"private": true,
|
| 5 |
"dependencies": {
|
|
|
|
| 6 |
"@testing-library/dom": "^10.4.0",
|
| 7 |
"@testing-library/jest-dom": "^6.6.3",
|
| 8 |
"@testing-library/react": "^16.3.0",
|
|
|
|
| 3 |
"version": "0.1.0",
|
| 4 |
"private": true,
|
| 5 |
"dependencies": {
|
| 6 |
+
"@huggingface/inference": "^4.8.0",
|
| 7 |
"@testing-library/dom": "^10.4.0",
|
| 8 |
"@testing-library/jest-dom": "^6.6.3",
|
| 9 |
"@testing-library/react": "^16.3.0",
|
src/components/ChatInterface.jsx
CHANGED
|
@@ -1,10 +1,23 @@
|
|
| 1 |
-
import { useState } from "react";
|
|
|
|
| 2 |
import MessageInput from "./MessageInput";
|
| 3 |
import MessagePair from "./MessagePair";
|
| 4 |
|
| 5 |
export default function ChatInterface() {
|
| 6 |
const [chatHistory, setChatHistory] = useState([]);
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
const handleMessageSend = (message) => {
|
| 9 |
const fakeResponse =
|
| 10 |
"This is a simulated response.\nIt can span multiple lines.";
|
|
|
|
| 1 |
+
import { useEffect, useState } from "react";
|
| 2 |
+
import { hf } from "../utils/hfClient";
|
| 3 |
import MessageInput from "./MessageInput";
|
| 4 |
import MessagePair from "./MessagePair";
|
| 5 |
|
| 6 |
export default function ChatInterface() {
|
| 7 |
const [chatHistory, setChatHistory] = useState([]);
|
| 8 |
|
| 9 |
+
useEffect(() => {
|
| 10 |
+
const testHF = async () => {
|
| 11 |
+
const result = await hf.chatCompletion({
|
| 12 |
+
model: "openai/gpt-oss-20b",
|
| 13 |
+
messages: [{ role: "user", content: "Hello!" }],
|
| 14 |
+
});
|
| 15 |
+
console.log("HF:", result.choices[0].message.content);
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
testHF();
|
| 19 |
+
}, []);
|
| 20 |
+
|
| 21 |
const handleMessageSend = (message) => {
|
| 22 |
const fakeResponse =
|
| 23 |
"This is a simulated response.\nIt can span multiple lines.";
|
src/utils/hfClient.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { InferenceClient } from "@huggingface/inference";
|
| 2 |
+
|
| 3 |
+
const HF_TOKEN = process.env.REACT_APP_HF_TOKEN;
|
| 4 |
+
|
| 5 |
+
export const hf = new InferenceClient(HF_TOKEN);
|