Skip to content

Commit

Permalink
add sensitive-filters
Browse files Browse the repository at this point in the history
  • Loading branch information
idoubi committed Nov 17, 2023
1 parent 8cfc605 commit bb68726
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
avatars
12 changes: 10 additions & 2 deletions web/app/api/gpts/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { getRandRows } from "@/app/models/gpts";

export async function GET() {
const data = await getRandRows(0, 50);
try {
const data = await getRandRows(0, 50);

return Response.json({ data: data });
return Response.json({ code: 0, message: "ok", data: data });
} catch (e) {
console.log("get gpts failed: ", e);
return Response.json({
code: -1,
message: "failed",
});
}
}
1 change: 1 addition & 0 deletions web/app/api/gptsall/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function POST(req: Request) {
});
}
} catch (e) {
console.log("get all gpts failed: ", e);
return Response.json({ code: -1, message: e });
}
}
3 changes: 2 additions & 1 deletion web/app/components/Brand/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default ({ count }: Props) => {
Third-party GPTs store
</h2>
<p className="mt-4 mb-4 md:mt-12 md:mb-8 text:lg md:text-4xl">
<span className="text-primary">{count}</span> fantastic GPTs stored
<span className="text-primary font-bold">{count}</span> fantastic
GPTs stored
<a
href="https://github.com/airyland/gptshunter.com/issues/1"
target="_blank"
Expand Down
18 changes: 13 additions & 5 deletions web/app/models/gpts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { QueryResultRow, sql } from "@vercel/postgres";

import { Gpts } from "../types/gpts";
import { Gpts } from "@/app/types/gpts";
import { isGptsSensitive } from "@/app/services/gpts";

export async function createTable() {
const res = await sql`CREATE TABLE gpts (
Expand Down Expand Up @@ -58,7 +59,9 @@ export async function getRows(last_id: number, limit: number): Promise<Gpts[]> {

rows.forEach((row) => {
const gpt = formatGpts(row);
gpts.push(gpt);
if (gpt) {
gpts.push(gpt);
}
});

return gpts;
Expand All @@ -76,10 +79,11 @@ export async function getRandRows(

const gpts: Gpts[] = [];
const { rows } = res;

rows.forEach((row) => {
const gpt = formatGpts(row);
gpts.push(gpt);
if (gpt) {
gpts.push(gpt);
}
});

return gpts;
Expand Down Expand Up @@ -110,7 +114,7 @@ export async function findByUuid(uuid: string): Promise<Gpts | undefined> {
return gpts;
}

function formatGpts(row: QueryResultRow): Gpts {
function formatGpts(row: QueryResultRow): Gpts | undefined {
const gpts: Gpts = {
uuid: row.uuid,
org_id: row.org_id,
Expand All @@ -126,5 +130,9 @@ function formatGpts(row: QueryResultRow): Gpts {
// detail: row.detail,
};

if (isGptsSensitive(gpts)) {
return;
}

return gpts;
}
24 changes: 23 additions & 1 deletion web/app/services/gpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,33 @@ export const searchGpts = async (question: string): Promise<Gpts[]> => {
});
const res = await resp.json();
if (res.data) {
return res.data;
return res.data.filter((gpts: Gpts) => !isGptsSensitive(gpts));
}
} catch (e) {
console.log("request gpts search failed: ", e);
}

return [];
};

export function isGptsSensitive(gpts: Gpts): boolean {
if (!gpts.name || !gpts.author_name || !gpts.description) {
return true;
}

const sensitiveKeywords = process.env.SENSITIVE_KEYWORDS || "";
const keywordsArr = sensitiveKeywords.split(",");
for (let i = 0, l = keywordsArr.length; i < l; i++) {
const keyword = keywordsArr[i].trim();
if (
gpts.name.includes(keyword) ||
gpts.description.includes(keyword) ||
gpts.author_name.includes(keyword)
) {
console.log("gpt is sensitive: ", gpts.uuid, gpts.name, keyword);
return true;
}
}

return false;
}
12 changes: 12 additions & 0 deletions web/debug/apitest.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
### update gpts
POST {{baseUri}}/update-gpts

### get all gpts
POST {{baseUri}}/gptsall
Content-Type: application/json

{
"last_id": 0,
"limit": 3
}

### get random gpts
GET {{baseUri}}/gpts

### search gpts
POST {{baseUri}}/search
Content-Type: application/json
Expand Down

0 comments on commit bb68726

Please sign in to comment.