Spaces:
Sleeping
Sleeping
File size: 2,918 Bytes
da93817 908eb88 da93817 cad7237 908eb88 771ba78 683ebd7 f74f567 908eb88 f74f567 683ebd7 f74f567 e0d3782 da93817 4c2427e da93817 908eb88 5b8053d 908eb88 0c832a6 771ba78 e0d3782 771ba78 908eb88 e0d3782 da93817 908eb88 7510230 908eb88 7510230 5017276 658038b 1839952 824f0a6 5017276 7510230 e22b79f 908eb88 66a8b59 908eb88 66a8b59 908eb88 244f310 908eb88 da93817 e22b79f da93817 67dcad9 da93817 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import express from "express";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import OpenAI from "openai";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 7860;
const BASE = "https://sharktide-lightning.hf.space/v1";
app.use(express.json({ limit: "1mb" }));
app.get("/api/check", async (_, res) => {
const env = {};
const interesting = ["IPAI_SK", "SPACE_ID", "SPACE_TITLE"];
for (const key of interesting) {
if (process.env[key]) env[key] = process.env[key].length > 10 ? process.env[key].slice(0, 8) + "..." : "(set)";
else env[key] = "(not set)";
}
res.json({ ok: true, env });
});
app.post("/api/hf", async (req, res) => {
try {
const key = process.env.IPAI_SK;
if (!key) {
return res.status(500).json({ error: "IPAI_SK not configured" });
}
const openai = new OpenAI({ apiKey: key, baseURL: BASE });
const model = req.body.model || "llama-3.3-70b-versatile";
const messages = req.body.messages || [];
const maxTokens = req.body.max_tokens || 1000;
const out = await openai.chat.completions.create({ model, messages, max_tokens: maxTokens });
res.json(out);
} catch (err) {
console.error("API error:", err);
const msg = err.message || String(err);
const status = err.status || 500;
res.status(status).json({ error: msg });
}
});
app.post("/api/images", async (req, res) => {
try {
const { itemName } = req.body;
if (!itemName) return res.status(400).json({ error: "Missing itemName" });
const apiRes = await fetch(
`https://api.openverse.org/v1/images/?q=${encodeURIComponent(itemName)}&page_size=4&page=1`,
{ headers: { "User-Agent": "RememberIt/1.0" } }
);
const data = await apiRes.json();
const images = (data.results || []).map(r => r.url).filter(Boolean).slice(0, 4);
res.json({ images });
} catch (err) {
res.json({ images: [], error: err.message });
}
});
app.get("/api/test-lib", async (_, res) => {
const results = {};
const key = process.env.IPAI_SK;
try {
const openai = new OpenAI({ apiKey: key, baseURL: BASE });
const out = await openai.chat.completions.create({
model: "llama3-8b-8192",
messages: [{ role: "user", content: "Say hello in one word." }],
max_tokens: 10,
});
results["chat"] = { status: 200, content: out.choices?.[0]?.message?.content };
} catch (e) {
results["chat"] = { error: e.message };
}
res.json(results);
});
app.use((err, _req, res, _next) => {
console.error("Unhandled:", err);
res.status(500).json({ error: err.message || "Internal error" });
});
const distPath = join(__dirname, "dist");
app.use(express.static(distPath));
app.get("*", (_, res) => res.sendFile(join(distPath, "index.html")));
app.listen(PORT, "0.0.0.0", () => console.log(`Server running on port ${PORT}`));
|