| import express from "express"; |
| import fetch from "node-fetch"; |
| import multer from "multer"; |
| import { Client } from "@gradio/client"; |
|
|
| const CREATOR = "LoRDx"; |
| const PORT = 7860; |
|
|
| const app = express(); |
| const upload = multer(); |
|
|
|
|
| const pinn = async (query) => { |
| const url = "https://www.pinterest.com/resource/BaseSearchResource/get/?data=" + |
| encodeURIComponent(JSON.stringify({ options: { query } })); |
| const res = await fetch(url, { |
| method: "HEAD", |
| headers: { |
| "screen-dpr": "4", |
| "x-pinterest-pws-handler": "www/search/[scope].js", |
| } |
| }); |
| if (!res.ok) throw new Error("Request failed"); |
| const lh = res.headers.get("Link"); |
| if (!lh) throw new Error("No results found"); |
| return [...lh.matchAll(/<([^>]+)>/g)].map(m => m[1]); |
| }; |
|
|
| app.get('/', (req, res) => { |
| res.send("API running"); |
| }); |
|
|
| app.get('/search/pin', async (req, res) => { |
| const { query } = req.query; |
| if (!query) return res.status(400).json({ error: "Missing query" }); |
| try { |
| const result = await pinn(query); |
| res.json({ status: true, creator: CREATOR, result }); |
| } catch (err) { |
| res.status(500).json({ error: err.message }); |
| } |
| }); |
|
|
| app.post("/tools/ocr", upload.single("image"), async (req, res) => { |
| try { |
| if (!req.file) return res.status(400).send("No image uploaded."); |
| const imgBuff = req.file.buffer; |
| const imgBlob = new Blob([imgBuff], { type: req.file.mimetype }); |
| const client = await Client.connect("prithivMLmods/Multimodal-OCR2"); |
| const result = await client.predict("/generate_image", { |
| model_name: "Nanonets-OCR-s", |
| text: "OCR this image", |
| image: imgBlob, |
| max_new_tokens: 1024, |
| temperature: 0.1, |
| top_p: 0.05, |
| top_k: 1, |
| repetition_penalty: 1, |
| }); |
| res.json({ status: true, creator: CREATOR, result: result.data }); |
| } catch (err) { |
| res.status(500).send(err.message); |
| } |
| }); |
|
|
| app.listen(PORT, () => console.log(`Server running on ${PORT}`)); |
|
|
| process.on('SIGINT', () => process.exit(0)); |