Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): rewrite everything from scratch, use classes, decorators etc #33

Merged
merged 13 commits into from
Nov 5, 2023
Merged
10 changes: 7 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ jobs:
uses: actions/checkout@v3

- name: Install dependencies
run: npm install
# temporary fix for installing deps for api
run: |
npm install
cd api && npm install

- name: Build code
run: |
cp ~/important/interchat-env .env
npm run build --if-present
npm run build --if-present
cd .. && npm run build --if-present
cp ~/important/interchat-env .env

- name: Deploy Commands
run: npm run deploy -- -b
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ tsconfig.tsbuildinfo
# Configuration
.env

# Unit tests
tests/
# Unit test coverage
__tests__/

# Unit test coverage reports
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# InterChat
Code for a growing discord bot which provides a fun inter-server chat!

// subcommand files must have the same name as the subcommand registered on discord
// commands and subcommands must be default exports

## Starting the bot:
1. Install dependencies using `npm install`
2. Make a file called `.env` and fill it out with the appropriate contents mentioned in the env.example file.
Expand Down
3 changes: 3 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules

dist/
42 changes: 42 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import express from 'express';
import { node } from '@tensorflow/tfjs-node';
import { load, predictionType } from 'nsfwjs';

const model = await load();
const app = express();
const port = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const analyzeImage = async (url: string): Promise<predictionType[] | null> => {
const imageBuffer = await (await fetch(url)).arrayBuffer();

const imageTensor = await node.decodeImage(Buffer.from(imageBuffer), 3) as any;

Check warning on line 15 in api/index.ts

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected any. Specify a different type
const predictions = await model.classify(imageTensor);
imageTensor.dispose();

return predictions;
};

app.get('/nsfw', async (req, res) => {
const url = req.query.url;

if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url query parameter.' });

const regex =
/(?:(?:(?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)(?:(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)(?:\.jpg|\.jpeg|\.png)/;
if (!regex.test(url)) {
return res
.status(400)
.send({ error: 'Invalid url parameter. Must be a valid PNG, JPG or JPEG image URL.' });
}

const predictions = await analyzeImage(url);
if (!predictions) return res.status(500).json({ error: 'Something went wrong while analyzing the image.' });
return res.status(200).json(predictions);
});

app.listen(port, () => {
console.log(`API listening on port ${port}`);

Check warning on line 41 in api/index.ts

View workflow job for this annotation

GitHub Actions / Lint Check

Unexpected console statement
});
Loading