Update app.py
Browse files
app.py
CHANGED
@@ -62,9 +62,9 @@ def models(text, model="Llama 3 8B Service", seed=42):
|
|
62 |
global conversation_history
|
63 |
seed = int(randomize_seed_fn(seed))
|
64 |
generator = torch.Generator().manual_seed(seed)
|
65 |
-
|
66 |
client = client_fn(model)
|
67 |
-
|
68 |
if "Llama 3 8B Service" in model:
|
69 |
messages = [
|
70 |
{"role": "system", "content": system_instructions1},
|
@@ -76,21 +76,21 @@ def models(text, model="Llama 3 8B Service", seed=42):
|
|
76 |
messages=messages
|
77 |
)
|
78 |
assistant_response = completion.choices[0].message.content
|
79 |
-
|
80 |
# Update conversation history
|
81 |
conversation_history.append({"role": "user", "content": text})
|
82 |
conversation_history.append({"role": "assistant", "content": assistant_response})
|
83 |
-
|
84 |
# Keep only the last 10 messages to avoid token limit issues
|
85 |
if len(conversation_history) > 20:
|
86 |
conversation_history = conversation_history[-20:]
|
87 |
-
|
88 |
return assistant_response
|
89 |
else:
|
90 |
# For other models, we'll concatenate the conversation history into a single string
|
91 |
history_text = "\n".join([f"{'User' if msg['role'] == 'user' else 'Assistant'}: {msg['content']}" for msg in conversation_history])
|
92 |
formatted_prompt = f"{system_instructions1}\n\nConversation history:\n{history_text}\n\nUser: {text}\nOPTIMUS:"
|
93 |
-
|
94 |
generate_kwargs = dict(
|
95 |
max_new_tokens=300,
|
96 |
seed=seed
|
@@ -101,15 +101,15 @@ def models(text, model="Llama 3 8B Service", seed=42):
|
|
101 |
for response in stream:
|
102 |
if not response.token.text == "</s>":
|
103 |
output += response.token.text
|
104 |
-
|
105 |
# Update conversation history
|
106 |
conversation_history.append({"role": "user", "content": text})
|
107 |
conversation_history.append({"role": "assistant", "content": output})
|
108 |
-
|
109 |
# Keep only the last 10 messages to avoid token limit issues
|
110 |
if len(conversation_history) > 20:
|
111 |
conversation_history = conversation_history[-20:]
|
112 |
-
|
113 |
return output
|
114 |
|
115 |
async def respond(audio, model, seed):
|
@@ -141,10 +141,10 @@ def translate_speech(audio_file, target_language):
|
|
141 |
"""
|
142 |
if audio_file is None:
|
143 |
return None
|
144 |
-
|
145 |
language_code = LANGUAGE_CODES[target_language]
|
146 |
output_file = "translated_audio.wav"
|
147 |
-
|
148 |
command = [
|
149 |
"expressivity_predict",
|
150 |
audio_file,
|
@@ -154,7 +154,7 @@ def translate_speech(audio_file, target_language):
|
|
154 |
"--gated-model-dir", "models",
|
155 |
"--output_path", output_file
|
156 |
]
|
157 |
-
|
158 |
subprocess.run(command, check=True)
|
159 |
|
160 |
if os.path.exists(output_file):
|
@@ -177,7 +177,7 @@ def speech_translation_tab():
|
|
177 |
|
178 |
with gr.Blocks(css="style.css") as demo:
|
179 |
description = gr.Markdown("# <center><b>Hello, I am Optimus Prime your personal AI voice assistant</b></center>")
|
180 |
-
|
181 |
with gr.Tabs() as tabs:
|
182 |
with gr.TabItem("Voice Assistant") as voice_assistant:
|
183 |
select = gr.Dropdown([
|
@@ -198,7 +198,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
198 |
value=0,
|
199 |
visible=False
|
200 |
)
|
201 |
-
input = gr.Audio(label="User", sources=["microphone"], type="filepath"
|
202 |
output = gr.Audio(label="AI", type="filepath",
|
203 |
interactive=False,
|
204 |
autoplay=True,
|
@@ -210,7 +210,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
210 |
outputs=[output],
|
211 |
live=True
|
212 |
)
|
213 |
-
|
214 |
with gr.TabItem("Speech Translation") as speech_translation:
|
215 |
input_audio = gr.Audio(label="User", sources=["microphone"], type="filepath")
|
216 |
target_lang = gr.Dropdown(
|
@@ -222,7 +222,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
222 |
interactive=False,
|
223 |
autoplay=True,
|
224 |
elem_classes="audio")
|
225 |
-
|
226 |
gr.Interface(
|
227 |
fn=translate_speech,
|
228 |
inputs=[input_audio, target_lang],
|
@@ -233,5 +233,5 @@ with gr.Blocks(css="style.css") as demo:
|
|
233 |
voice_assistant.select(fn=voice_assistant_tab, inputs=None, outputs=description)
|
234 |
speech_translation.select(fn=speech_translation_tab, inputs=None, outputs=description)
|
235 |
|
236 |
-
if
|
237 |
demo.queue(max_size=200).launch()
|
|
|
62 |
global conversation_history
|
63 |
seed = int(randomize_seed_fn(seed))
|
64 |
generator = torch.Generator().manual_seed(seed)
|
65 |
+
|
66 |
client = client_fn(model)
|
67 |
+
|
68 |
if "Llama 3 8B Service" in model:
|
69 |
messages = [
|
70 |
{"role": "system", "content": system_instructions1},
|
|
|
76 |
messages=messages
|
77 |
)
|
78 |
assistant_response = completion.choices[0].message.content
|
79 |
+
|
80 |
# Update conversation history
|
81 |
conversation_history.append({"role": "user", "content": text})
|
82 |
conversation_history.append({"role": "assistant", "content": assistant_response})
|
83 |
+
|
84 |
# Keep only the last 10 messages to avoid token limit issues
|
85 |
if len(conversation_history) > 20:
|
86 |
conversation_history = conversation_history[-20:]
|
87 |
+
|
88 |
return assistant_response
|
89 |
else:
|
90 |
# For other models, we'll concatenate the conversation history into a single string
|
91 |
history_text = "\n".join([f"{'User' if msg['role'] == 'user' else 'Assistant'}: {msg['content']}" for msg in conversation_history])
|
92 |
formatted_prompt = f"{system_instructions1}\n\nConversation history:\n{history_text}\n\nUser: {text}\nOPTIMUS:"
|
93 |
+
|
94 |
generate_kwargs = dict(
|
95 |
max_new_tokens=300,
|
96 |
seed=seed
|
|
|
101 |
for response in stream:
|
102 |
if not response.token.text == "</s>":
|
103 |
output += response.token.text
|
104 |
+
|
105 |
# Update conversation history
|
106 |
conversation_history.append({"role": "user", "content": text})
|
107 |
conversation_history.append({"role": "assistant", "content": output})
|
108 |
+
|
109 |
# Keep only the last 10 messages to avoid token limit issues
|
110 |
if len(conversation_history) > 20:
|
111 |
conversation_history = conversation_history[-20:]
|
112 |
+
|
113 |
return output
|
114 |
|
115 |
async def respond(audio, model, seed):
|
|
|
141 |
"""
|
142 |
if audio_file is None:
|
143 |
return None
|
144 |
+
|
145 |
language_code = LANGUAGE_CODES[target_language]
|
146 |
output_file = "translated_audio.wav"
|
147 |
+
|
148 |
command = [
|
149 |
"expressivity_predict",
|
150 |
audio_file,
|
|
|
154 |
"--gated-model-dir", "models",
|
155 |
"--output_path", output_file
|
156 |
]
|
157 |
+
|
158 |
subprocess.run(command, check=True)
|
159 |
|
160 |
if os.path.exists(output_file):
|
|
|
177 |
|
178 |
with gr.Blocks(css="style.css") as demo:
|
179 |
description = gr.Markdown("# <center><b>Hello, I am Optimus Prime your personal AI voice assistant</b></center>")
|
180 |
+
|
181 |
with gr.Tabs() as tabs:
|
182 |
with gr.TabItem("Voice Assistant") as voice_assistant:
|
183 |
select = gr.Dropdown([
|
|
|
198 |
value=0,
|
199 |
visible=False
|
200 |
)
|
201 |
+
input = gr.Audio(label="User", sources=["microphone"], type="filepath")
|
202 |
output = gr.Audio(label="AI", type="filepath",
|
203 |
interactive=False,
|
204 |
autoplay=True,
|
|
|
210 |
outputs=[output],
|
211 |
live=True
|
212 |
)
|
213 |
+
|
214 |
with gr.TabItem("Speech Translation") as speech_translation:
|
215 |
input_audio = gr.Audio(label="User", sources=["microphone"], type="filepath")
|
216 |
target_lang = gr.Dropdown(
|
|
|
222 |
interactive=False,
|
223 |
autoplay=True,
|
224 |
elem_classes="audio")
|
225 |
+
|
226 |
gr.Interface(
|
227 |
fn=translate_speech,
|
228 |
inputs=[input_audio, target_lang],
|
|
|
233 |
voice_assistant.select(fn=voice_assistant_tab, inputs=None, outputs=description)
|
234 |
speech_translation.select(fn=speech_translation_tab, inputs=None, outputs=description)
|
235 |
|
236 |
+
if name == "main":
|
237 |
demo.queue(max_size=200).launch()
|