diff --git a/examples/langtail-youtube-context/.env.example b/examples/langtail-youtube-context/.env.example new file mode 100644 index 0000000..df24710 --- /dev/null +++ b/examples/langtail-youtube-context/.env.example @@ -0,0 +1,7 @@ +# DO NOT COMMIT THIS FILE - IT WILL CONTAIN YOUR SENSITIVE KEYS. +# Instead, rename this file to ".env" instead of ".env.example". (.env will not be committed) + +# Create an OpenAI API key at https://platform.openai.com/account/api-keys +OPENAI_API_KEY="sk-123..." +LANGTAIL_API_KEY="" +DATABASE_URL="file:./dev.db" \ No newline at end of file diff --git a/examples/langtail-youtube-context/.gitignore b/examples/langtail-youtube-context/.gitignore new file mode 100644 index 0000000..5d092d7 --- /dev/null +++ b/examples/langtail-youtube-context/.gitignore @@ -0,0 +1,43 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js +.yarn/install-state.gz + +# 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 + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts + +# python env +venv + +# ignore temp folder +temp \ No newline at end of file diff --git a/examples/langtail-youtube-context/README.md b/examples/langtail-youtube-context/README.md new file mode 100644 index 0000000..1f61de3 --- /dev/null +++ b/examples/langtail-youtube-context/README.md @@ -0,0 +1,55 @@ +# Hackathon Project: Youtube Context + Langtail + +## Description +User provides youtube link and the app will provide summary of the video in the language of their choice. + +## Restrictions +- Long videos will have large transcriptions. E.g. 1 hour interview video could have >200K tokens. + +# TypeScript Next.js example + +This is a really simple project that shows the usage of Next.js with TypeScript. + +## Deploy your own + +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-typescript) + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-typescript&project-name=with-typescript&repository-name=with-typescript) + +## How to use it? + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example: + +```bash +npx create-next-app --example with-typescript with-typescript-app +``` + +```bash +yarn create next-app --example with-typescript with-typescript-app +``` + +```bash +pnpm create next-app --example with-typescript with-typescript-app +``` + +Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). + +## Notes + +This example shows how to integrate the TypeScript type system into Next.js. Since TypeScript is supported out of the box with Next.js, all we have to do is to install TypeScript. + +``` +npm install --save-dev typescript +``` + +To enable TypeScript's features, we install the type declarations for React and Node. + +``` +npm install --save-dev @types/react @types/react-dom @types/node +``` + +When we run `next dev` the next time, Next.js will start looking for any `.ts` or `.tsx` files in our project and builds it. It even automatically creates a `tsconfig.json` file for our project with the recommended settings. + +Next.js has built-in TypeScript declarations, so we'll get autocompletion for Next.js' modules straight away. + +A `type-check` script is also added to `package.json`, which runs TypeScript's `tsc` CLI in `noEmit` mode to run type-checking separately. You can then include this, for example, in your `test` scripts. diff --git a/examples/langtail-youtube-context/components.json b/examples/langtail-youtube-context/components.json new file mode 100644 index 0000000..53969d2 --- /dev/null +++ b/examples/langtail-youtube-context/components.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://ui.shadcn.com/schema.json", + "style": "new-york", + "rsc": true, + "tsx": true, + "tailwind": { + "config": "tailwind.config.js", + "css": "globals.css", + "baseColor": "red", + "cssVariables": true, + "prefix": "" + }, + "aliases": { + "components": "@/components", + "utils": "@/lib/utils" + } +} diff --git a/examples/langtail-youtube-context/get_context.py b/examples/langtail-youtube-context/get_context.py new file mode 100644 index 0000000..bcee9d4 --- /dev/null +++ b/examples/langtail-youtube-context/get_context.py @@ -0,0 +1,28 @@ +# get_context.py + +import sys +import subprocess +import os + +def download_transcript(video_url): + command = f"yt-dlp --write-auto-sub --sub-lang en --skip-download {video_url} -o 'transcript.%(ext)s'" + subprocess.run(command, shell=True, check=True) + transcript_file = 'transcript.en.vtt' + if os.path.exists(transcript_file): + with open(transcript_file, 'r') as file: + transcript = file.read() + return transcript + else: + raise FileNotFoundError(f"Transcript file {transcript_file} not found") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python get_context.py ") + sys.exit(1) + + video_url = sys.argv[1] + try: + transcript = download_transcript(video_url) + print(transcript) + except Exception as e: + print(f"Error: {e}") diff --git a/examples/langtail-youtube-context/globals.css b/examples/langtail-youtube-context/globals.css new file mode 100644 index 0000000..6011548 --- /dev/null +++ b/examples/langtail-youtube-context/globals.css @@ -0,0 +1,69 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +@layer base { + :root { + --background: 0 0% 100%; + --foreground: 0 0% 3.9%; + --card: 0 0% 100%; + --card-foreground: 0 0% 3.9%; + --popover: 0 0% 100%; + --popover-foreground: 0 0% 3.9%; + --primary: 0 72.2% 50.6%; + --primary-foreground: 0 85.7% 97.3%; + --secondary: 0 0% 96.1%; + --secondary-foreground: 0 0% 9%; + --muted: 0 0% 96.1%; + --muted-foreground: 0 0% 45.1%; + --accent: 0 0% 96.1%; + --accent-foreground: 0 0% 9%; + --destructive: 0 84.2% 60.2%; + --destructive-foreground: 0 0% 98%; + --border: 0 0% 89.8%; + --input: 0 0% 89.8%; + --ring: 0 72.2% 50.6%; + --radius: 0.5rem; + --chart-1: 12 76% 61%; + --chart-2: 173 58% 39%; + --chart-3: 197 37% 24%; + --chart-4: 43 74% 66%; + --chart-5: 27 87% 67%; + } + + .dark { + --background: 0 0% 3.9%; + --foreground: 0 0% 98%; + --card: 0 0% 3.9%; + --card-foreground: 0 0% 98%; + --popover: 0 0% 3.9%; + --popover-foreground: 0 0% 98%; + --primary: 0 72.2% 50.6%; + --primary-foreground: 0 85.7% 97.3%; + --secondary: 0 0% 14.9%; + --secondary-foreground: 0 0% 98%; + --muted: 0 0% 14.9%; + --muted-foreground: 0 0% 63.9%; + --accent: 0 0% 14.9%; + --accent-foreground: 0 0% 98%; + --destructive: 0 62.8% 30.6%; + --destructive-foreground: 0 0% 98%; + --border: 0 0% 14.9%; + --input: 0 0% 14.9%; + --ring: 0 72.2% 50.6%; + --chart-1: 220 70% 50%; + --chart-2: 160 60% 45%; + --chart-3: 30 80% 55%; + --chart-4: 280 65% 60%; + --chart-5: 340 75% 55%; + } +} + +@layer base { + * { + @apply border-border; + } + body { + @apply bg-background text-foreground; + } +} diff --git a/examples/langtail-youtube-context/lib/prisma.ts b/examples/langtail-youtube-context/lib/prisma.ts new file mode 100644 index 0000000..20b75a3 --- /dev/null +++ b/examples/langtail-youtube-context/lib/prisma.ts @@ -0,0 +1,13 @@ +import { PrismaClient } from "@prisma/client"; + +const prismaClientSingleton = () => { + return new PrismaClient(); +}; + +declare global { + var prisma: undefined | ReturnType; +} + +export const prisma = globalThis.prisma ?? prismaClientSingleton(); + +if (process.env.NODE_ENV !== "production") globalThis.prisma = prisma; diff --git a/examples/langtail-youtube-context/lib/styles.ts b/examples/langtail-youtube-context/lib/styles.ts new file mode 100644 index 0000000..365058c --- /dev/null +++ b/examples/langtail-youtube-context/lib/styles.ts @@ -0,0 +1,6 @@ +import { type ClassValue, clsx } from "clsx"; +import { twMerge } from "tailwind-merge"; + +export function cn(...inputs: ClassValue[]) { + return twMerge(clsx(inputs)); +} diff --git a/examples/langtail-youtube-context/lib/trpc.ts b/examples/langtail-youtube-context/lib/trpc.ts new file mode 100644 index 0000000..7938e45 --- /dev/null +++ b/examples/langtail-youtube-context/lib/trpc.ts @@ -0,0 +1,47 @@ +import { httpBatchLink } from "@trpc/client"; +import { createTRPCNext } from "@trpc/next"; +import type { AppRouter } from "@/server/routers/_app"; + +function getBaseUrl() { + if (typeof window !== "undefined") + // browser should use relative path + return ""; + + if (process.env.VERCEL_URL) + // reference for vercel.com + return `https://${process.env.VERCEL_URL}`; + + if (process.env.RENDER_INTERNAL_HOSTNAME) + // reference for render.com + return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`; + + // assume localhost + return `http://localhost:${process.env.PORT ?? 3000}`; +} + +export const trpc = createTRPCNext({ + config(opts) { + return { + links: [ + httpBatchLink({ + /** + * If you want to use SSR, you need to use the server's full URL + * @link https://trpc.io/docs/v11/ssr + **/ + url: `${getBaseUrl()}/api/trpc`, + + // You can pass any HTTP headers you wish here + async headers() { + return { + // authorization: getAuthCookie(), + }; + }, + }), + ], + }; + }, + /** + * @link https://trpc.io/docs/v11/ssr + **/ + ssr: false, +}); diff --git a/examples/langtail-youtube-context/lib/utils.ts b/examples/langtail-youtube-context/lib/utils.ts new file mode 100644 index 0000000..529dee2 --- /dev/null +++ b/examples/langtail-youtube-context/lib/utils.ts @@ -0,0 +1,5 @@ +import { Langtail } from "langtail"; + +export const lt = new Langtail({ + apiKey: process.env.LANGTAIL_API_KEY ?? "", +}); diff --git a/examples/langtail-youtube-context/package.json b/examples/langtail-youtube-context/package.json new file mode 100644 index 0000000..fe28d23 --- /dev/null +++ b/examples/langtail-youtube-context/package.json @@ -0,0 +1,42 @@ +{ + "private": true, + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start", + "type-check": "tsc" + }, + "dependencies": { + "@hookform/resolvers": "^3.3.4", + "@prisma/client": "5.17.0", + "@radix-ui/react-icons": "^1.3.0", + "@tanstack/react-query": "^5.51.3", + "@trpc/client": "11.0.0-rc.465", + "@trpc/next": "11.0.0-rc.465", + "@trpc/react-query": "11.0.0-rc.465", + "@trpc/server": "11.0.0-rc.465", + "autoprefixer": "^10.4.19", + "class-variance-authority": "^0.7.0", + "clsx": "^2.1.1", + "langtail": "^0.5.4", + "next": "latest", + "openai": "^4.52.7", + "postcss": "^8.4.39", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-hook-form": "^7.45.4", + "sqlite3": "^5.1.7", + "tailwind-merge": "^2.4.0", + "tailwindcss": "^3.4.6", + "tailwindcss-animate": "^1.0.7", + "youtube-dl-exec": "^3.0.6", + "zod": "^3.23.8" + }, + "devDependencies": { + "@types/node": "^12.12.21", + "@types/react": "^17.0.2", + "@types/react-dom": "^17.0.1", + "prisma": "^5.17.0", + "typescript": "^4.8.3" + } +} diff --git a/examples/langtail-youtube-context/pages/_app.tsx b/examples/langtail-youtube-context/pages/_app.tsx new file mode 100644 index 0000000..8b0b4bf --- /dev/null +++ b/examples/langtail-youtube-context/pages/_app.tsx @@ -0,0 +1,8 @@ +import type { AppType } from "next/app"; +import { trpc } from "@/lib/trpc"; +import "../globals.css"; + +const MyApp: AppType = ({ Component, pageProps }) => { + return ; +}; +export default trpc.withTRPC(MyApp); diff --git a/examples/langtail-youtube-context/pages/api/trpc/[trpc].ts b/examples/langtail-youtube-context/pages/api/trpc/[trpc].ts new file mode 100644 index 0000000..7dde7d2 --- /dev/null +++ b/examples/langtail-youtube-context/pages/api/trpc/[trpc].ts @@ -0,0 +1,8 @@ +import * as trpcNext from "@trpc/server/adapters/next"; +import { appRouter } from "../../../server/routers/_app"; +// export API handler +// @link https://trpc.io/docs/v11/server/adapters +export default trpcNext.createNextApiHandler({ + router: appRouter, + createContext: () => ({}), +}); diff --git a/examples/langtail-youtube-context/pages/api/youtube-context.ts b/examples/langtail-youtube-context/pages/api/youtube-context.ts new file mode 100644 index 0000000..e69de29 diff --git a/examples/langtail-youtube-context/pages/index.tsx b/examples/langtail-youtube-context/pages/index.tsx new file mode 100644 index 0000000..be05f55 --- /dev/null +++ b/examples/langtail-youtube-context/pages/index.tsx @@ -0,0 +1,230 @@ +import { zodResolver } from "@hookform/resolvers/zod"; +import { FormEvent, useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; +import { cn } from "@/lib/styles"; +import { trpc } from "@/lib/trpc"; + +const formSchema = z.object({ + url: z.string().url(), + maxLength: z.string().optional(), + notes: z.string().optional(), + useWhisper: z.boolean().optional(), + videoId: z.string(), +}); + +export default function IndexPage() { + const form = useForm>({ + mode: "all", + resolver: zodResolver(formSchema), + defaultValues: { + url: "", + maxLength: "", + notes: "", + useWhisper: false, + videoId: "", + }, + }); + + const getVideos = trpc.getVideos.useQuery(); + const getVideoInfo = trpc.getVideoInfo.useMutation(); + const processViaAutoTranscription = + trpc.processViaAutoTranscription.useMutation(); + const processViaAudioWhisper = trpc.processViaAudioWhisper.useMutation(); + + const isProcessing = + processViaAutoTranscription.isPending || processViaAudioWhisper.isPending; + + const message = + processViaAutoTranscription.data?.[0]?.message?.content || + processViaAudioWhisper.data?.[0]?.message?.content; + + const handleUrlInputBlur = (e: FormEvent) => { + const validationResult = z.string().url().safeParse(e.currentTarget.value); + + if (!validationResult.success) return; + + const url = validationResult.data; + + if (url) { + if (getVideoInfo.data?.video.url !== url) { + processViaAutoTranscription.reset(); + processViaAudioWhisper.reset(); + getVideoInfo.mutate({ + url, + }); + } + } else { + getVideoInfo.reset(); + processViaAutoTranscription.reset(); + processViaAudioWhisper.reset(); + } + }; + + useEffect(() => { + if (getVideoInfo.data?.video.id) { + form.setValue("videoId", getVideoInfo.data.video.id); + } + }, [getVideoInfo.data?.video.id]); + + return ( +
+
+

YouTube to social media

+
+
+
+ {!getVideoInfo.data || getVideoInfo.isPending ? ( + + +
{ + if (data.useWhisper) { + processViaAutoTranscription.reset(); + processViaAudioWhisper.mutate({ + url: data.url, + videoId: data.videoId, + }); + } else { + processViaAudioWhisper.reset(); + processViaAutoTranscription.mutate({ + url: data.url, + videoId: data.videoId, + }); + } + })} + > + + +