Spaces:
Runtime error
Runtime error
Revert "Call HF directly"
Browse filesThis reverts commit 65fafc532ea43f954bd54f302bc19ba0d4c06ae3.
- auth-service.js +19 -6
- document-authoring.js +8 -20
- nginx.conf +0 -6
auth-service.js
CHANGED
|
@@ -1,14 +1,27 @@
|
|
| 1 |
const express = require('express');
|
|
|
|
|
|
|
| 2 |
const app = express();
|
| 3 |
const port = 4001;
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
-
res.json({ token });
|
| 12 |
});
|
| 13 |
|
| 14 |
app.listen(port, () => {
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const jwt = require('jsonwebtoken');
|
| 3 |
+
|
| 4 |
const app = express();
|
| 5 |
const port = 4001;
|
| 6 |
|
| 7 |
+
const privateKey = process.env.JWT_PRIVATE_KEY;
|
| 8 |
+
if (!privateKey) {
|
| 9 |
+
console.error('JWT_PRIVATE_KEY environment variable is required');
|
| 10 |
+
process.exit(1);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
app.get('/auth-token', (req, res) => {
|
| 14 |
+
try {
|
| 15 |
+
const token = jwt.sign({}, privateKey, {
|
| 16 |
+
algorithm: process.env.JWT_ALGORITHM || 'RS256',
|
| 17 |
+
expiresIn: '1h'
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
res.json({ token });
|
| 21 |
+
} catch (error) {
|
| 22 |
+
console.error('Error generating token:', error);
|
| 23 |
+
res.status(500).json({ error: 'Failed to generate token' });
|
| 24 |
}
|
|
|
|
| 25 |
});
|
| 26 |
|
| 27 |
app.listen(port, () => {
|
document-authoring.js
CHANGED
|
@@ -20,28 +20,14 @@ async function getAuthToken() {
|
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
-
async function getHFToken() {
|
| 24 |
-
try {
|
| 25 |
-
const response = await fetch('/api/hf-token');
|
| 26 |
-
if (!response.ok) {
|
| 27 |
-
throw new Error('Failed to fetch HF token');
|
| 28 |
-
}
|
| 29 |
-
const { token } = await response.json();
|
| 30 |
-
return token;
|
| 31 |
-
} catch (error) {
|
| 32 |
-
console.error('Error getting HF token:', error);
|
| 33 |
-
throw error;
|
| 34 |
-
}
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
async function checkServicesStatus() {
|
| 38 |
try {
|
| 39 |
-
const token = await
|
| 40 |
-
const response = await fetch('
|
| 41 |
method: 'POST',
|
| 42 |
headers: {
|
| 43 |
'Content-Type': 'application/json',
|
| 44 |
-
'Authorization':
|
| 45 |
},
|
| 46 |
body: JSON.stringify({
|
| 47 |
model: 'gemma-2b',
|
|
@@ -75,6 +61,7 @@ async function checkServicesStatus() {
|
|
| 75 |
statusIndicator.style.color = '#dc3545';
|
| 76 |
} else {
|
| 77 |
statusIndicator.innerHTML = `<span class="spinner"></span> Initializing AI services...`;
|
|
|
|
| 78 |
setTimeout(checkServicesStatus, 2000);
|
| 79 |
}
|
| 80 |
}
|
|
@@ -147,12 +134,12 @@ script.onload = async () => {
|
|
| 147 |
|
| 148 |
async function translate(content, targetLang, sourceLang = 'English') {
|
| 149 |
try {
|
| 150 |
-
const token = await
|
| 151 |
-
const response = await fetch('
|
| 152 |
method: 'POST',
|
| 153 |
headers: {
|
| 154 |
'Content-Type': 'application/json',
|
| 155 |
-
'Authorization':
|
| 156 |
},
|
| 157 |
body: JSON.stringify({
|
| 158 |
messages: [
|
|
@@ -175,6 +162,7 @@ ${targetLang}: "Las empresas usan Nutrient para..."
|
|
| 175 |
${content}`
|
| 176 |
}
|
| 177 |
],
|
|
|
|
| 178 |
stream: false
|
| 179 |
})
|
| 180 |
});
|
|
|
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
async function checkServicesStatus() {
|
| 24 |
try {
|
| 25 |
+
const token = await getAuthToken();
|
| 26 |
+
const response = await fetch('/inference/api/v1/chat/completions', {
|
| 27 |
method: 'POST',
|
| 28 |
headers: {
|
| 29 |
'Content-Type': 'application/json',
|
| 30 |
+
'Authorization': `Token token=${token}`
|
| 31 |
},
|
| 32 |
body: JSON.stringify({
|
| 33 |
model: 'gemma-2b',
|
|
|
|
| 61 |
statusIndicator.style.color = '#dc3545';
|
| 62 |
} else {
|
| 63 |
statusIndicator.innerHTML = `<span class="spinner"></span> Initializing AI services...`;
|
| 64 |
+
// Schedule next check only if we haven't exceeded retries
|
| 65 |
setTimeout(checkServicesStatus, 2000);
|
| 66 |
}
|
| 67 |
}
|
|
|
|
| 134 |
|
| 135 |
async function translate(content, targetLang, sourceLang = 'English') {
|
| 136 |
try {
|
| 137 |
+
const token = await getAuthToken();
|
| 138 |
+
const response = await fetch('/inference/api/v1/chat/completions', {
|
| 139 |
method: 'POST',
|
| 140 |
headers: {
|
| 141 |
'Content-Type': 'application/json',
|
| 142 |
+
'Authorization': `Token token=${token}`
|
| 143 |
},
|
| 144 |
body: JSON.stringify({
|
| 145 |
messages: [
|
|
|
|
| 162 |
${content}`
|
| 163 |
}
|
| 164 |
],
|
| 165 |
+
model: "gemma-2b",
|
| 166 |
stream: false
|
| 167 |
})
|
| 168 |
});
|
nginx.conf
CHANGED
|
@@ -29,11 +29,5 @@ http {
|
|
| 29 |
proxy_set_header Host $host;
|
| 30 |
proxy_set_header X-Real-IP $remote_addr;
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
location /api/hf-token {
|
| 34 |
-
proxy_pass http://localhost:4001/hf-token;
|
| 35 |
-
proxy_set_header Host $host;
|
| 36 |
-
proxy_set_header X-Real-IP $remote_addr;
|
| 37 |
-
}
|
| 38 |
}
|
| 39 |
}
|
|
|
|
| 29 |
proxy_set_header Host $host;
|
| 30 |
proxy_set_header X-Real-IP $remote_addr;
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
}
|