File size: 6,336 Bytes
6a06834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3520560
6a06834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import uuid
import gradio as gr
import assemblyai as aai
from translate import Translator
from elevenlabs import VoiceSettings
from elevenlabs.client import ElevenLabs
from pathlib import Path
from dotenv import load_dotenv
from PIL import Image

# Load environment variables
load_dotenv()

assemblyai_api_key = os.getenv("ASSEMBLYAI_API_KEY")
elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY")

# Initialize AssemblyAI and ElevenLabs clients
aai.settings.api_key = assemblyai_api_key
client = ElevenLabs(api_key=elevenlabs_api_key)

# Use relative paths
script_dir = Path(__file__).parent  # Gets the directory where the script is running
images_dir = script_dir / "images"  # Points to the 'images' folder in your project

ru_img = Image.open(images_dir / "Russia.png").resize((80, 40))
tr_img = Image.open(images_dir / "Turkey.png").resize((80, 40))
sv_img = Image.open(images_dir / "Sweden.png").resize((80, 40))
de_img = Image.open(images_dir / "Germany.png").resize((80, 40))
es_img = Image.open(images_dir / "Spain.png").resize((80, 40))
ja_img = Image.open(images_dir / "Japanese.png").resize((80, 40))


def voice_to_voice(audio_file, progress=gr.Progress()):
    try:
        # Transcribe speech
        progress(0.1, desc="Transcribing audio...")
        transcript = transcribe_audio(audio_file)

        if transcript.status == aai.TranscriptStatus.error:
            raise gr.Error(f"Transcription failed: {transcript.error}")
        else:
            transcript_text = transcript.text

        # Translate text
        progress(0.4, desc="Translating text...")
        list_translations = translate_text(transcript_text)

        # Generate speech from text
        generated_audio_paths = []
        for i, translation in enumerate(list_translations):
            progress(0.5 + (i * 0.1), desc=f"Generating audio for {['Russian', 'Turkish', 'Swedish', 'German', 'Spanish', 'Japanese'][i]}...")
            translated_audio_file_name = text_to_speech(translation)
            path = Path(translated_audio_file_name)
            generated_audio_paths.append(path)

        return generated_audio_paths + list_translations

    except Exception as e:
        raise gr.Error(f"An error occurred: {str(e)}")

# Function to transcribe audio using AssemblyAI
def transcribe_audio(audio_file):
    transcriber = aai.Transcriber()
    transcript = transcriber.transcribe(audio_file)
    return transcript

# Function to translate text
def translate_text(text: str) -> list:
    languages = ["ru", "tr", "sv", "de", "es", "ja"]
    list_translations = []
    for lan in languages:
        try:
            translator = Translator(from_lang="en", to_lang=lan)
            translation = translator.translate(text)
            list_translations.append(translation)
        except Exception as e:
            print(f"Translation to {lan} failed: {str(e)}")
            list_translations.append(f"Translation to {lan} failed.")
    return list_translations

# Function to generate speech
def text_to_speech(text: str) -> str:
    response = client.text_to_speech.convert(
        voice_id="Xb7hH8MSUJpSbSDYk0k2",  # Choose a voice on ElevenLabs dashboard and copy the id
        optimize_streaming_latency="0",
        output_format="mp3_22050_32",
        text=text,
        model_id="eleven_multilingual_v2",  # Use the turbo model for low latency, for other languages use the `eleven_multilingual_v2`
        voice_settings=VoiceSettings(
            stability=0.5,
            similarity_boost=0.75,
            style=0.0,
            use_speaker_boost=True,
        ),
    )

    save_file_path = f"{uuid.uuid4()}.mp3"

    # Writing the audio to a file
    with open(save_file_path, "wb") as f:
        for chunk in response:
            if chunk:
                f.write(chunk)

    print(f"{save_file_path}: A new audio file was saved successfully!")

    # Return the path of the saved audio file
    return save_file_path

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## Record yourself in English and immediately receive voice translations.")
    
    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(
                sources=["upload"],
                type="filepath",
                show_download_button=True,
                waveform_options=gr.WaveformOptions(
                    waveform_color="#01C6FF",
                    waveform_progress_color="#0066B4",
                    skip_length=2,
                    show_controls=False,
                ),
            )
            with gr.Row():
                submit = gr.Button("Submit", variant="primary")
                btn = gr.ClearButton(audio_input, "Clear")

    with gr.Row():
        with gr.Group() as russian:
            gr.Markdown("### Russian 🇷🇺")
            gr.Image(ru_img)
            ru_output = gr.Audio(label="Russian", interactive=False)
            ru_text = gr.Markdown()

        with gr.Group() as turkish:
            gr.Markdown("### Turkish 🇹🇷")
            gr.Image(tr_img)
            tr_output = gr.Audio(label="Turkish", interactive=False)
            tr_text = gr.Markdown()

        with gr.Group() as swedish:
            gr.Markdown("### Swedish 🇸🇪")
            gr.Image(sv_img)
            sv_output = gr.Audio(label="Swedish", interactive=False)
            sv_text = gr.Markdown()

    with gr.Row():
        with gr.Group() as german:
            gr.Markdown("### German 🇩🇪")
            gr.Image(de_img)
            de_output = gr.Audio(label="German", interactive=False)
            de_text = gr.Markdown()

        with gr.Group() as spanish:
            gr.Markdown("### Spanish 🇪🇸")
            gr.Image(es_img)
            es_output = gr.Audio(label="Spanish", interactive=False)
            es_text = gr.Markdown()

        with gr.Group() as japanese:
            gr.Markdown("### Japanese 🇯🇵")
            gr.Image(ja_img)
            jp_output = gr.Audio(label="Japanese", interactive=False)
            jp_text = gr.Markdown()

    output_components = [ru_output, tr_output, sv_output, de_output, es_output, jp_output, ru_text, tr_text, sv_text, de_text, es_text, jp_text]
    submit.click(fn=voice_to_voice, inputs=audio_input, outputs=output_components, show_progress=True)

if __name__ == "__main__":
    demo.launch()