-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
124 lines (101 loc) · 3.66 KB
/
server.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import express from "express"
import {Connection} from 'postgresql-client';
import 'dotenv/config'
const app = express()
const port = 3000
const connection = new Connection(process.env.DATABASE_URL)
await connection.connect()
app.set('view engine', 'ejs');
app.use(
express.static('assets'),
express.json(),
express.urlencoded({ extended: true })
);
app.get("/", async (req, res) => {
// Get all the notes to show
let db_notes_req = `SELECT * FROM notes WHERE DATE(created_at) = DATE(NOW());`
const db_note_resp = await connection.execute(db_notes_req)
const notes = db_note_resp.results[0].rows
// Get the summary and score of the day
let db_day_req = `SELECT * FROM days WHERE DATE(created_at) = DATE(NOW());`
const db_day_resp = await connection.execute(db_day_req)
let description = db_day_resp.results[0].rows[0]?.[1]
let score = db_day_resp.results[0].rows[0]?.[2]
res.render('index', { data: notes.reverse(), score: score, description: description})
})
app.post("/add", async (req, res) => {
/// escape single quotes
let text = req.body.note.replaceAll("'", "''")
/// Construct a SQL request that that performs sentiment analysis when entered into database:
///
/// 1. Analyzes the sentiment of the note
/// 2. Converts sentiment score to +/1 or 0 dependingn on pos, neg, or neutral
/// 3. Inserts the note and the sentiment score into the notes table
let db_request = `
WITH note AS (
SELECT pgml.transform(
inputs => ARRAY['${text}'],
task => '{"task": "text-classification", "model": "finiteautomata/bertweet-base-sentiment-analysis"}'::JSONB
) AS market_sentiment
),
score AS (
SELECT
CASE
WHEN (SELECT market_sentiment FROM note)[0]::JSONB ->> 'label' = 'POS' THEN 1
WHEN (SELECT market_sentiment FROM note)[0]::JSONB ->> 'label' = 'NEG' THEN -1
ELSE 0
END AS score
)
INSERT INTO notes (note, score) VALUES ('${text}', (SELECT score FROM score));
`
await connection.execute(db_request);
res.redirect('/')
})
app.get("/analyze", async (req, res) => {
/// Construct a SQL request to compute daily analysis and summarization of notes:
/// 1. Get all notes from today
/// 2. Summarize the notes
/// 3. Insert the summary and the sentiment score into the days table
let db_request = `
WITH day AS (
SELECT
note,
score
FROM notes
WHERE DATE(created_at) = DATE(NOW())),
sum AS (
SELECT pgml.transform(
task => '{"task": "summarization", "model": "sshleifer/distilbart-cnn-12-6"}'::JSONB,
inputs => array[(SELECT STRING_AGG(note, '\n') FROM day)],
args => '{"min_length" : 20, "max_length" : 70}'::JSONB
) AS summary
)
INSERT INTO days (summary, score)
VALUES ((SELECT summary FROM sum)[0]::JSONB ->> 'summary_text', (SELECT SUM(score) FROM day))
On Conflict (created_at) DO UPDATE SET summary=EXCLUDED.summary, score=EXCLUDED.score
RETURNING score;
`
await connection.execute(db_request);
res.redirect('/')
})
app.listen(port, async () => {
// Create the notes table if it does not exist
const notes = await connection.execute(`
CREATE TABLE IF NOT EXISTS notes (
id BIGSERIAL PRIMARY KEY,
note VARCHAR(5000),
score FLOAT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);`
)
// Create the days table if it does not exist
const day = await connection.execute(`
CREATE TABLE IF NOT EXISTS days (
id BIGSERIAL PRIMARY KEY,
summary VARCHAR,
score FLOAT,
created_at DATE NOT NULL UNIQUE DEFAULT DATE(NOW())
);`
)
console.log(`Server is running at http://localhost:${port}`)
})