Skip to content

Commit

Permalink
Merge pull request #44 from TID-Lab/AITags
Browse files Browse the repository at this point in the history
Basic Templated AI Tagging
  • Loading branch information
codelastnight authored Oct 14, 2024
2 parents a53db6b + 61be23b commit ba6672f
Show file tree
Hide file tree
Showing 23 changed files with 9,109 additions and 5,494 deletions.
2 changes: 1 addition & 1 deletion backend/api/controllers/reportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const parseQueryData = (queryString) => {
if (!queryString) return {};
// Data passed through URL parameters
var query = _.pick(queryString, ['keywords', 'status', 'after', 'before', 'media',
'sourceId', 'groupId', 'author', 'tags', 'list', 'escalated', 'veracity', 'isRelevantReports']);
'sourceId', 'groupId', 'author', 'tags', 'list', 'escalated', 'veracity', 'isRelevantReports',]);
if (query.tags) query.tags = tags.toArray(query.tags);
return query;
}
Expand Down
2 changes: 2 additions & 0 deletions backend/fetching.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const errorListener = require('./fetching/listeners/error');
// Import hooks
const postToReport = require('./fetching/hooks/postToReport');
const saveToDatabase = require('./fetching/hooks/saveToDatabase');
const tagReportsAI = require('./fetching/hooks/tagReportsAI'); // Import the new hook

// Extend global error class
require('./error');
Expand All @@ -34,6 +35,7 @@ process.on('uncaughtException', function(err) {

// Use hooks
downstream.use(postToReport);
//downstream.use(tagReportsAI); // Add the new hook here
downstream.use(saveToDatabase);

// Register the error listener
Expand Down
1 change: 1 addition & 0 deletions backend/fetching/hooks/postToReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { parseJunkipediaPostMetadata } = require('../util');

module.exports = async function postToReport(post, next) {
console.log(post);
console.log('postToReport');
const {
channel: channelID,
platform,
Expand Down
47 changes: 47 additions & 0 deletions backend/fetching/hooks/tagReportsAI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Adds AI-generated tags to the report

module.exports = async function tagReportsAI(report, next) {
const axios = require("axios");

try {
console.log("PreAPI Report", JSON.parse(JSON.stringify(report)));
const response = await axios.post(
"http://localhost:8080/twoshotextract",
JSON.parse(JSON.stringify(report))
);
const data = response.data;

const aiTags = {};
const tagNames = [];

for (const key in data) {
if (!key.endsWith("_rationale") && !key == "red_flag") {
const rationaleKey = `${key}_rationale`;
aiTags[key] = {
value: data[key],
rationale: data[rationaleKey] || null,
};
tagNames.push(key);
}
}
console.log(aiTags);
console.log(tagNames);
report.aitags = aiTags;
report.aitagnames = tagNames;
report.red_flag = Boolean(data["red_flag"]);
} catch (error) {
console.error("Error fetching AI tags:", error);
const aiTags = {
key1: { value: true, rationale: "Default rationale for key1" },
key2: {
value: "default string",
rationale: "Default rationale for key2",
},
};
report.aitags = aiTags;
report.aitagnames = Object.keys(aiTags);
}
console.log(report);

await next();
};
2 changes: 1 addition & 1 deletion backend/fetching/sourceToChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function createChannel(source) {
// Reference - https://www.junkipedia.org/apidocs#tag/Posts/paths/~1api~1v1~1posts/get
// TODO: Add list or channel specification
// interval is set to 30 minutes
interval: 900000,
interval: 60000,
queryParams: {
lists: lists,
keyword: keywords,
Expand Down
10 changes: 9 additions & 1 deletion backend/models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Schema = mongoose.Schema;
const SchemaTypes = mongoose.SchemaTypes;
const SMTCTag = require('./tag');
const { addPost, removePost } = require('../comments');
const { strict } = require('assert');

let schema = new Schema({
authoredAt: { type: Date, index: true },
Expand All @@ -32,7 +33,14 @@ let schema = new Schema({
notes: { type: String },
escalated: { type: Boolean, default: false, required: true, index: true },
content_lang: { type: String },
irrelevant: { type: String, default: 'false', required: false, enum: ['false', 'true', 'maybe'] }
irrelevant: { type: String, default: 'false', required: false, enum: ['false', 'true', 'maybe'] },
aitags: {
type: Map,
of: SchemaTypes.Mixed,
default: {},
},
aitagnames: { type: [String], default: [] },
red_flag: { type: Boolean, default: false, index: true }
});

schema.index({ 'metadata.ct_tag': 1 }, { background: true });
Expand Down
Loading

0 comments on commit ba6672f

Please sign in to comment.