-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (38 loc) · 1.25 KB
/
app.js
File metadata and controls
52 lines (38 loc) · 1.25 KB
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
43
44
45
46
47
48
49
50
51
52
import express from 'express';
import { fileURLToPath } from 'url';
import path from 'path';
import fetch from 'node-fetch';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const apiUrl ="https://api-inference.huggingface.co/models/michellejieli/emotion_text_classifier";
const apiToken = "your_api";
async function query(data) {
const response = await fetch(apiUrl, {
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
});
const result = await response.json();
return result;
}
const app = express();
app.use(express.json());
app.use('/public', express.static('./public'));
app.post('/', async (req, res) => {
try {
const result = await query({ inputs: req.body.text });
res.json(result);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Error processing sentiment analysis');
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, () => {
console.log('Server listening on port: 3000');
});