-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
160 lines (130 loc) · 4.76 KB
/
generator.js
File metadata and controls
160 lines (130 loc) · 4.76 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// specifically for CSE TY
let fs = require("fs");
let { spawn } = require("child_process");
const { mdToPdf } = require("md-to-pdf");
const { mkdir } = require("fs").promises;
let subjects = {
bis: "Business Intelligence System",
cns: "Cryptography Network Security",
dt: "Design Thinking",
nlp: "Natural Language Processing",
misc: "Miscellaneous",
};
let note =
"Note : this document was generated by an AI and is not approved by any faculty from our college";
let website = ` \n\n\n\n<br> <hr> <br> <p align="center"> By <a href="http://65.0.14.141/" style="text-decoration:none;"> Assignment Buddy </a> </p>`;
Object.keys(subjects).forEach(async (sub) => {
try {
await mkdir("./documents/" + sub);
console.log(`${sub} directory created successfully`);
} catch (e) {
console.log(`${sub} directory already exists`);
}
});
let toPDF = async (path, cb, authKey, username) => {
const pdf = await mdToPdf({ path: path }).catch(console.error);
let temp = false;
if (pdf) {
if (!config.hasAccess(authKey, username)) {
temp = '-' + path.split("-")[1].replace(".md","");
setTimeout(() => {
if (path.includes("temp")) fs.unlinkSync(path.replace(".md", ".pdf"));
if (path.includes("temp")) fs.unlinkSync(path);
}, config.fileDeleteDelay * 1000);
}
fs.writeFileSync(path.replace(".md", ".pdf"), pdf.content);
cb(path.replace(".md", ".pdf"), temp ? temp.replace(".md", "") : temp);
console.log("Done...");
} else {
cb("-Error could not write PDF");
}
};
function Datify() {
return Date().split(" ").slice(0, 3).join(" ").replace(" ", ", ");
}
//console.log(Datify());
let heading = (title, number, type) => {
if(type.toLowerCase().startsWith("exp")) type = type.slice(0,10);
let text = `<span style='color:red'><i> (${note}) </i> <br> <span style="color:green;"> </span> </span> \n \n # ${type} ${number}\n **Class** : CSE TY <br>**Subject** : ${title} <br>**Generated On** : ${Datify()} \n <hr> \n\n`;
return text;
};
class Generator {
constructor(subject, number, type, cb) {
this.subject = subject ? subjects[subject] : "_";
this.number = number;
this.subjectPrefix = subject;
this.year = 3;
this.type = type ? type : "Assignment";
// this.extra = extra ? extra : "BTech";
this.c = 0;
this.path = `./documents/${this.subjectPrefix}/${
this.subjectPrefix + this.number
}.md`;
this.__init__();
this.readMeStream = fs.createWriteStream(this.path);
this.callback = cb;
}
__init__() {
switch(this.type.charAt(0).toLowerCase()) {
case 'e': {
console.log(this.type);
let batch = this.type.toLowerCase().replace("experiment","");
this.path = this.path.replace(`${this.subjectPrefix + this.number}`,`${this.subjectPrefix + this.number + "exp" + batch}`)
}
break;
case 'q': {
console.log(this.type);
let qbNumber = this.type.toLowerCase().replace("questionbank","");
this.path = this.path.replace(`${this.subjectPrefix + this.number}`,`${this.subjectPrefix + this.number + "qb" + qbNumber}`)
}
break;
default : {}
}
}
askQ(qs, authKey, username,notFirst) {
if(!notFirst) {
if (!subjects[this.subjectPrefix])
throw Error("ERROR : Invalid subject prefix");
if (!config.hasAccess(authKey, username)) {
this.path = this.path.replace(".md", "-temp" + Date.now() + ".md");
this.readMeStream = fs.createWriteStream(
this.path
);
}
}
this.readMeStream.write(heading(this.subject, this.number, this.type));
console.log("Generating Answer...");
this.AIStream = spawn("node", ["ai.mjs"]);
this.AIStream.on("error", (e) => console.log(e));
this.readMeStream.write(
` \n\n #### Q${this.c + 1}. ${qs[this.c]} \n\n #### Ans. <br> \n`
);
this.AIStream.stderr.on("data", (err) => console.log(err.toString()));
this.AIStream.stdin.write("-" + qs[this.c] + ",in " + this.subject);
this.AIStream.stdout.on("data", (data) => {
this.readMeStream.write(data);
});
this.AIStream.stdout.on("end", () => {
console.log("Done...");
this.c += 1;
if (qs[this.c]) {
this.AIStream.kill();
this.AIStream = null;
this.askQ(qs, authKey, username,true);
} else {
this.readMeStream.write(` <i>${website} </i>`);
this.readMeStream.end();
console.log("Converting to PDF...");
toPDF(this.path, this.callback, authKey, username);
}
});
}
}
module.exports = Generator;
/*
let qs = [
"What is a Cloud and explain Software as a Service (SaaS) in detail.",
"Define ETL process and explain in detail with architecture. Support your answer with a diagram.",
"Write a short note on: a. BI tools b. Types of users",
];
BIS.askQ(qs);*/