Spaces:
Sleeping
Sleeping
ausername-12345 commited on
Commit Β·
1839952
1
Parent(s): 824f0a6
Use Wikipedia API for images, remove image URLs from prompt
Browse files- server.js +16 -26
- src/rememberit.jsx +1 -1
server.js
CHANGED
|
@@ -48,33 +48,23 @@ app.post("/api/images", async (req, res) => {
|
|
| 48 |
const { itemName } = req.body;
|
| 49 |
if (!itemName) return res.status(400).json({ error: "Missing itemName" });
|
| 50 |
|
| 51 |
-
const
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
const
|
| 56 |
-
const
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
const
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if (!vqd) {
|
| 65 |
-
const scriptTag = html.match(/<script[^>]*>([^<]*)<\/script>/gi);
|
| 66 |
-
if (scriptTag) {
|
| 67 |
-
for (const s of scriptTag) {
|
| 68 |
-
const m = s.match(/["'](\d+(?:-\d+)*)["']/);
|
| 69 |
-
if (m) { vqd = m[1]; break; }
|
| 70 |
-
}
|
| 71 |
-
}
|
| 72 |
-
}
|
| 73 |
-
if (!vqd) return res.json({ images: [] });
|
| 74 |
-
|
| 75 |
-
const imgRes = await fetch(`https://duckduckgo.com/i.js?q=${encodeURIComponent(itemName)}&o=json&vqd=${vqd}`, { headers });
|
| 76 |
const imgData = await imgRes.json();
|
| 77 |
-
const
|
|
|
|
|
|
|
| 78 |
res.json({ images });
|
| 79 |
} catch (err) {
|
| 80 |
res.json({ images: [], error: err.message });
|
|
|
|
| 48 |
const { itemName } = req.body;
|
| 49 |
if (!itemName) return res.status(400).json({ error: "Missing itemName" });
|
| 50 |
|
| 51 |
+
const searchRes = await fetch(
|
| 52 |
+
`https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(itemName)}&format=json&srlimit=3`,
|
| 53 |
+
{ headers: { "User-Agent": "RememberIt/1.0" } }
|
| 54 |
+
);
|
| 55 |
+
const searchData = await searchRes.json();
|
| 56 |
+
const pages = searchData?.query?.search || [];
|
| 57 |
+
if (!pages.length) return res.json({ images: [] });
|
| 58 |
+
|
| 59 |
+
const titles = pages.slice(0, 2).map(p => p.title);
|
| 60 |
+
const imgRes = await fetch(
|
| 61 |
+
`https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(titles.join("|"))}&prop=pageimages&format=json&pithumbsize=300`,
|
| 62 |
+
{ headers: { "User-Agent": "RememberIt/1.0" } }
|
| 63 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
const imgData = await imgRes.json();
|
| 65 |
+
const imgPages = imgData?.query?.pages || {};
|
| 66 |
+
const images = Object.values(imgPages).map(p => p.thumbnail?.source).filter(Boolean).slice(0, 3);
|
| 67 |
+
|
| 68 |
res.json({ images });
|
| 69 |
} catch (err) {
|
| 70 |
res.json({ images: [], error: err.message });
|
src/rememberit.jsx
CHANGED
|
@@ -71,7 +71,7 @@ Rules:
|
|
| 71 |
- confidence: a short honest phrase, e.g. "High confidence", "Fairly confident", "Moderate β a few possibilities", "Low confidence β hard to say without more context"
|
| 72 |
- description: 2β3 sentences covering what it is, what it looks like, and what it is used for
|
| 73 |
- alternatives: 0β1 entries if highly confident; 2β3 entries if uncertain; must be genuinely distinct items not synonyms
|
| 74 |
-
- images:
|
| 75 |
- searchLinks: always exactly these three, with the itemName URL-encoded:
|
| 76 |
{"label":"Amazon","url":"https://www.amazon.com/s?k=ENCODED"},
|
| 77 |
{"label":"Google Shopping","url":"https://www.google.com/search?q=ENCODED&tbm=shop"},
|
|
|
|
| 71 |
- confidence: a short honest phrase, e.g. "High confidence", "Fairly confident", "Moderate β a few possibilities", "Low confidence β hard to say without more context"
|
| 72 |
- description: 2β3 sentences covering what it is, what it looks like, and what it is used for
|
| 73 |
- alternatives: 0β1 entries if highly confident; 2β3 entries if uncertain; must be genuinely distinct items not synonyms
|
| 74 |
+
- images: always an empty array
|
| 75 |
- searchLinks: always exactly these three, with the itemName URL-encoded:
|
| 76 |
{"label":"Amazon","url":"https://www.amazon.com/s?k=ENCODED"},
|
| 77 |
{"label":"Google Shopping","url":"https://www.google.com/search?q=ENCODED&tbm=shop"},
|