Added initial codebase for open-audio

This commit is contained in:
Malhar Ujawane 2023-11-07 20:51:19 -08:00
parent 512c701060
commit 5e53a36c9e
9 changed files with 2561 additions and 144 deletions

2
.gitignore vendored
View File

@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts
.env

2250
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,15 +9,22 @@
"lint": "next lint"
},
"dependencies": {
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.1",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"file-saver": "^2.0.5",
"framer-motion": "^10.16.4",
"next": "14.0.1",
"openai": "^4.16.1",
"react": "^18",
"react-dom": "^18",
"next": "14.0.1"
"react-dom": "^18"
},
"devDependencies": {
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.0.1"
"eslint-config-next": "14.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0"
}
}

BIN
public/favicon.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

1
public/og-image.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -1,5 +1,13 @@
import '@/styles/globals.css'
import { ChakraProvider, extendTheme } from '@chakra-ui/react';
const theme = extendTheme({});
export default function App({ Component, pageProps }) {
return <Component {...pageProps} />
}
return (
<ChakraProvider theme={theme}> {/* If using a custom theme */}
<Component {...pageProps} />
</ChakraProvider>
);
}

View File

@ -3,7 +3,32 @@ import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<Head>
{/* Page Title */}
<title>Open-Audio TTS</title>
{/* Favicon */}
<link rel="icon" type="image/png" sizes="16x16" href="/favicon.png" />
{/* Meta Tags for SEO */}
<meta name="description" content="An OpenAI-powered Text-to-Speech tool." />
{/* Open Graph / Facebook */}
<meta property="og:type" content="website" />
<meta property="og:url" content="https://www.yourdomain.com/" />
<meta property="og:title" content="Open-Audio TTS" />
<meta property="og:description" content="An OpenAI-powered Text-to-Speech tool." />
<meta property="og:image" content="https://www.yourdomain.com/images/og-image.svg" />
{/* Twitter */}
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content="https://www.yourdomain.com/" />
<meta property="twitter:title" content="Open-Audio TTS" />
<meta property="twitter:description" content="An OpenAI-powered Text-to-Speech tool." />
<meta property="twitter:image" content="https://www.yourdomain.com/images/twitter-og-image.svg" />
{/* Add additional meta tags as needed */}
</Head>
<body>
<Main />
<NextScript />

View File

@ -1,118 +1,294 @@
import Image from 'next/image'
import { Inter } from 'next/font/google'
// pages/index.js
import {
Container,
Flex,
Heading,
FormControl,
FormLabel,
Input,
Textarea,
Select,
Button,
VStack,
HStack,
Slider,
SliderTrack,
SliderFilledTrack,
SliderThumb,
SliderMark,
Text,
useToast,
Spinner,
Grid,
Box,
Tooltip,
Switch,
FormHelperText
} from '@chakra-ui/react';
import OpenAI from "openai";
import { useState, useRef, useEffect } from 'react';
import { saveAs } from 'file-saver'; // You will need to install file-saver: npm install file-saver
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
const [apiKeyInput, setApiKey] = useState('');
const [model, setModel] = useState('tts-1');
const [inputText, setInputText] = useState('');
const [voice, setVoice] = useState('alloy');
const [speed, setSpeed] = useState(1);
const [isSubmitting, setIsSubmitting] = useState(false);
const [sliderValue, setSliderValue] = useState(1);
const [showTooltip, setShowTooltip] = useState(false);
const sliderRef = useRef(null);
const [audioUrl, setAudioUrl] = useState(null);
useEffect(() => {
// Clean up the URL object when the component is unmounted or audioUrl changes
return () => {
if (audioUrl) {
URL.revokeObjectURL(audioUrl);
}
};
}, [audioUrl]);
const toast = useToast();
const handleModelToggle = () => {
setModel(model === 'tts-1' ? 'tts-1-hd' : 'tts-1');
};
const handleDownload = () => {
saveAs(audioUrl, 'speech.mp3'); // This will save the file as "speech.mp3"
};
// Assuming `openai.audio.speech.create` returns a stream or binary data
const handleSubmit = async (e) => {
e.preventDefault();
setIsSubmitting(true);
setAudioUrl(null);
try {
// Define the request headers
const headers = new Headers();
headers.append("Authorization", `Bearer ${apiKeyInput}`);
headers.append("Content-Type", "application/json");
// Define the request body
const body = JSON.stringify({
model: model,
input: inputText,
voice: voice,
speed: speed.toFixed(1)
});
// Make the fetch request to the OpenAI API
const response = await fetch('https://api.openai.com/v1/audio/speech', {
method: 'POST',
headers: headers,
body: body,
});
console.log(response);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Get the response body as Blob
const blob = await response.blob();
// Create a URL for the Blob
const audioUrl = URL.createObjectURL(blob);
// Update your component's state or context
setAudioUrl(audioUrl);
} catch (error) {
console.error("Error:", error);
toast({
title: 'An error occurred',
description: error.message,
status: 'error',
duration: 5000,
isClosable: true,
});
} finally {
setIsSubmitting(false);
}
};
const handleInputChange = (e) => {
if (e.target.value.length <= 4096) {
setInputText(e.target.value);
}
};
return (
<main
className={`flex min-h-screen flex-col items-center justify-between p-24 ${inter.className}`}
>
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">src/pages/index.js</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
<Container bg={'gray.100'} maxW="container">
<Container centerContent p={4} maxW="container.md">
<Flex direction="column" align="center" justify="center" minH="100vh" w="full">
<Box
bg="white" // Assuming the card is white
borderRadius="lg" // Rounded corners
boxShadow="lg" // Shadow effect
p={6} // Padding inside the card
w="full" // Full width of the parent
maxW="md" // Maximum width
>
By{' '}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>
<VStack spacing={6} as="form" onSubmit={handleSubmit} width="full" maxW="md">
<Box bg='black' w='100%' p={5} borderTopRadius="md" boxShadow="lg">
<Heading textAlign="center" color="white">Open-Audio TTS</Heading>
<Text fontSize="xs" color="gray.100" textAlign="center" mt={2}>Powered by OpenAI TTS</Text>
</Box>
<Grid
templateColumns={{ md: '4fr 1fr' }} // 80-20 ratio
gap={4}
width="full"
>
<FormControl isRequired>
<FormLabel htmlFor='api-key'>API Key</FormLabel>
<Input
id='api-key'
placeholder='Enter your OpenAI API key'
type='password'
value={apiKeyInput}
onChange={(e) => setApiKey(e.target.value)}
variant="outline"
borderColor="black"
/>
</FormControl>
<div className="relative flex place-items-center before:absolute before:h-[300px] before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700/10 after:dark:from-sky-900 after:dark:via-[#0141ff]/40 before:lg:h-[360px]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>
<FormControl>
<VStack align="start" spacing={0}>
<FormLabel htmlFor='model'>
Quality
</FormLabel>
<HStack align="center" h='100%' mx='0' mt='2' >
<Switch
id='model'
colorScheme="blackAlpha"
isChecked={model === 'tts-1-hd'}
onChange={handleModelToggle}
size="md" // Optional: if you want a larger switch
/>
<FormHelperText textAlign="center" mt={'-1'}>
{model === 'tts-1' ? 'High' : 'HD'}
</FormHelperText>
</HStack>
</VStack>
</FormControl>
<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Docs{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Find in-depth information about Next.js features and API.
</p>
</a>
</Grid>
<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Learn{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>
<FormControl isRequired>
<FormLabel htmlFor='input-text'>Input Text</FormLabel>
<Textarea
id='input-text'
placeholder='Enter the text you want to convert to speech'
value={inputText}
onChange={handleInputChange}
resize="vertical"
maxLength={4096}
borderColor="black"
/>
<Box textAlign="right" fontSize="sm">
{inputText.length} / 4096
</Box>
</FormControl>
<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Templates{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Discover and deploy boilerplate example Next.js&nbsp;projects.
</p>
</a>
<HStack width="full" justifyContent="space-between">
<FormControl isRequired width="45%">
<FormLabel htmlFor='voice'>Voice</FormLabel>
<Select
id='voice'
value={voice}
onChange={(e) => setVoice(e.target.value)}
variant="outline"
placeholder="Select voice"
borderColor="black"
focusBorderColor="black"
colorScheme="blackAlpha"
_hover={{ borderColor: 'gray.400' }} // Optional: style for hover state
>
{/* List of supported voices */}
<option value='alloy'>Alloy</option>
<option value='echo'>Echo</option>
<option value='fable'>Fable</option>
<option value='onyx'>Onyx</option>
<option value='nova'>Nova</option>
<option value='shimmer'>Shimmer</option>
</Select>
</FormControl>
<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Deploy{' '}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
</main>
)
<FormControl width="40%" mt='-15'>
<FormLabel htmlFor='speed'>Speed </FormLabel>
<Slider
id='speed'
defaultValue={1}
min={0.25}
max={4}
step={0.25}
onChange={(v) => setSliderValue(v)}
onMouseEnter={() => setShowTooltip(true)}
onMouseLeave={() => setShowTooltip(false)}
ref={sliderRef}
aria-label='slider-ex-1'
>
<SliderTrack bg="gray">
<SliderFilledTrack bg="black" />
</SliderTrack>
<Tooltip
hasArrow
bg='black'
color='white'
placement='bottom'
isOpen={showTooltip}
label={`${(sliderValue).toFixed(2)}x`}
>
<SliderThumb />
</Tooltip>
</Slider>
</FormControl>
</HStack>
<Button
size="lg"
bg='black'
color={'white'}
colorScheme='black'
borderColor="black"
type='submit'
isLoading={isSubmitting}
loadingText="Generating..."
>
Create Speech
</Button>
{isSubmitting && (
<Spinner
thickness="4px"
speed="0.65s"
emptyColor="gray.200"
color="black"
size="md"
/>
)}
{audioUrl && (
<>
<audio controls src={audioUrl}>
Your browser does not support the audio element.
</audio>
<Button onClick={handleDownload}>Download MP3</Button>
</>
)}
</VStack>
</Box>
</Flex>
</Container>
</Container>
);
}

View File

@ -7,11 +7,7 @@ module.exports = {
],
theme: {
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
},
},
},
plugins: [],