Skip to content

Commit 3ecc7c5

Browse files
committed
aún tratando de que funcione, añadido servicio
1 parent 498eef2 commit 3ecc7c5

File tree

6 files changed

+51
-38
lines changed

6 files changed

+51
-38
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"dependencies": {
1414
"@astrojs/svelte": "^7.0.4",
1515
"@astrojs/vercel": "^8.0.4",
16+
"@huggingface/inference": "^1.8.0",
1617
"astro": "^5.1.9",
17-
"dotenv": "^16.4.5",
18-
"@huggingface/inference": "^1.0.0"
18+
"dotenv": "^16.4.7"
1919
}
2020
}

src/components/Riddles.svelte

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
body: JSON.stringify({ word1: userInput, word2: riddle.solution }),
3737
});
3838
39+
if (!res.ok) {
40+
throw new Error(`HTTP error! status: ${res.status}`);
41+
}
42+
3943
const data = await res.json();
4044
4145
if (data.error) {

src/lib/embeddingService.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export async function getEmbeddings(text: string): Promise<number[]> {
2+
try {
3+
const response = await fetch('https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2', {
4+
method: 'POST',
5+
headers: {
6+
Authorization: `Bearer ${process.env.HF_ACCESS_TOKEN}`,
7+
'Content-Type': 'application/json',
8+
},
9+
body: JSON.stringify({ inputs: text }),
10+
});
11+
12+
if (!response.ok) {
13+
const errorText = await response.text();
14+
console.error(`Error from Hugging Face API: ${response.status} ${response.statusText} - ${errorText}`);
15+
throw new Error(`API Error: ${response.status} ${response.statusText} - ${errorText}`);
16+
}
17+
18+
const result = await response.json();
19+
20+
if (Array.isArray(result) && result.length > 0) {
21+
if (Array.isArray(result[0]) && result[0].every((item) => typeof item === 'number')) {
22+
return result[0];
23+
}
24+
if (result.every((item) => typeof item === 'number')) {
25+
return result;
26+
}
27+
}
28+
29+
throw new Error("Unexpected response format. Full response: " + JSON.stringify(result));
30+
} catch (error) {
31+
console.error('Error extracting features:', error.message);
32+
throw error;
33+
}
34+
}

src/pages/api/compare.ts

+4-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { APIRoute } from 'astro';
22
import dotenv from 'dotenv';
3-
import fetch from 'node-fetch';
3+
import { getEmbeddings } from '../../lib/embeddingService';
44

55
dotenv.config();
66

7-
const hfApiUrl = 'https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2';
8-
const hfAccessToken = process.env.HUGGING_FACE_API_KEY;
7+
const HF_ACCESS_TOKEN = process.env.HF_ACCESS_TOKEN;
98

109
function singleSoftmax(value: number): number {
1110
const scaledValue = (value - 0.5) * 10;
@@ -23,35 +22,6 @@ function cosineSimilarity(vectorA: number[], vectorB: number[]): number {
2322
return singleSoftmax(similarity) * 100;
2423
}
2524

26-
async function extractFeatures(text: string): Promise<number[]> {
27-
const response = await fetch(hfApiUrl, {
28-
method: 'POST',
29-
headers: {
30-
Authorization: `Bearer ${hfAccessToken}`,
31-
'Content-Type': 'application/json',
32-
},
33-
body: JSON.stringify({ inputs: text }),
34-
});
35-
36-
if (!response.ok) {
37-
const errorText = await response.text();
38-
throw new Error(`API Error: ${response.status} ${response.statusText} - ${errorText}`);
39-
}
40-
41-
const result = await response.json();
42-
43-
if (Array.isArray(result) && result.length > 0) {
44-
if (Array.isArray(result[0]) && result[0].every((item) => typeof item === 'number')) {
45-
return result[0];
46-
}
47-
if (result.every((item) => typeof item === 'number')) {
48-
return result;
49-
}
50-
}
51-
52-
throw new Error("Unexpected response format. Full response: " + JSON.stringify(result));
53-
}
54-
5525
export const post: APIRoute = async ({ request }) => {
5626
const { word1, word2 } = await request.json();
5727

@@ -60,8 +30,8 @@ export const post: APIRoute = async ({ request }) => {
6030
}
6131

6232
try {
63-
const vectorA = await extractFeatures(word1);
64-
const vectorB = await extractFeatures(word2);
33+
const vectorA = await getEmbeddings(word1);
34+
const vectorB = await getEmbeddings(word2);
6535

6636
const cosineSim = cosineSimilarity(vectorA, vectorB);
6737

svelte.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { vitePreprocess } from '@astrojs/svelte';
2+
3+
export default {
4+
preprocess: vitePreprocess(),
5+
}

0 commit comments

Comments
 (0)