Skip to content

Commit

Permalink
feat: workss
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRohit committed Sep 24, 2024
1 parent bf3da6b commit 560f7bd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 67 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"youtubei.js": "^10.4.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240821.1",
"@cloudflare/workers-types": "^4.20240919.0",
"wrangler": "3.78.2"
}
}
}
22 changes: 3 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { KVNamespace, R2Bucket } from "@cloudflare/workers-types";
import { Hono } from "hono";
import { download } from "./tasks/download";
import processVideo from "./tasks/process-video";

export type Bindings = {
KV: KVNamespace;
Expand All @@ -15,23 +15,7 @@ export type Bindings = {
const app = new Hono<{ Bindings: Bindings }>();

app.get("/", (c) => {
return c.text("Hello , the deploy works!");
});

app.put("/put/:key", async (c) => {
const key = c.req.param("key");
const body = await c.req.text();
await c.env.KV.put(key, body);
return c.text(`Put ${key} successfully!`);
});

app.get("/get/:key", async (c) => {
const key = c.req.param("key");
const value = await c.env.KV.get(key);
if (value === null) {
return c.text(`Key ${key} not found`, 404);
}
return c.text(value);
return c.text("Hello , hehe");
});

app.get("/process-video/:id", async (c, env) => {
Expand All @@ -40,7 +24,7 @@ app.get("/process-video/:id", async (c, env) => {
if (!id) {
return c.json({ error: "Missing video ID" }, 400);
}
const result = await download(id, c);
const result = await processVideo(c, id);
return c.json(result);
} catch (error) {
console.error("Error processing video:", error);
Expand Down
76 changes: 37 additions & 39 deletions src/tasks/process-video.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Pinecone } from "@pinecone-database/pinecone";
import { traceable } from "langsmith/traceable";
import { download, VideoInfo } from "./download";
import { Context } from "hono";
import { traceable } from "langsmith/traceable";
import {
createPineconeIndex,
updatePineconeWithTranscription,
} from "../utils/rag-util";
import { download } from "./download";
import { generateChapters } from "./generate-chapters";
import { transcribe } from "./transcribe";

import type { Bindings } from "../index";

Expand All @@ -18,49 +17,48 @@ const processVideo = traceable(
});

try {
// const cachedData = await c.env.KV.get<{
// transcription: string;
// videoInfo: VideoInfo;
// output: string;
// }>(id);
const cachedData = await c.env.KV.get(id);
const parsed = JSON.parse(cachedData ?? "");

// if (cachedData) {
// console.log("----- CACHED -----");
// const { transcription, videoInfo, output } = cachedData;
// return {
// transcription,
// output,
// status: "complete",
// cached: true,
// videoInfo,
// };
// }
if (parsed) {
console.log("----- CACHED -----");
const { transcription, videoInfo, output } = parsed;
return {
transcription,
output,
status: "complete",
cached: true,
videoInfo,
};
}

// If not in cache, proceed with transcription
const result = await download(id, c);
// const { transcription, videoInfo } = result;

// const output = await generateChapters(c, transcription);
const { transcription, videoInfo } = result;

// await c.env.KV.put(id, JSON.stringify({ transcription, output }));
// await createPineconeIndex(client, "video-transcriptions", 1024);
// await updatePineconeWithTranscription(
// client,
// "video-transcriptions",
// transcription,
// id,
// c
// );
const output = await generateChapters(c, transcription ?? "");
console.log("ADDED TO KV");
await c.env.KV.put(
id,
JSON.stringify({ transcription, videoInfo, output })
);

// return {
// transcription,
// output,
// status: "complete",
// cached: false,
// videoInfo,
await createPineconeIndex(client, "video-transcriptions", 1024);
await updatePineconeWithTranscription(
client,
"video-transcriptions",
transcription ?? "",
id,
c
);

// };
return result;
return {
transcription,
output,
status: "complete",
cached: false,
videoInfo,
};
} catch (error) {
console.error("Error Processing Video", error);
throw new Error("An error occurred during video processing");
Expand Down
13 changes: 6 additions & 7 deletions src/utils/cookies/generate-yt-token.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Innertube } from "youtubei.js";

import { Innertube } from "youtubei.js/cf-worker";
const bail = (...msg) => {
console.error(...msg);
throw new Error(msg);
Expand All @@ -9,15 +8,15 @@ const tube = await Innertube.create();

tube.session.once("auth-pending", ({ verification_url, user_code }) => {
console.log(
"The token generated by this script is sensitive and you should not share it with anyone!",
"The token generated by this script is sensitive and you should not share it with anyone!"
);
console.log(
"By using this token, you are risking your Google account getting terminated.",
"By using this token, you are risking your Google account getting terminated."
);
console.log("You should not use your personal account!");
console.log();
console.log(
`Open ${verification_url} in a browser and enter ${user_code} when asked for the code.`,
`Open ${verification_url} in a browser and enter ${user_code} when asked for the code.`
);
});

Expand All @@ -32,8 +31,8 @@ tube.session.once("auth", ({ credentials }) => {
JSON.stringify(
Object.entries(credentials)
.map(([k, v]) => `${k}=${v instanceof Date ? v.toISOString() : v}`)
.join("; "),
),
.join("; ")
)
);
});

Expand Down

0 comments on commit 560f7bd

Please sign in to comment.