Skip to content

Commit

Permalink
fix: use description if no subtitles
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 8, 2023
1 parent 5e291ac commit dbee3e6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/fetchSubtitle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { VideoService } from "~/lib/types";
import { fetchYoutubeSubtitleUrls, SUBTITLE_DOWNLOADER_URL } from "~/lib/youtube/fetchYoutubeSubtitleUrls";
import {
fetchYoutubeSubtitleUrls,
SUBTITLE_DOWNLOADER_URL,
} from "~/lib/youtube/fetchYoutubeSubtitleUrls";
import { find } from "~/utils/fp";
import { fetchBilibiliSubtitleUrls } from "./bilibili/fetchBilibiliSubtitleUrls";

Expand Down Expand Up @@ -51,10 +54,11 @@ export async function fetchSubtitle(
// });
// @ts-ignore
const res = await fetchBilibiliSubtitleUrls(videoId);
const title = res?.title;
const { title, desc, dynamic } = res || {};
const descriptionText = desc + "\n" + dynamic;
const subtitleList = res?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
return { title, subtitles: null };
return { title, subtitles: null, descriptionText };
}

const betterSubtitle =
Expand All @@ -81,5 +85,5 @@ export async function fetchSubtitle(
};
}
);
return { title, subtitles: transcripts };
return { title, subtitles: transcripts, descriptionText };
}
2 changes: 1 addition & 1 deletion lib/openai/trimOpenAiResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function trimOpenAiResult(result: any) {
const answer = result.choices[0].message?.content || "";
if (answer.startsWith("\n\n")) {
return answer.substring(2);
return answer.substring(1);
}
return answer;
}
12 changes: 8 additions & 4 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ export default async function handler(
if (!videoId) {
return new Response("No videoId in the request", { status: 500 });
}
const { title, subtitles } = await fetchSubtitle(
const { title, subtitles, descriptionText } = await fetchSubtitle(
videoId,
service,
shouldShowTimestamp
);
if (!subtitles) {
if (!subtitles && !descriptionText) {
console.error("No subtitle in the video: ", videoId);
return new Response("No subtitle in the video", { status: 501 });
}
const text = getChunckedTranscripts(subtitles, subtitles);
const prompt = getSummaryPrompt(title, text, { shouldShowTimestamp });
const inputText = subtitles
? getChunckedTranscripts(subtitles, subtitles)
: descriptionText;
const prompt = getSummaryPrompt(title, inputText, {
shouldShowTimestamp: subtitles ? shouldShowTimestamp : false,
});

try {
userKey && console.log("========use user apiKey========");
Expand Down

1 comment on commit dbee3e6

@vercel
Copy link

@vercel vercel bot commented on dbee3e6 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.