|
1 | | -import { Ollama } from 'ollama' |
2 | | - |
3 | | -let content = "BART model pre-trained on English language, and fine-tuned on CNN Daily Mail. It was introduced in the paper BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension by Lewis et al. and first released in [this repository (https://github.com/pytorch/fairseq/tree/master/examples/bart).Disclaimer: The team releasing BART did not write a model card for this model so this model card has been written by the Hugging Face team.Model description BART is a transformer encoder-encoder (seq2seq) model with a bidirectional (BERT-like) encoder and an autoregressive (GPT-like) decoder. BART is pre-trained by (1) corrupting text with an arbitrary noising function, and (2) learning a model to reconstruct the original text.BART is particularly effective when fine-tuned for text generation (e.g. summarization, translation) but also works well for comprehension tasks (e.g. text classification, question answering). This particular checkpoint has been fine-tuned on CNN Daily Mail, a large collection of text-summary pairs." |
4 | | - |
5 | | -const summarize = async (content, showThinking = false) => { |
6 | | - const ollama = new Ollama({ host: 'http://127.0.0.1:11434' }) |
7 | | - const prompt = "Please provide a concise summary of the following content:" |
8 | | - const response = await ollama.chat({ |
9 | | - model: 'deepseek-r1:1.5b', |
10 | | - messages: [{ role: 'user', content: `${prompt} ${content}` }], |
11 | | - }) |
12 | | - const messageContent = response.message.content |
13 | | - if (showThinking == true){ |
14 | | - return messageContent |
15 | | - } else { |
16 | | - // return messageContent.replace(/<\s*think\s*>[\s\S]*<\s*\/\s*think\s*>/gi, '') |
17 | | - // return messageContent.split("</think>", 1)[1]; |
18 | | - return messageContent.split("</think>", 2)[1]; |
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import { Ollama } from "ollama"; |
| 4 | + |
| 5 | +const app = express(); |
| 6 | +app.use(express.json()); |
| 7 | +app.use(cors()); |
| 8 | + |
| 9 | +const ollama = new Ollama(); |
| 10 | + |
| 11 | +app.post("/summarize", async (req, res) => { |
| 12 | + try { |
| 13 | + const { text } = req.body; |
| 14 | + if (!text) { |
| 15 | + return res.status(400).json({ error: "Text is required - try selecting the text you want to summarize." }); |
| 16 | + } |
| 17 | + |
| 18 | + console.log("Text being sent to BART:", text); |
| 19 | + |
| 20 | + const response = await ollama.chat({ |
| 21 | + model: "deepseek-r1:1.5b", |
| 22 | + messages: [{ role: "user", content: `Summarize this text in 2-3 sentences: ${text}` }], |
| 23 | + }); |
| 24 | + |
| 25 | + console.log("Server esponse from BART:", response); |
| 26 | + |
| 27 | + if (!response || !response.message || !response.message.content || response.message.content.trim() === "{}") { |
| 28 | + return res.status(500).json({ error: "Unexpected response from BART. Try again with a shorter text." }); |
| 29 | + } |
| 30 | + |
| 31 | + let summary = response.message.content.trim(); |
| 32 | + |
| 33 | + if(summary.includes("</think")){ |
| 34 | + summary = summary.split("</think>", 2)[1]?.trim() || summary; |
| 35 | + } |
| 36 | + |
| 37 | + res.json({ summary }); |
| 38 | + |
| 39 | + } catch (error) { |
| 40 | + console.error("Threw an error:", error); |
| 41 | + res.status(500).json({ error: "BART cannot summarize", details: error.message }); |
19 | 42 | } |
20 | | -} |
21 | | -const response = await summarize(content) |
22 | | -console.log(response) |
| 43 | +}); |
| 44 | + |
| 45 | +app.listen(5000, () => console.log("Server running on port 5000")); |
0 commit comments