Spaces:
Running
Running
File size: 5,271 Bytes
4fb0c68 7f37689 4fb0c68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import { useState } from 'react';
import { textToSpeech } from '../services/api';
import languageVoices from '../data/languageVoices'; // Adjust the path as needed
function TextToSpeech() {
const [text, setText] = useState('');
const [languageCode, setLanguageCode] = useState('en-US');
const [ssmlGender, setSsmlGender] = useState('NEUTRAL');
const [name, setName] = useState('en-US-Standard-C');
const [pitch, setPitch] = useState(0.0);
const [speakingRate, setSpeakingRate] = useState(1.0);
const [volumeGainDb, setVolumeGainDb] = useState(0.0);
const [audioUrl, setAudioUrl] = useState(null);
// Ensure you have VITE_API_URL set in your .env file
const API_URL = import.meta.env.VITE_API_URL;
const handleTextToSpeech = async () => {
// Call text-to-speech service
try {
const response = await textToSpeech(text, languageCode, ssmlGender, name, pitch, speakingRate, volumeGainDb);
const baseUrl = new URL(API_URL);
const audioUrl = `${baseUrl.origin}/static/storage/exported/${response}`;
console.log(audioUrl)
setAudioUrl(audioUrl);
} catch (error) {
console.error('Text-to-Speech failed:', error);
}
};
const handleDeleteAudio = () => {
setAudioUrl(null);
};
return (
<div className="flex justify-center items-center min-h-screen bg-gray-100 p-4">
<div className="w-full max-w-3xl p-6 bg-white rounded-lg shadow-md">
<h2 className="text-3xl font-bold mb-6 text-center text-blue-500">Text-to-Speech AI</h2>
<div className="mb-6">
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
className="w-full h-32 p-3 border rounded"
placeholder="Enter some text here..."
/>
</div>
<div className="mb-4">
<label className="block text-gray-700 font-bold mb-2">Volume</label>
<input
type="range"
min="-96.0"
max="16.0"
step="0.1"
value={volumeGainDb}
onChange={(e) => setVolumeGainDb(parseFloat(e.target.value))}
className="w-full"
/>
<span className="block text-right text-gray-600">{volumeGainDb.toFixed(1)}</span>
</div>
<div className="mb-4">
<label className="block text-gray-700 font-bold mb-2">Rate</label>
<input
type="range"
min="0.25"
max="4.0"
step="0.01"
value={speakingRate}
onChange={(e) => setSpeakingRate(parseFloat(e.target.value))}
className="w-full"
/>
<span className="block text-right text-gray-600">{speakingRate.toFixed(2)}</span>
</div>
<div className="mb-4">
<label className="block text-gray-700 font-bold mb-2">Pitch</label>
<input
type="range"
min="-20.0"
max="20.0"
step="0.1"
value={pitch}
onChange={(e) => setPitch(parseFloat(e.target.value))}
className="w-full"
/>
<span className="block text-right text-gray-600">{pitch.toFixed(1)}</span>
</div>
<div className="mb-4">
<label className="block text-gray-700 font-bold mb-2">Language</label>
<select
value={languageCode}
onChange={(e) => {
setLanguageCode(e.target.value);
setName(languageVoices[e.target.value][0].name); // Set default voice for selected language
}}
className="w-full p-2 border rounded"
>
{Object.keys(languageVoices).map((lang) => (
<option key={lang} value={lang}>
{lang}
</option>
))}
</select>
</div>
<div className="mb-4">
<label className="block text-gray-700 font-bold mb-2">Voice</label>
<select
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full p-2 border rounded"
>
{languageVoices[languageCode].map((voice) => (
<option key={voice.name} value={voice.name}>
{voice.label}
</option>
))}
</select>
</div>
<div className="flex justify-between mt-6">
<button
onClick={handleTextToSpeech} // Stop button handler
className="w-fit px-4 py-2 bg-blue-500 text-white rounded hover:shadow-lg hover:bg-blue-700 transition duration-200"
>
Generate
</button>
</div>
{audioUrl && (
<div className="mt-6">
<div className="flex items-center">
<audio controls src={audioUrl} className="w-full">
Your browser does not support the audio element.
</audio>
<button
onClick={handleDeleteAudio}
className="ml-4 px-4 py-2 bg-red-500 text-white rounded hover:bg-red-700 transition duration-200"
>
Delete
</button>
</div>
</div>
)}
</div>
</div>
);
}
export default TextToSpeech;
|