Skip to content

Commit

Permalink
Add all files
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshihexus committed Dec 5, 2023
1 parent 31ddb61 commit 9b4a875
Show file tree
Hide file tree
Showing 24 changed files with 1,213 additions and 2 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

# Turborepo
.turbo

# typescript
*.tsbuildinfo

# cdk
.cdk.staging
cdk.out
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# gpt-wrapped
Unofficial GPT Year in Review
# chatgpt-wrapped

## Download node & pnpm - npm install -g pnpm
## Run pnpm build to check if the project build
## Run pnpm dev to start the Next.js app at http://localhost:3000.

# libraries

## D3 for visuzalition, compromise for NLP - runs slowly, can optimize further


# Note
## Feel free to create a pull request or report any bugs. Accuracy of the code is not guaranteed.
93 changes: 93 additions & 0 deletions app/[key]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"use client";
import Visualize from "ui/visualize";
import html2canvas from "html2canvas";
import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { useEffect, useState, useRef } from "react";
import useSWRImmutable from "swr/immutable";
import { BsLinkedin, BsFacebook, BsReddit } from "react-icons/bs";
import { FaSquareXTwitter } from "react-icons/fa6";

async function jsonFetcher<JSON = any>(
input: RequestInfo,
init?: RequestInit,
): Promise<JSON> {
const res = await fetch(input, init);
if (!res.ok) {
return JSON.parse('{"error":true}');
}
return res.json();
}
export default function Page({ params }: { params: { key: string } }) {
const [url, setUrl] = useState(
typeof window !== "undefined"
? window.location.href
: "https://hexus.ai/gpt_wrapped/",
);
const router = useRouter();
const { data, error, isLoading } = useSWRImmutable(
params.key && params.key.length > 0 ? `#ADD_API` : null,
jsonFetcher,
);
useEffect(() => {
if (error || (data && (data as any).error)) {
return router.push("/");
}
}, [data, error, isLoading]);

return (
<>
<a
className="font-sm fixed top-4 right-40 z-40 items-center rounded-full px-3 py-2 text-sm"
href={` https://www.reddit.com/submit?url=${url}`}
target="_blank"
>
<BsReddit
size={24}
className="w-16 text-gray-300 hover:text-[#1BA0F4]"
/>
</a>

<a
className="font-sm fixed top-4 right-28 z-40 items-center rounded-full px-3 py-2 text-sm"
href={` https://www.facebook.com/sharer/sharer.php?u=${url}`}
target="_blank"
>
<BsFacebook
size={24}
className="w-16 text-gray-300 hover:text-[#1BA0F4]"
/>
</a>
<a
className="font-sm fixed top-4 right-16 z-40 items-center rounded-full px-3 py-2 text-sm"
href={` https://www.linkedin.com/sharing/share-offsite/?url=${url}`}
target="_blank"
>
<BsLinkedin
size={24}
className="w-16 text-gray-300 hover:text-[#1BA0F4]"
/>
</a>

<a
className="font-sm fixed top-4 right-4 z-40 items-center rounded-full px-3 py-2 text-sm"
href={` https://twitter.com/intent/tweet?text=${
url + " " + "#GPTWrapped"
}`}
target="_blank"
>
<FaSquareXTwitter
size={24}
className="w-16 text-gray-300 hover:text-[#1BA0F4]"
/>
</a>
{isLoading ? (
<></>
) : (
<section className="static text-neutral-200 ">
<Visualize data={data} />
</section>
)}
</>
);
}
34 changes: 34 additions & 0 deletions app/client-layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client";
import Image from "next/image";
import { usePathname } from "next/navigation";
import { useRouter } from "next/navigation";
import Link from "next/link";

export const LayoutProvider = ({ children }) => {
const pathname = usePathname();
const router = useRouter();

return (
<>
<header className="sticky top-0 left-10 z-20 flex border-b border-slate-600 bg-slate-900 py-8 pl-6">
<>
<div className="flex">
<Link href="/" className="fixed top-4 ">
<Image
width={30}
height={100}
className="mt-1 h-6 w-auto"
src={
"https://assets-global.website-files.com/64809e6177ea6a3d2f622ed5/64974480708de09e4308b8c2_hexus%20logo%20(1).svg"
}
alt="logo"
/>
</Link>
</div>
</>
</header>

<main>{children}</main>
</>
);
};
198 changes: 198 additions & 0 deletions app/gpt_wrapped/text-analysis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import nlp from "compromise";

const STOPWORDS = [
"i",
"me",
"my",
"myself",
"we",
"our",
"ours",
"ourselves",
"you",
"your",
"yours",
"yourself",
"yourselves",
"he",
"him",
"his",
"himself",
"she",
"her",
"hers",
"herself",
"it",
"its",
"itself",
"they",
"them",
"their",
"theirs",
"themselves",
"what",
"which",
"who",
"whom",
"this",
"that",
"these",
"those",
"am",
"is",
"are",
"was",
"were",
"be",
"been",
"being",
"have",
"has",
"had",
"having",
"do",
"does",
"did",
"doing",
"a",
"an",
"the",
"and",
"but",
"if",
"or",
"because",
"as",
"until",
"while",
"of",
"at",
"by",
"for",
"with",
"about",
"against",
"between",
"into",
"through",
"during",
"before",
"after",
"above",
"below",
"to",
"from",
"up",
"down",
"in",
"out",
"on",
"off",
"over",
"under",
"again",
"further",
"then",
"once",
"here",
"there",
"when",
"where",
"why",
"how",
"all",
"any",
"both",
"each",
"few",
"more",
"most",
"other",
"some",
"such",
"no",
"nor",
"not",
"only",
"own",
"same",
"so",
"than",
"too",
"very",
"s",
"t",
"can",
"will",
"just",
"don",
"should",
"now",
];
export function getDataAnalysis(fileData: [], useNLP) {
let allText = [];

for (let value of fileData) {
let mapping = (value as any).mapping;
for (let key in mapping) {
if (mapping[key]?.message?.author?.role === "user") {
let contentArr = mapping[key].message.content.parts.join(".");
allText.push(contentArr);
}
}
}

if (useNLP) {
let doc = nlp(
allText
.join(" ")
.substring(0, 100000)
.replace(/[^\w\s]/gi, ""),
); // limit processing
let wordFrequency = doc
.nouns()
.out("topk") // sort by frequency
.concat(doc.adjectives().out("topk"))
.sort((a, b) => b.freq - a.freq)
.slice(0, 100);
return {
words: wordFrequency,
};
} else {
let docWordFrequency = mostFrequentWord(allText);
return { words: docWordFrequency };
}
}

function mostFrequentWord(allText) {
const words = allText
.join(" ")
.substring(0, 100000)
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "")
.toLowerCase()
.split(/\s+/);

// Count the frequency of each word
const wordCounts = {};
words.forEach((word) => {
wordCounts[word] = (wordCounts[word] || 0) + 1;
});

// Sort the words based on their frequencies
const sortedWords = Object.entries(wordCounts).sort(
(a, b) => (b as any)[1] - (a as any)[1],
);

// Take the top k words
const topKWords = sortedWords.slice(0, 300);
return removeStopWords(topKWords);
}

function removeStopWords(topKWords) {
// Remove stop words
return topKWords
.filter((word) => !STOPWORDS.includes(word[0]))
.map((wordArr) => {
return { normal: wordArr[0], count: wordArr[1] };
});
}
Loading

0 comments on commit 9b4a875

Please sign in to comment.