Loading...
) : !feed.data?.pages[0]?.count ? (
@@ -57,103 +195,12 @@ export const MainFeed: React.FC<{
{feed.data?.pages.map((posts) =>
posts?.list.map((post) => {
- currentLength++
return (
-
-
-
-
-
-
-
-
- {post.character?.metadata?.content?.name ||
- post.character?.handle}
-
-
-
-
·
-
-
-
-
-
- {post.metadata?.content?.title}
-
-
- {!!post.metadata?.content?.tags?.filter(
- (tag) => tag !== "post" && tag !== "page",
- ).length && (
-
- {post.metadata?.content?.tags
- ?.filter(
- (tag) => tag !== "post" && tag !== "page",
- )
- .map((tag) => (
- {
- e.preventDefault()
- router.push(`/tag/${tag}`)
- }}
- >
- #{tag}
-
- ))}
-
- )}
-
-
- {post.metadata?.content?.summary}
- {post.metadata?.content?.summary && "..."}
-
-
- {post.metadata?.content.cover && (
-
-
-
- )}
-
-
+
)
}),
)}
diff --git a/src/lib/ipfs-parser.ts b/src/lib/ipfs-parser.ts
index 1340fc79..cbcc389e 100644
--- a/src/lib/ipfs-parser.ts
+++ b/src/lib/ipfs-parser.ts
@@ -31,4 +31,5 @@ export const toCid = (url: string) => {
.replaceAll("https://cf-ipfs.com/ipfs/", "")
.replaceAll("https://ipfs.4everland.xyz/ipfs/", "")
.replaceAll("https://rss3.mypinata.cloud/ipfs/", "")
+ .replaceAll(IPFS_PREFIX, "")
}
diff --git a/src/models/page.model.ts b/src/models/page.model.ts
index afa389a1..c65452c9 100644
--- a/src/models/page.model.ts
+++ b/src/models/page.model.ts
@@ -640,6 +640,10 @@ export async function getSummary({
).data
}
+export async function getScore({ cid }: { cid: string }) {
+ return (await (await fetch(`/api/score?cid=${cid}`)).json()).data
+}
+
export async function checkMirror(characterId: string) {
const notes = await indexer.getNotes({
characterId,
diff --git a/src/pages/api/score.ts b/src/pages/api/score.ts
new file mode 100644
index 00000000..dec6cdc9
--- /dev/null
+++ b/src/pages/api/score.ts
@@ -0,0 +1,121 @@
+import { NextApiRequest, NextApiResponse } from "next"
+
+import { toGateway } from "~/lib/ipfs-parser"
+import prisma from "~/lib/prisma.server"
+import { cacheGet } from "~/lib/redis.server"
+
+const getOriginalScore = async (cid: string) => {
+ try {
+ const { content } = await (await fetch(toGateway(`ipfs://${cid}`))).json()
+
+ if (content) {
+ console.time(`fetching score ${cid}`)
+
+ const prompt = `According to rule 1 not too short content, rule 2 good originality and innovation, and rule 3 good fun or logic, give this article a score in the range of 0-100 and explain the reason:
+ "${content}"
+ Score:`
+ const response = await (
+ await fetch("https://api.openai.com/v1/chat/completions", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
+ },
+ body: JSON.stringify({
+ model: "gpt-4",
+ temperature: 0,
+ messages: [
+ {
+ role: "user",
+ content: prompt,
+ },
+ ],
+ }),
+ })
+ ).json()
+
+ console.timeEnd(`fetching score ${cid}`)
+
+ return {
+ score: parseInt(response.choices?.[0]?.message?.content?.trim()),
+ reason: response.choices?.[0]?.message?.content
+ ?.trim()
+ .replace(/^\d+([,.\s]*)/, "")
+ .trim()
+ .replace(/^Reason:/, "")
+ .trim(),
+ }
+ }
+ } catch (error) {
+ console.error(error)
+ console.timeEnd(`fetching score ${cid}`)
+ }
+}
+
+export async function getScore(cid: string) {
+ const score = await cacheGet({
+ key: ["summary_score222", cid],
+ getValueFun: async () => {
+ const meta = await prisma.metadata.findFirst({
+ where: {
+ uri: `ipfs://${cid}`,
+ },
+ })
+ if (meta) {
+ if (meta?.ai_score !== null) {
+ return {
+ score: meta.ai_score,
+ reason: meta.ai_score_reason,
+ }
+ } else {
+ const score = await getOriginalScore(cid)
+ if (score) {
+ await prisma.metadata.update({
+ where: {
+ uri: `ipfs://${cid}`,
+ },
+ data: {
+ ai_score: score.score,
+ ai_score_reason: score.reason,
+ },
+ })
+
+ return score
+ }
+ }
+ } else {
+ const score = await getOriginalScore(cid)
+ if (score) {
+ await prisma.metadata.create({
+ data: {
+ uri: `ipfs://${cid}`,
+ ai_score: score.score,
+ ai_score_reason: score.reason,
+ },
+ })
+
+ return score
+ }
+ }
+ },
+ noUpdate: true,
+ })
+
+ return score
+}
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ let { cid } = req.query
+
+ if (!cid) {
+ res.status(400).send("Bad Request")
+ return
+ }
+
+ res.status(200).json({
+ data: await getScore(cid as string),
+ })
+}
diff --git a/src/pages/api/summary.ts b/src/pages/api/summary.ts
index dd7f807f..e07420c6 100644
--- a/src/pages/api/summary.ts
+++ b/src/pages/api/summary.ts
@@ -21,40 +21,44 @@ const chains = new Map
()
const getOriginalSummary = async (cid: string, lang: string) => {
try {
- console.log("fetching summary", cid, lang)
-
const { content } = await (await fetch(toGateway(`ipfs://${cid}`))).json()
- let chain = chains.get(lang)
- if (!chain) {
- const prompt = new PromptTemplate({
- template: `Summarize this in ${lang} language:
- "{text}"
- CONCISE SUMMARY:`,
- inputVariables: ["text"],
+ if (content) {
+ console.time(`fetching summary ${cid}, ${lang}`)
+
+ let chain = chains.get(lang)
+ if (!chain) {
+ const prompt = new PromptTemplate({
+ template: `Summarize this in "${lang}" language:
+ "{text}"
+ CONCISE SUMMARY:`,
+ inputVariables: ["text"],
+ })
+
+ const combineDocsChain = loadSummarizationChain(model, {
+ prompt,
+ combineMapPrompt: prompt,
+ combinePrompt: prompt,
+ })
+
+ chain = new AnalyzeDocumentChain({
+ combineDocumentsChain: combineDocsChain,
+ })
+
+ chains.set(lang, chain)
+ }
+
+ const res = await chain.call({
+ input_document: content,
})
- const combineDocsChain = loadSummarizationChain(model, {
- prompt,
- combineMapPrompt: prompt,
- combinePrompt: prompt,
- })
+ console.timeEnd(`fetching summary ${cid}, ${lang}`)
- chain = new AnalyzeDocumentChain({
- combineDocumentsChain: combineDocsChain,
- })
-
- chains.set(lang, chain)
+ return res?.text
}
-
- const res = await chain.call({
- input_document: content,
- })
-
- return res?.text
} catch (error) {
console.error(error)
- return undefined
+ console.timeEnd(`fetching summary ${cid}, ${lang}`)
}
}
diff --git a/src/queries/page.ts b/src/queries/page.ts
index 9defdb0f..52a16aa5 100644
--- a/src/queries/page.ts
+++ b/src/queries/page.ts
@@ -256,6 +256,17 @@ export function useGetSummary(input: { cid?: string; lang?: string }) {
})
}
+export function useGetScore(input: { cid?: string }) {
+ return useQuery(["getScore", input.cid], async () => {
+ if (!input.cid) {
+ return
+ }
+ return pageModel.getScore({
+ cid: input.cid,
+ })
+ })
+}
+
export function useGetMirrorXyz(input: { address: string }) {
return useQuery(["getMirror", input.address], async () => {
if (!input.address) {