diff --git a/custom/live-transcription/.babelrc b/custom/live-transcription/.babelrc new file mode 100644 index 0000000..a6f4434 --- /dev/null +++ b/custom/live-transcription/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["next/babel"], + "plugins": ["inline-react-svg"] +} diff --git a/custom/live-transcription/README.md b/custom/live-transcription/README.md new file mode 100644 index 0000000..903c19a --- /dev/null +++ b/custom/live-transcription/README.md @@ -0,0 +1,46 @@ +# Live Transcription + + + +### Live example + +**[See it in action here ➡️](https://custom-live-transcription.vercel.app)** + +--- + +## What does this demo do? + +- Use `startTranscription()` and `stopTranscription()` methods to create a transcript of a call +- Integrates Deepgram transcription service into Daily calls +- Listen for incoming transcription messages using the call object `app-message` event +- Extend the basic call demo with a transcription provider and aside +- Show a notification bubble on transcript tray button when a new message is received + +Please note: this demo is not currently mobile optimised + +### Getting started + +``` +# set both DAILY_API_KEY and DAILY_DOMAIN +mv env.example .env.local + +yarn +yarn workspace @custom/live-transcription dev +``` + +### Setting up transcription + +For testing the transcription service, you will have to register for a Deepgram API key and configure your Daily domain with that key. + +TODO: Add more instructions here, or point to docs when ready. + +## How does this example work? + +In this example we extend the [basic call demo](../basic-call) with the ability to generate transcription of the meeting in real time and log that in a side panel. + +We pass a custom tray object, a custom app object (wrapping the original in a new `TranscriptionProvider`) as well as add our `TranscriptionAside` panel. We also symlink both the `public` and `pages/api` folders from the basic call. + + +## Deploy your own on Vercel + +[](https://vercel.com/new/daily-co/clone-flow?repository-url=https%3A%2F%2Fgithub.com%2Fdaily-demos%2Fexamples.git&env=DAILY_DOMAIN%2CDAILY_API_KEY&envDescription=Your%20Daily%20domain%20and%20API%20key%20can%20be%20found%20on%20your%20account%20dashboard&envLink=https%3A%2F%2Fdashboard.daily.co&project-name=daily-examples&repo-name=daily-examples) diff --git a/custom/live-transcription/components/App.js b/custom/live-transcription/components/App.js new file mode 100644 index 0000000..064f8c2 --- /dev/null +++ b/custom/live-transcription/components/App.js @@ -0,0 +1,13 @@ +import React from 'react'; + +import App from '@custom/basic-call/components/App'; +import { TranscriptionProvider } from '../contexts/TranscriptionProvider'; + +// Extend our basic call app component with the Live Transcription context +export const AppWithTranscription = () => ( + + + +); + +export default AppWithTranscription; diff --git a/custom/live-transcription/components/TranscriptionAside.js b/custom/live-transcription/components/TranscriptionAside.js new file mode 100644 index 0000000..7b17dd5 --- /dev/null +++ b/custom/live-transcription/components/TranscriptionAside.js @@ -0,0 +1,97 @@ +import React, { useEffect, useRef, useState } from 'react'; +import { Aside } from '@custom/shared/components/Aside'; +import Button from '@custom/shared/components/Button'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; +import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider'; +import { useUIState } from '@custom/shared/contexts/UIStateProvider'; +import { useTranscription } from '../contexts/TranscriptionProvider'; + +export const TRANSCRIPTION_ASIDE = 'transcription'; + +export const TranscriptionAside = () => { + const { callObject } = useCallState(); + const { showAside, setShowAside } = useUIState(); + const { transcriptionHistory } = useTranscription(); + const [isTranscribing, setIsTranscribing] = useState(false); + const { isOwner } = useParticipants(); + + const msgWindowRef = useRef(); + + + useEffect(() => { + if (msgWindowRef.current) { + msgWindowRef.current.scrollTop = msgWindowRef.current.scrollHeight; + } + }, [transcriptionHistory?.length]); + + if (!showAside || showAside !== TRANSCRIPTION_ASIDE) { + return null; + } + + async function startTranscription() { + setIsTranscribing(true); + await callObject.startTranscription(); + } + + async function stopTranscription() { + setIsTranscribing(false); + await callObject.stopTranscription(); + } + + return ( + + ); +}; + +export default TranscriptionAside; diff --git a/custom/live-transcription/components/Tray.js b/custom/live-transcription/components/Tray.js new file mode 100644 index 0000000..5041ab6 --- /dev/null +++ b/custom/live-transcription/components/Tray.js @@ -0,0 +1,26 @@ +import React from 'react'; + +import { TrayButton } from '@custom/shared/components/Tray'; +import { useUIState } from '@custom/shared/contexts/UIStateProvider'; +import { ReactComponent as IconTranscription } from '@custom/shared/icons/chat-md.svg'; +import { useTranscription } from '../contexts/TranscriptionProvider'; +import { TRANSCRIPTION_ASIDE } from './TranscriptionAside'; + +export const Tray = () => { + const { toggleAside } = useUIState(); + const { hasNewMessages } = useTranscription(); + + return ( + { + toggleAside(TRANSCRIPTION_ASIDE); + }} + > + + + ); +}; + +export default Tray; diff --git a/custom/live-transcription/contexts/TranscriptionProvider.js b/custom/live-transcription/contexts/TranscriptionProvider.js new file mode 100644 index 0000000..6703901 --- /dev/null +++ b/custom/live-transcription/contexts/TranscriptionProvider.js @@ -0,0 +1,92 @@ +import React, { + createContext, + useCallback, + useContext, + useEffect, + useState, +} from 'react'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; +import { nanoid } from 'nanoid'; +import PropTypes from 'prop-types'; + +export const TranscriptionContext = createContext(); + +export const TranscriptionProvider = ({ children }) => { + const { callObject } = useCallState(); + const [transcriptionHistory, setTranscriptionHistory] = useState([]); + const [hasNewMessages, setHasNewMessages] = useState(false); + const [isTranscribing, setIsTranscribing] = useState(false); + + const handleNewMessage = useCallback( + (e) => { + const participants = callObject.participants(); + // Collect only transcription messages, and gather enough + // words to be able to post messages at sentence intervals + if (e.fromId === 'transcription' && e.data?.is_final) { + + // Get the sender's current display name or the local name + const sender = e.data?.session_id !== participants.local.session_id + ? participants[e.data.session_id].user_name + : participants.local.user_name; + + setTranscriptionHistory((oldState) => [ + ...oldState, + { sender, message: e.data.text, id: nanoid() }, + ]); + } + + setHasNewMessages(true); + }, + [callObject] + ); + + const handleTranscriptionStarted = useCallback(() => { + console.log('💬 Transcription started'); + setIsTranscribing(true); + }, []); + + const handleTranscriptionStopped = useCallback(() => { + console.log('💬 Transcription stopped'); + setIsTranscribing(false); + }, []); + + const handleTranscriptionError = useCallback(() => { + console.log('❗ Transcription error!'); + setIsTranscribing(false); + }, []); + + useEffect(() => { + if (!callObject) { + return false; + } + + console.log(`💬 Transcription provider listening for app messages`); + + callObject.on('app-message', handleNewMessage); + + callObject.on('transcription-started', handleTranscriptionStarted); + callObject.on('transcription-stopped', handleTranscriptionStopped); + callObject.on('transcription-error', handleTranscriptionError); + + return () => callObject.off('app-message', handleNewMessage); + }, [callObject, handleNewMessage, handleTranscriptionStarted, handleTranscriptionStopped, handleTranscriptionError]); + + return ( + + {children} + + ); +}; + +TranscriptionProvider.propTypes = { + children: PropTypes.node, +}; + +export const useTranscription = () => useContext(TranscriptionContext); diff --git a/custom/live-transcription/env.example b/custom/live-transcription/env.example new file mode 100644 index 0000000..5ab7e03 --- /dev/null +++ b/custom/live-transcription/env.example @@ -0,0 +1,8 @@ +# Domain excluding 'https://' and 'daily.co' e.g. 'somedomain' +DAILY_DOMAIN= + +# Obtained from https://dashboard.daily.co/developers +DAILY_API_KEY= + +# Daily REST API endpoint +DAILY_REST_DOMAIN=https://api.daily.co/v1 diff --git a/custom/live-transcription/index.js b/custom/live-transcription/index.js new file mode 100644 index 0000000..9044efc --- /dev/null +++ b/custom/live-transcription/index.js @@ -0,0 +1 @@ +// Note: I am here because next-transpile-modules requires a mainfile diff --git a/custom/live-transcription/next.config.js b/custom/live-transcription/next.config.js new file mode 100644 index 0000000..9140e28 --- /dev/null +++ b/custom/live-transcription/next.config.js @@ -0,0 +1,13 @@ +const withPlugins = require('next-compose-plugins'); +const withTM = require('next-transpile-modules')([ + '@custom/shared', + '@custom/basic-call', +]); + +const packageJson = require('./package.json'); + +module.exports = withPlugins([withTM], { + env: { + PROJECT_TITLE: packageJson.description, + }, +}); diff --git a/custom/live-transcription/package.json b/custom/live-transcription/package.json new file mode 100644 index 0000000..16f434c --- /dev/null +++ b/custom/live-transcription/package.json @@ -0,0 +1,25 @@ +{ + "name": "@custom/live-transcription", + "description": "Basic Call + Transcription Example", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "@custom/shared": "*", + "@custom/basic-call": "*", + "next": "^11.0.0", + "pluralize": "^8.0.0", + "react": "^17.0.2", + "react-dom": "^17.0.2" + }, + "devDependencies": { + "babel-plugin-module-resolver": "^4.1.0", + "next-compose-plugins": "^2.2.1", + "next-transpile-modules": "^8.0.0" + } +} diff --git a/custom/live-transcription/pages/_app.js b/custom/live-transcription/pages/_app.js new file mode 100644 index 0000000..4d05411 --- /dev/null +++ b/custom/live-transcription/pages/_app.js @@ -0,0 +1,12 @@ +import React from 'react'; +import App from '@custom/basic-call/pages/_app'; +import AppWithTranscription from '../components/App'; + +import TranscriptionAside from '../components/TranscriptionAside'; +import Tray from '../components/Tray'; + +App.asides = [TranscriptionAside]; +App.customAppComponent = ; +App.customTrayComponent = ; + +export default App; diff --git a/custom/live-transcription/pages/api b/custom/live-transcription/pages/api new file mode 120000 index 0000000..999f604 --- /dev/null +++ b/custom/live-transcription/pages/api @@ -0,0 +1 @@ +../../basic-call/pages/api \ No newline at end of file diff --git a/custom/live-transcription/pages/index.js b/custom/live-transcription/pages/index.js new file mode 100644 index 0000000..84a3f53 --- /dev/null +++ b/custom/live-transcription/pages/index.js @@ -0,0 +1,13 @@ +import Index from '@custom/basic-call/pages'; +import getDemoProps from '@custom/shared/lib/demoProps'; + +export async function getStaticProps() { + const defaultProps = getDemoProps(); + + // Pass through domain as prop + return { + props: defaultProps, + }; +} + +export default Index; diff --git a/custom/live-transcription/public/assets/daily-logo-dark.svg b/custom/live-transcription/public/assets/daily-logo-dark.svg new file mode 100644 index 0000000..ef3a565 --- /dev/null +++ b/custom/live-transcription/public/assets/daily-logo-dark.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/custom/live-transcription/public/assets/daily-logo.svg b/custom/live-transcription/public/assets/daily-logo.svg new file mode 100644 index 0000000..534a18a --- /dev/null +++ b/custom/live-transcription/public/assets/daily-logo.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/custom/live-transcription/public/assets/join.mp3 b/custom/live-transcription/public/assets/join.mp3 new file mode 100644 index 0000000..7657915 Binary files /dev/null and b/custom/live-transcription/public/assets/join.mp3 differ diff --git a/custom/live-transcription/public/assets/message.mp3 b/custom/live-transcription/public/assets/message.mp3 new file mode 100644 index 0000000..a067315 Binary files /dev/null and b/custom/live-transcription/public/assets/message.mp3 differ diff --git a/custom/live-transcription/public/assets/pattern-bg.png b/custom/live-transcription/public/assets/pattern-bg.png new file mode 100644 index 0000000..01e0d0d Binary files /dev/null and b/custom/live-transcription/public/assets/pattern-bg.png differ