Jaward commited on
Commit
e0fb8ee
·
verified ·
1 Parent(s): 90a64f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -43
app.py CHANGED
@@ -11,9 +11,6 @@ import random
11
  from openai import OpenAI
12
  import subprocess
13
  from starlette.requests import ClientDisconnect
14
- import hashlib
15
- from functools import lru_cache
16
-
17
 
18
  LLAMA_3B_API_ENDPOINT = os.environ.get("LLAMA_3B_API_ENDPOINT")
19
  LLAMA_3B_API_KEY = os.environ.get("LLAMA_3B_API_KEY")
@@ -123,20 +120,16 @@ def models(text, model="Llama 3 8B Service", seed=42):
123
 
124
  return output
125
 
126
- translation_cache = {}
 
 
127
 
128
- @lru_cache(maxsize=100)
129
- def cached_translate_speech(audio_file_hash, target_language):
130
- # This function will be called if the result is not in the cache
131
  language_code = LANGUAGE_CODES[target_language]
132
- output_file = f"translated_audio_{audio_file_hash[:10]}.wav"
133
-
134
- if os.path.exists(output_file):
135
- return output_file
136
 
137
  command = [
138
  "expressivity_predict",
139
- audio_file_hash, # We're using the hash as a unique identifier
140
  "--tgt_lang", language_code,
141
  "--model_name", "seamless_expressivity",
142
  "--vocoder_name", "vocoder_pretssel",
@@ -144,39 +137,15 @@ def cached_translate_speech(audio_file_hash, target_language):
144
  "--output_path", output_file
145
  ]
146
 
147
- try:
148
- subprocess.run(command, check=True, capture_output=True, text=True)
149
- if os.path.exists(output_file):
150
- print(f"File created successfully: {output_file}")
151
- return output_file
152
- else:
153
- print(f"File not found: {output_file}")
154
- return None
155
- except subprocess.CalledProcessError as e:
156
- print(f"Translation process failed: {e.stderr}")
157
- return None
158
 
159
- def translate_speech(audio_file, target_language):
160
- if audio_file is None:
 
 
 
161
  return None
162
 
163
- # Generate a hash of the audio file content
164
- with open(audio_file, "rb") as f:
165
- file_hash = hashlib.md5(f.read()).hexdigest()
166
-
167
- # Check if we have a cached result
168
- cache_key = (file_hash, target_language)
169
- if cache_key in translation_cache:
170
- return translation_cache[cache_key]
171
-
172
- # If not in cache, call the cached function
173
- result = cached_translate_speech(file_hash, target_language)
174
-
175
- # Store the result in our custom cache
176
- translation_cache[cache_key] = result
177
-
178
- return result
179
-
180
  async def respond(audio, model, seed, target_language):
181
  try:
182
  if audio is None:
@@ -212,7 +181,7 @@ def clear_history():
212
 
213
  with gr.Blocks(css="style.css") as demo:
214
  gr.Markdown("# <center><b>Optimus Prime: Your Personal AI Voice Assistant with Speech Translation</b></center>")
215
- gr.Markdown("<center><b>For speech translation, start with the phrase 'Please translate' followed by the speech you want to translate</b></center>")
216
 
217
  with gr.Row():
218
  with gr.Column(scale=1):
 
11
  from openai import OpenAI
12
  import subprocess
13
  from starlette.requests import ClientDisconnect
 
 
 
14
 
15
  LLAMA_3B_API_ENDPOINT = os.environ.get("LLAMA_3B_API_ENDPOINT")
16
  LLAMA_3B_API_KEY = os.environ.get("LLAMA_3B_API_KEY")
 
120
 
121
  return output
122
 
123
+ def translate_speech(audio_file, target_language):
124
+ if audio_file is None:
125
+ return None
126
 
 
 
 
127
  language_code = LANGUAGE_CODES[target_language]
128
+ output_file = "translated_audio.wav"
 
 
 
129
 
130
  command = [
131
  "expressivity_predict",
132
+ audio_file,
133
  "--tgt_lang", language_code,
134
  "--model_name", "seamless_expressivity",
135
  "--vocoder_name", "vocoder_pretssel",
 
137
  "--output_path", output_file
138
  ]
139
 
140
+ subprocess.run(command, check=True)
 
 
 
 
 
 
 
 
 
 
141
 
142
+ if os.path.exists(output_file):
143
+ print(f"File created successfully: {output_file}")
144
+ return output_file
145
+ else:
146
+ print(f"File not found: {output_file}")
147
  return None
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  async def respond(audio, model, seed, target_language):
150
  try:
151
  if audio is None:
 
181
 
182
  with gr.Blocks(css="style.css") as demo:
183
  gr.Markdown("# <center><b>Optimus Prime: Your Personal AI Voice Assistant with Speech Translation</b></center>")
184
+ gr.Markdown("## <center><b>For speech translation, start with the phrase 'Please translate' followed by the speech you want to translate</b></center>")
185
 
186
  with gr.Row():
187
  with gr.Column(scale=1):