diff --git a/.env.example b/.env.example deleted file mode 100644 index fbce9b8..0000000 --- a/.env.example +++ /dev/null @@ -1 +0,0 @@ -OPENAI_API_KEY="" diff --git a/README.md b/README.md index aa0358b..e44a9c8 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ To set up the project locally, follow these steps: 4. Start the development server with `npm run dev`. 5. Open `http://localhost:3000` to view it in the browser. -## How to use Environment Variable +## How to use Environment Variable (Disabled and reverted to keep things purely on Client Side) 1. Copy the `.env.example` file to: - .env.local file: For Local Development (This should not be committed to Git) - `cp .env.example .env.local` - .env: For Production Deployment (This can be via Vercel Environment Variables under your Project's settings page. `cp .env.example .env` diff --git a/src/pages/index.js b/src/pages/index.js index 03119e3..ac6ef49 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -23,26 +23,18 @@ import { Box, Tooltip, Switch, - FormHelperText -} from '@chakra-ui/react'; + 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 - +import { useState, useRef, useEffect } from "react"; +import { saveAs } from "file-saver"; // You will need to install file-saver: npm install file-saver export default function Home() { - const apiKeyFromEnv = process.env.OPENAI_API_KEY; - const [apiKeyInput, setApiKey] = useState(''); + const [apiKeyInput, setApiKey] = useState(""); - useEffect(() => { - if (!!apiKeyFromEnv) { - setApiKey(apiKeyFromEnv); - } - }, [apiKeyFromEnv]); - - const [model, setModel] = useState('tts-1'); - const [inputText, setInputText] = useState(''); - const [voice, setVoice] = useState('alloy'); + 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); @@ -62,14 +54,13 @@ export default function Home() { const toast = useToast(); const handleModelToggle = () => { - setModel(model === 'tts-1' ? 'tts-1-hd' : 'tts-1'); + setModel(model === "tts-1" ? "tts-1-hd" : "tts-1"); }; const handleDownload = () => { - saveAs(audioUrl, 'speech.mp3'); // This will save the file as "speech.mp3" + 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(); @@ -78,7 +69,7 @@ export default function Home() { try { // Define the request headers const headers = new Headers(); - const apiKey = apiKeyFromEnv && !!apiKeyFromEnv ? apiKeyFromEnv : apiKeyInput; + const apiKey = apiKeyInput; headers.append("Authorization", `Bearer ${apiKey}`); headers.append("Content-Type", "application/json"); @@ -87,12 +78,12 @@ export default function Home() { model: model, input: inputText, voice: voice, - speed: speed.toFixed(1) + 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', + const response = await fetch("https://api.openai.com/v1/audio/speech", { + method: "POST", headers: headers, body: body, }); @@ -111,13 +102,12 @@ export default function Home() { // Update your component's state or context setAudioUrl(audioUrl); - } catch (error) { console.error("Error:", error); toast({ - title: 'An error occurred', + title: "An error occurred", description: error.message, - status: 'error', + status: "error", duration: 5000, isClosable: true, }); @@ -126,10 +116,6 @@ export default function Home() { } }; - - - - const handleInputChange = (e) => { if (e.target.value.length <= 4096) { setInputText(e.target.value); @@ -137,9 +123,15 @@ export default function Home() { }; return ( - + - + - - - Open-Audio TTS - Powered by OpenAI TTS - - - View on GitHub - - - + + + + Open-Audio TTS + + + Powered by OpenAI TTS{" "} + + + + View on GitHub + + + - API Key + API Key setApiKey(e.target.value)} variant="outline" borderColor="black" - disabled={!!apiKeyFromEnv} // Disable the input if apiKeyFromEnv is truthy /> - - Quality - - + Quality + - - {model === 'tts-1' ? 'High' : 'HD'} + + {model === "tts-1" ? "High" : "HD"} - - Input Text + Input Text