File size: 6,812 Bytes
5ae02e5
23dd469
 
5ae02e5
23dd469
388fe1b
23dd469
6418068
23dd469
388fe1b
 
 
23dd469
5ae02e5
 
 
 
 
 
 
 
 
388fe1b
5ae02e5
 
388fe1b
6418068
 
 
 
 
 
5ae02e5
6418068
 
 
 
 
 
 
 
23dd469
6418068
 
 
 
 
 
 
 
 
 
 
 
23dd469
6418068
 
 
 
 
23dd469
6418068
 
 
 
 
 
 
 
 
5ae02e5
6418068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388fe1b
6418068
388fe1b
 
 
 
6418068
388fe1b
 
6418068
388fe1b
 
 
 
6418068
 
388fe1b
 
 
 
6418068
 
388fe1b
6418068
 
 
 
 
 
 
388fe1b
6418068
388fe1b
 
 
6418068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388fe1b
6418068
 
 
 
 
5ae02e5
6418068
388fe1b
23dd469
 
6418068
 
388fe1b
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import gradio as gr
import torch
import torchaudio
import numpy as np
from transformers import AutoProcessor, SeamlessM4Tv2Model
from datetime import datetime

class SeamlessTranslator:
    def __init__(self, model_name: str = "facebook/seamless-m4t-v2-large"):
        self.processor = AutoProcessor.from_pretrained(model_name)
        self.model = SeamlessM4Tv2Model.from_pretrained(model_name)
        self.sample_rate = self.model.config.sampling_rate
        
        self.language_codes = {
            "English": "eng",
            "Spanish": "spa",
            "French": "fra",
            "German": "deu",
            "Italian": "ita",
            "Portuguese": "por",
            "Russian": "rus",
            "Chinese": "cmn",
            "Japanese": "jpn"
        }

    def translate(self, text: str, src_lang: str, tgt_lang: str) -> tuple[int, np.ndarray]:
        try:
            inputs = self.processor(text=text, src_lang=self.language_codes[src_lang], return_tensors="pt")
            audio_array = self.model.generate(**inputs, tgt_lang=self.language_codes[tgt_lang])[0].cpu().numpy().squeeze()
            return self.sample_rate, audio_array
        except Exception as e:
            raise gr.Error(f"Translation failed: {str(e)}")

# Custom CSS for Jarvis theme
css = """
#jarvis-interface {
    background-color: black !important;
    background-image: radial-gradient(circle at center, #00303030 0%, #00000080 100%);
    min-height: 100vh;
    font-family: 'Courier New', monospace;
}

#status-ring {
    width: 300px;
    height: 300px;
    border: 4px solid #00ffff;
    border-radius: 50%;
    margin: 20px auto;
    position: relative;
    animation: pulse 2s infinite;
    display: flex;
    align-items: center;
    justify-content: center;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0.4); }
    70% { box-shadow: 0 0 0 20px rgba(0, 255, 255, 0); }
    100% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0); }
}

#inner-ring {
    width: 200px;
    height: 200px;
    border: 2px solid #00ffff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#core {
    width: 100px;
    height: 100px;
    border: 3px solid #00ffff;
    border-radius: 50%;
    background-color: black;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #00ffff;
    text-align: center;
    padding: 10px;
}

.jarvis-textbox {
    background-color: black !important;
    border: 2px solid #00ffff !important;
    color: #00ffff !important;
    font-family: 'Courier New', monospace !important;
}

.jarvis-button {
    background-color: transparent !important;
    border: 2px solid #00ffff !important;
    color: #00ffff !important;
    font-family: 'Courier New', monospace !important;
}

.jarvis-button:hover {
    background-color: rgba(0, 255, 255, 0.1) !important;
}

.status-box {
    background-color: black !important;
    border: 2px solid #00ffff !important;
    color: #00ffff !important;
    padding: 10px !important;
    border-radius: 5px !important;
    margin: 5px !important;
    text-align: center !important;
}
"""

def create_interface():
    translator = SeamlessTranslator()
    
    def translate_text(text: str, src_lang: str, tgt_lang: str, progress=gr.Progress()):
        progress(0, desc="Initializing...")
        progress(0.3, desc="Processing text...")
        sample_rate, audio = translator.translate(text, src_lang, tgt_lang)
        progress(0.7, desc="Generating audio...")
        progress(1.0, desc="Complete!")
        return audio
    
    with gr.Blocks(css=css, title="J.A.R.V.I.S Translator") as demo:
        gr.Markdown(
            """
            # J.A.R.V.I.S TRANSLATION SYSTEM
            ### Powered by SeamlessM4T
            """
        )
        
        # Jarvis interface container
        with gr.Column(elem_id="jarvis-interface"):
            # Status Ring
            gr.HTML("""
                <div id="status-ring">
                    <div id="inner-ring">
                        <div id="core">
                            <div>JARVIS</div>
                            <div>ACTIVE</div>
                        </div>
                    </div>
                </div>
            """)
            
            # Input controls
            with gr.Row():
                text_input = gr.Textbox(
                    label="Command Input",
                    placeholder="Enter text to translate...",
                    elem_classes=["jarvis-textbox"],
                    lines=3
                )
            
            with gr.Row():
                src_lang = gr.Dropdown(
                    choices=list(translator.language_codes.keys()),
                    value="English",
                    label="Source Language",
                    elem_classes=["jarvis-textbox"]
                )
                tgt_lang = gr.Dropdown(
                    choices=list(translator.language_codes.keys()),
                    value="Spanish",
                    label="Target Language",
                    elem_classes=["jarvis-textbox"]
                )
            
            translate_btn = gr.Button(
                "▶ EXECUTE TRANSLATION",
                elem_classes=["jarvis-button"]
            )
            
            # Output audio
            audio_output = gr.Audio(
                label="Translated Speech",
                type="numpy"
            )
            
            # Status boxes
            with gr.Row():
                with gr.Column():
                    gr.Markdown(
                        """
                        <div class="status-box">
                            SYSTEM STATUS<br>
                            <strong>ACTIVE</strong>
                        </div>
                        """
                    )
                with gr.Column():
                    gr.Markdown(
                        """
                        <div class="status-box">
                            AUDIO SYSTEM<br>
                            <strong>READY</strong>
                        </div>
                        """
                    )
                with gr.Column():
                    gr.Markdown(
                        """
                        <div class="status-box">
                            TRANSLATION<br>
                            <strong>ONLINE</strong>
                        </div>
                        """
                    )
            
            # Event handler
            translate_btn.click(
                fn=translate_text,
                inputs=[text_input, src_lang, tgt_lang],
                outputs=audio_output
            )
    
    return demo

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