-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (75 loc) · 2.35 KB
/
app.js
File metadata and controls
84 lines (75 loc) · 2.35 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const app = express();
const port = 3000;
const addTaskToCalendar = require("./google_calendar");
const categoryColorMapping = {
Work: 5,
Personal: 2,
"High Priority": 11,
Default: 7,
};
app.use(bodyParser.json());
app.get("/", (req, res) => {
axios
.get("http://127.0.0.1:5000")
.then((result) => {
console.log(result.data);
res.json(result.data);
})
.catch((error) => {
console.error("Error with AI service:", error.message); // Log error
res.status(500).send("Error with AI service");
});
});
// Example endpoint to add a task
app.post("/add-task", (req, res) => {
const { task, deadline } = req.body;
// Ensure task and deadline are provided
if (!task || !deadline) {
return res.status(400).json({ message: "Task or deadline missing!" });
}
// Send task details to Python for prioritization
axios
.post("http://127.0.0.1:5000/prioritize", { task, deadline })
.then((response) => {
const category = response.data.category;
const priority = response.data.priority;
res.json({
message: "Task added and prioritized successfully!",
priority: priority,
task: task,
category: category,
});
const colorId =
categoryColorMapping[category] || categoryColorMapping["Default"];
addTaskToCalendar(task, deadline, priority, category, colorId);
})
.catch((error) => {
console.error("Error with AI service:", error.message); // Log error
res.status(500).send("Error with AI service");
});
});
// Endpoint to train the AI model
app.post("/train", (req, res) => {
const trainingData = req.body;
if (!Array.isArray(trainingData) || trainingData.length === 0) {
return res.status(400).json({ message: "Invalid or empty training data" });
}
axios
.post("http://127.0.0.1:5000/train", { trainingData })
.then((response) => {
res.json({
message: "Training data sent successfully!",
result: response.data,
});
})
.catch((error) => {
console.error("Error with AI training service:", error.message);
res.status(500).send("Error with AI training service");
});
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});