Spaces:
Sleeping
Sleeping
Commit
·
b20d67c
1
Parent(s):
cbb5596
Add natural language translation layer
Browse files- app.py +21 -4
- gensim/utils.py +40 -0
app.py
CHANGED
|
@@ -22,7 +22,7 @@ from gensim.agent import Agent
|
|
| 22 |
from gensim.critic import Critic
|
| 23 |
from gensim.sim_runner import SimulationRunner
|
| 24 |
from gensim.memory import Memory
|
| 25 |
-
from gensim.utils import set_gpt_model, clear_messages
|
| 26 |
|
| 27 |
|
| 28 |
class DemoRunner:
|
|
@@ -64,12 +64,21 @@ class DemoRunner:
|
|
| 64 |
set_gpt_model(cfg['gpt_model'])
|
| 65 |
cfg['load_memory'] = True
|
| 66 |
cfg['use_template'] = True
|
| 67 |
-
|
| 68 |
cfg['task_description_candidate_num'] = 10
|
| 69 |
cfg['record']['save_video'] = True
|
| 70 |
|
| 71 |
print("cfg = ", cfg)
|
| 72 |
memory = Memory(cfg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
agent = Agent(cfg, memory)
|
| 74 |
critic = Critic(cfg, memory)
|
| 75 |
self.simulation_runner = SimulationRunner(cfg, agent, critic, memory)
|
|
@@ -80,7 +89,15 @@ class DemoRunner:
|
|
| 80 |
|
| 81 |
def run(self, instruction, progress):
|
| 82 |
cfg = self._cfg
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# self._env.cache_video = []
|
| 86 |
self.simulation_runner._md_logger = ''
|
|
@@ -154,7 +171,7 @@ if __name__ == '__main__':
|
|
| 154 |
|
| 155 |
model_choice = gr.Radio(["gpt-3.5-turbo-16k", "gpt-4"], label="Qual modelo?", interactive=True)
|
| 156 |
option_choice = gr.Radio(["top-down"], label="Qual modo?", interactive=True)
|
| 157 |
-
inp_instruction = gr.Textbox(label='Nome da tarefa alvo (
|
| 158 |
info_setup = gr.Markdown(label='Status da configuração')
|
| 159 |
btn_setup = gr.Button("Configurar/Resetar simulação")
|
| 160 |
btn_run = gr.Button("Executar (pode levar 30+ segundos)")
|
|
|
|
| 22 |
from gensim.critic import Critic
|
| 23 |
from gensim.sim_runner import SimulationRunner
|
| 24 |
from gensim.memory import Memory
|
| 25 |
+
from gensim.utils import set_gpt_model, clear_messages, translate_natural_language_to_task_name
|
| 26 |
|
| 27 |
|
| 28 |
class DemoRunner:
|
|
|
|
| 64 |
set_gpt_model(cfg['gpt_model'])
|
| 65 |
cfg['load_memory'] = True
|
| 66 |
cfg['use_template'] = True
|
| 67 |
+
|
| 68 |
cfg['task_description_candidate_num'] = 10
|
| 69 |
cfg['record']['save_video'] = True
|
| 70 |
|
| 71 |
print("cfg = ", cfg)
|
| 72 |
memory = Memory(cfg)
|
| 73 |
+
|
| 74 |
+
# Detectar se é linguagem natural e traduzir se necessário
|
| 75 |
+
if target_task_name and (' ' in target_task_name or any(c.isupper() for c in target_task_name) or not target_task_name.replace('-', '').replace('_', '').isalnum()):
|
| 76 |
+
print(f"Detectado linguagem natural: '{target_task_name}' - traduzindo...")
|
| 77 |
+
translated_name = translate_natural_language_to_task_name(target_task_name, interaction_txt=memory.chat_log)
|
| 78 |
+
print(f"Traduzido para: '{translated_name}'")
|
| 79 |
+
cfg['target_task_name'] = translated_name
|
| 80 |
+
else:
|
| 81 |
+
cfg['target_task_name'] = target_task_name
|
| 82 |
agent = Agent(cfg, memory)
|
| 83 |
critic = Critic(cfg, memory)
|
| 84 |
self.simulation_runner = SimulationRunner(cfg, agent, critic, memory)
|
|
|
|
| 89 |
|
| 90 |
def run(self, instruction, progress):
|
| 91 |
cfg = self._cfg
|
| 92 |
+
|
| 93 |
+
# Detectar se é linguagem natural e traduzir se necessário
|
| 94 |
+
if instruction and (' ' in instruction or any(c.isupper() for c in instruction) or not instruction.replace('-', '').replace('_', '').isalnum()):
|
| 95 |
+
print(f"Detectado linguagem natural: '{instruction}' - traduzindo...")
|
| 96 |
+
translated_name = translate_natural_language_to_task_name(instruction, interaction_txt=self.simulation_runner.chat_log)
|
| 97 |
+
print(f"Traduzido para: '{translated_name}'")
|
| 98 |
+
cfg['target_task_name'] = translated_name
|
| 99 |
+
else:
|
| 100 |
+
cfg['target_task_name'] = instruction
|
| 101 |
|
| 102 |
# self._env.cache_video = []
|
| 103 |
self.simulation_runner._md_logger = ''
|
|
|
|
| 171 |
|
| 172 |
model_choice = gr.Radio(["gpt-3.5-turbo-16k", "gpt-4"], label="Qual modelo?", interactive=True)
|
| 173 |
option_choice = gr.Radio(["top-down"], label="Qual modo?", interactive=True)
|
| 174 |
+
inp_instruction = gr.Textbox(label='Nome da tarefa alvo ou descrição em linguagem natural (ex: "construa uma ponte" ou "build-bridge")', lines=1)
|
| 175 |
info_setup = gr.Markdown(label='Status da configuração')
|
| 176 |
btn_setup = gr.Button("Configurar/Resetar simulação")
|
| 177 |
btn_run = gr.Button("Executar (pode levar 30+ segundos)")
|
gensim/utils.py
CHANGED
|
@@ -351,6 +351,46 @@ def clear_messages():
|
|
| 351 |
existing_messages = []
|
| 352 |
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
def format_finetune_prompt(task_name):
|
| 355 |
instruction_text = open('prompts/finetune_instructions_prompt.txt').read()
|
| 356 |
instruction_text = instruction_text.replace("TASK_NAME_TEMPLATE", task_name)
|
|
|
|
| 351 |
existing_messages = []
|
| 352 |
|
| 353 |
|
| 354 |
+
def translate_natural_language_to_task_name(natural_language_input, interaction_txt=None):
|
| 355 |
+
"""
|
| 356 |
+
Converte linguagem natural (português ou inglês) para o formato de nome de tarefa esperado.
|
| 357 |
+
Exemplo: "construa uma ponte colorida" -> "build-colorful-bridge"
|
| 358 |
+
"""
|
| 359 |
+
prompt = f"""Convert the following task description to a task name in the format "word-word-word" (lowercase, separated by hyphens).
|
| 360 |
+
|
| 361 |
+
The task name should be concise and descriptive, following the pattern of existing tasks like:
|
| 362 |
+
- "build-bridge" (for building a bridge)
|
| 363 |
+
- "stack-blue-on-red" (for stacking blue blocks on red blocks)
|
| 364 |
+
- "align-spheres-in-colored-zones" (for aligning spheres in colored zones)
|
| 365 |
+
- "insert-sphere-into-container" (for inserting a sphere into a container)
|
| 366 |
+
|
| 367 |
+
Input (in natural language): {natural_language_input}
|
| 368 |
+
|
| 369 |
+
Return ONLY the task name in the format "word-word-word", nothing else. No explanations, no code blocks, just the task name."""
|
| 370 |
+
|
| 371 |
+
try:
|
| 372 |
+
result = generate_feedback(prompt, max_tokens=50, temperature=0.0, interaction_txt=interaction_txt, retry_max=3)
|
| 373 |
+
# Limpar o resultado (remover espaços, quebras de linha, code blocks, etc)
|
| 374 |
+
task_name = result.strip()
|
| 375 |
+
# Remover markdown code blocks se houver
|
| 376 |
+
task_name = re.sub(r'```[a-z]*\n?', '', task_name).strip()
|
| 377 |
+
# Garantir que está em lowercase e com hífens
|
| 378 |
+
task_name = task_name.lower().replace(' ', '-').replace('_', '-')
|
| 379 |
+
# Remover caracteres inválidos
|
| 380 |
+
task_name = re.sub(r'[^a-z0-9-]', '', task_name)
|
| 381 |
+
|
| 382 |
+
if interaction_txt is not None:
|
| 383 |
+
add_to_txt(interaction_txt, f"Translated '{natural_language_input}' to task name: {task_name}", with_print=True)
|
| 384 |
+
|
| 385 |
+
return task_name
|
| 386 |
+
except Exception as e:
|
| 387 |
+
print(f"Error translating natural language: {e}")
|
| 388 |
+
# Fallback: tentar converter manualmente
|
| 389 |
+
fallback = natural_language_input.lower().replace(' ', '-').replace('_', '-')
|
| 390 |
+
fallback = re.sub(r'[^a-z0-9-]', '', fallback)
|
| 391 |
+
return fallback
|
| 392 |
+
|
| 393 |
+
|
| 394 |
def format_finetune_prompt(task_name):
|
| 395 |
instruction_text = open('prompts/finetune_instructions_prompt.txt').read()
|
| 396 |
instruction_text = instruction_text.replace("TASK_NAME_TEMPLATE", task_name)
|