Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,8 @@ import torchaudio
|
|
| 4 |
import numpy as np
|
| 5 |
from transformers import AutoProcessor, SeamlessM4Tv2Model
|
| 6 |
from datetime import datetime
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
class
|
| 10 |
def __init__(self, model_name: str = "facebook/seamless-m4t-v2-large"):
|
| 11 |
self.processor = AutoProcessor.from_pretrained(model_name)
|
| 12 |
self.model = SeamlessM4Tv2Model.from_pretrained(model_name)
|
|
@@ -25,131 +24,201 @@ class JarvisTranslator:
|
|
| 25 |
}
|
| 26 |
|
| 27 |
def translate(self, text: str, src_lang: str, tgt_lang: str) -> tuple[int, np.ndarray]:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
border-radius: 10px;
|
| 41 |
-
border: 2px solid #00ffff;
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
#status-circle {
|
| 45 |
-
width: 150px;
|
| 46 |
-
height: 150px;
|
| 47 |
-
border: 4px solid #00ffff;
|
| 48 |
-
border-radius: 50%;
|
| 49 |
-
margin: 20px auto;
|
| 50 |
-
position: relative;
|
| 51 |
-
animation: pulse 2s infinite;
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
@keyframes pulse {
|
| 55 |
-
0% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0.4); }
|
| 56 |
-
70% { box-shadow: 0 0 0 20px rgba(0, 255, 255, 0); }
|
| 57 |
-
100% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0); }
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
.custom-button {
|
| 61 |
-
background-color: transparent !important;
|
| 62 |
-
border: 2px solid #00ffff !important;
|
| 63 |
-
color: #00ffff !important;
|
| 64 |
-
font-family: 'Courier New', monospace !important;
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
.custom-button:hover {
|
| 68 |
-
background-color: rgba(0, 255, 255, 0.1) !important;
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
.status-text {
|
| 72 |
-
color: #00ffff;
|
| 73 |
-
text-align: center;
|
| 74 |
-
font-size: 1.2em;
|
| 75 |
-
margin: 10px 0;
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
.time-display {
|
| 79 |
-
position: absolute;
|
| 80 |
-
top: 10px;
|
| 81 |
-
right: 10px;
|
| 82 |
-
color: #00ffff;
|
| 83 |
-
font-family: 'Courier New', monospace;
|
| 84 |
-
}
|
| 85 |
-
"""
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
return None, f"Error: {str(e)}"
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
text_input = gr.Textbox(
|
| 113 |
label="Command Input",
|
| 114 |
placeholder="Enter text to translate...",
|
|
|
|
| 115 |
lines=3
|
| 116 |
)
|
| 117 |
-
|
| 118 |
with gr.Row():
|
| 119 |
src_lang = gr.Dropdown(
|
| 120 |
choices=list(translator.language_codes.keys()),
|
| 121 |
value="English",
|
| 122 |
-
label="Source Language"
|
|
|
|
| 123 |
)
|
| 124 |
tgt_lang = gr.Dropdown(
|
| 125 |
choices=list(translator.language_codes.keys()),
|
| 126 |
value="Spanish",
|
| 127 |
-
label="Target Language"
|
|
|
|
| 128 |
)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
| 133 |
audio_output = gr.Audio(
|
| 134 |
-
label="Translated
|
| 135 |
type="numpy"
|
| 136 |
)
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
)
|
| 150 |
-
|
| 151 |
return demo
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo =
|
|
|
|
| 155 |
demo.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from transformers import AutoProcessor, SeamlessM4Tv2Model
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
+
class SeamlessTranslator:
|
| 9 |
def __init__(self, model_name: str = "facebook/seamless-m4t-v2-large"):
|
| 10 |
self.processor = AutoProcessor.from_pretrained(model_name)
|
| 11 |
self.model = SeamlessM4Tv2Model.from_pretrained(model_name)
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
def translate(self, text: str, src_lang: str, tgt_lang: str) -> tuple[int, np.ndarray]:
|
| 27 |
+
try:
|
| 28 |
+
inputs = self.processor(text=text, src_lang=self.language_codes[src_lang], return_tensors="pt")
|
| 29 |
+
audio_array = self.model.generate(**inputs, tgt_lang=self.language_codes[tgt_lang])[0].cpu().numpy().squeeze()
|
| 30 |
+
return self.sample_rate, audio_array
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise gr.Error(f"Translation failed: {str(e)}")
|
| 33 |
|
| 34 |
+
# Custom CSS for Jarvis theme
|
| 35 |
+
css = """
|
| 36 |
+
#jarvis-interface {
|
| 37 |
+
background-color: black !important;
|
| 38 |
+
background-image: radial-gradient(circle at center, #00303030 0%, #00000080 100%);
|
| 39 |
+
min-height: 100vh;
|
| 40 |
+
font-family: 'Courier New', monospace;
|
| 41 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
#status-ring {
|
| 44 |
+
width: 300px;
|
| 45 |
+
height: 300px;
|
| 46 |
+
border: 4px solid #00ffff;
|
| 47 |
+
border-radius: 50%;
|
| 48 |
+
margin: 20px auto;
|
| 49 |
+
position: relative;
|
| 50 |
+
animation: pulse 2s infinite;
|
| 51 |
+
display: flex;
|
| 52 |
+
align-items: center;
|
| 53 |
+
justify-content: center;
|
| 54 |
+
}
|
| 55 |
|
| 56 |
+
@keyframes pulse {
|
| 57 |
+
0% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0.4); }
|
| 58 |
+
70% { box-shadow: 0 0 0 20px rgba(0, 255, 255, 0); }
|
| 59 |
+
100% { box-shadow: 0 0 0 0 rgba(0, 255, 255, 0); }
|
| 60 |
+
}
|
| 61 |
|
| 62 |
+
#inner-ring {
|
| 63 |
+
width: 200px;
|
| 64 |
+
height: 200px;
|
| 65 |
+
border: 2px solid #00ffff;
|
| 66 |
+
border-radius: 50%;
|
| 67 |
+
display: flex;
|
| 68 |
+
align-items: center;
|
| 69 |
+
justify-content: center;
|
| 70 |
+
}
|
|
|
|
| 71 |
|
| 72 |
+
#core {
|
| 73 |
+
width: 100px;
|
| 74 |
+
height: 100px;
|
| 75 |
+
border: 3px solid #00ffff;
|
| 76 |
+
border-radius: 50%;
|
| 77 |
+
background-color: black;
|
| 78 |
+
display: flex;
|
| 79 |
+
align-items: center;
|
| 80 |
+
justify-content: center;
|
| 81 |
+
color: #00ffff;
|
| 82 |
+
text-align: center;
|
| 83 |
+
padding: 10px;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.jarvis-textbox {
|
| 87 |
+
background-color: black !important;
|
| 88 |
+
border: 2px solid #00ffff !important;
|
| 89 |
+
color: #00ffff !important;
|
| 90 |
+
font-family: 'Courier New', monospace !important;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.jarvis-button {
|
| 94 |
+
background-color: transparent !important;
|
| 95 |
+
border: 2px solid #00ffff !important;
|
| 96 |
+
color: #00ffff !important;
|
| 97 |
+
font-family: 'Courier New', monospace !important;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.jarvis-button:hover {
|
| 101 |
+
background-color: rgba(0, 255, 255, 0.1) !important;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
.status-box {
|
| 105 |
+
background-color: black !important;
|
| 106 |
+
border: 2px solid #00ffff !important;
|
| 107 |
+
color: #00ffff !important;
|
| 108 |
+
padding: 10px !important;
|
| 109 |
+
border-radius: 5px !important;
|
| 110 |
+
margin: 5px !important;
|
| 111 |
+
text-align: center !important;
|
| 112 |
+
}
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
def create_interface():
|
| 116 |
+
translator = SeamlessTranslator()
|
| 117 |
+
|
| 118 |
+
def translate_text(text: str, src_lang: str, tgt_lang: str, progress=gr.Progress()):
|
| 119 |
+
progress(0, desc="Initializing...")
|
| 120 |
+
progress(0.3, desc="Processing text...")
|
| 121 |
+
sample_rate, audio = translator.translate(text, src_lang, tgt_lang)
|
| 122 |
+
progress(0.7, desc="Generating audio...")
|
| 123 |
+
progress(1.0, desc="Complete!")
|
| 124 |
+
return audio
|
| 125 |
+
|
| 126 |
+
with gr.Blocks(css=css, title="J.A.R.V.I.S Translator") as demo:
|
| 127 |
+
gr.Markdown(
|
| 128 |
+
"""
|
| 129 |
+
# J.A.R.V.I.S TRANSLATION SYSTEM
|
| 130 |
+
### Powered by SeamlessM4T
|
| 131 |
+
"""
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Jarvis interface container
|
| 135 |
+
with gr.Column(elem_id="jarvis-interface"):
|
| 136 |
+
# Status Ring
|
| 137 |
+
gr.HTML("""
|
| 138 |
+
<div id="status-ring">
|
| 139 |
+
<div id="inner-ring">
|
| 140 |
+
<div id="core">
|
| 141 |
+
<div>JARVIS</div>
|
| 142 |
+
<div>ACTIVE</div>
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
</div>
|
| 146 |
+
""")
|
| 147 |
|
| 148 |
+
# Input controls
|
| 149 |
with gr.Row():
|
| 150 |
text_input = gr.Textbox(
|
| 151 |
label="Command Input",
|
| 152 |
placeholder="Enter text to translate...",
|
| 153 |
+
elem_classes=["jarvis-textbox"],
|
| 154 |
lines=3
|
| 155 |
)
|
| 156 |
+
|
| 157 |
with gr.Row():
|
| 158 |
src_lang = gr.Dropdown(
|
| 159 |
choices=list(translator.language_codes.keys()),
|
| 160 |
value="English",
|
| 161 |
+
label="Source Language",
|
| 162 |
+
elem_classes=["jarvis-textbox"]
|
| 163 |
)
|
| 164 |
tgt_lang = gr.Dropdown(
|
| 165 |
choices=list(translator.language_codes.keys()),
|
| 166 |
value="Spanish",
|
| 167 |
+
label="Target Language",
|
| 168 |
+
elem_classes=["jarvis-textbox"]
|
| 169 |
)
|
| 170 |
+
|
| 171 |
+
translate_btn = gr.Button(
|
| 172 |
+
"▶ EXECUTE TRANSLATION",
|
| 173 |
+
elem_classes=["jarvis-button"]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Output audio
|
| 177 |
audio_output = gr.Audio(
|
| 178 |
+
label="Translated Speech",
|
| 179 |
type="numpy"
|
| 180 |
)
|
| 181 |
|
| 182 |
+
# Status boxes
|
| 183 |
+
with gr.Row():
|
| 184 |
+
with gr.Column():
|
| 185 |
+
gr.Markdown(
|
| 186 |
+
"""
|
| 187 |
+
<div class="status-box">
|
| 188 |
+
SYSTEM STATUS<br>
|
| 189 |
+
<strong>ACTIVE</strong>
|
| 190 |
+
</div>
|
| 191 |
+
"""
|
| 192 |
+
)
|
| 193 |
+
with gr.Column():
|
| 194 |
+
gr.Markdown(
|
| 195 |
+
"""
|
| 196 |
+
<div class="status-box">
|
| 197 |
+
AUDIO SYSTEM<br>
|
| 198 |
+
<strong>READY</strong>
|
| 199 |
+
</div>
|
| 200 |
+
"""
|
| 201 |
+
)
|
| 202 |
+
with gr.Column():
|
| 203 |
+
gr.Markdown(
|
| 204 |
+
"""
|
| 205 |
+
<div class="status-box">
|
| 206 |
+
TRANSLATION<br>
|
| 207 |
+
<strong>ONLINE</strong>
|
| 208 |
+
</div>
|
| 209 |
+
"""
|
| 210 |
+
)
|
| 211 |
|
| 212 |
+
# Event handler
|
| 213 |
+
translate_btn.click(
|
| 214 |
+
fn=translate_text,
|
| 215 |
+
inputs=[text_input, src_lang, tgt_lang],
|
| 216 |
+
outputs=audio_output
|
| 217 |
)
|
| 218 |
+
|
| 219 |
return demo
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
+
demo = create_interface()
|
| 223 |
+
demo.queue()
|
| 224 |
demo.launch()
|