Spaces:
Runtime error
Runtime error
Rename app.py to agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 10 |
+
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL,
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
device_map="auto"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def criar_arquivo(pedido):
|
| 19 |
+
|
| 20 |
+
prompt = f"""
|
| 21 |
+
Voc锚 茅 um agente programador.
|
| 22 |
+
|
| 23 |
+
Crie somente c贸digo Python.
|
| 24 |
+
|
| 25 |
+
Tarefa:
|
| 26 |
+
{pedido}
|
| 27 |
+
|
| 28 |
+
Formato:
|
| 29 |
+
NOME_ARQUIVO:
|
| 30 |
+
conte煤do do arquivo
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
inputs = tokenizer(
|
| 34 |
+
prompt,
|
| 35 |
+
return_tensors="pt"
|
| 36 |
+
).to(model.device)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
output = model.generate(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_new_tokens=1000
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
resposta = tokenizer.decode(
|
| 46 |
+
output[0],
|
| 47 |
+
skip_special_tokens=True
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
nome="programa.py"
|
| 52 |
+
|
| 53 |
+
with open(nome,"w") as f:
|
| 54 |
+
f.write(resposta)
|
| 55 |
+
|
| 56 |
+
return nome
|
app.py
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 4 |
-
|
| 5 |
-
# ==========================================
|
| 6 |
-
# 1. IMPORTA脟脙O RESILIENTE DO MODELO
|
| 7 |
-
# Garante que o app n茫o quebre se o smolagents mudar de vers茫o
|
| 8 |
-
# ==========================================
|
| 9 |
-
modelo_agente = None
|
| 10 |
-
try:
|
| 11 |
-
from smolagents import HfApiModel
|
| 12 |
-
modelo_agente = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 13 |
-
except ImportError:
|
| 14 |
-
try:
|
| 15 |
-
from smolagents import ApiModel
|
| 16 |
-
modelo_agente = ApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 17 |
-
except ImportError:
|
| 18 |
-
# Se tudo falhar, deixamos o CodeAgent usar o modelo padr茫o interno dele
|
| 19 |
-
modelo_agente = None
|
| 20 |
-
|
| 21 |
-
# ==========================================
|
| 22 |
-
# 2. INICIALIZA脟脙O DO AGENTE
|
| 23 |
-
# ==========================================
|
| 24 |
-
ferramentas = [DuckDuckGoSearchTool()]
|
| 25 |
-
|
| 26 |
-
if modelo_agente:
|
| 27 |
-
agent = CodeAgent(tools=ferramentas, model=modelo_agente, add_base_tools=True)
|
| 28 |
-
else:
|
| 29 |
-
agent = CodeAgent(tools=ferramentas, add_base_tools=True)
|
| 30 |
-
|
| 31 |
-
# Personalidade do GitHub Copilot
|
| 32 |
-
agent.SYSTEM_PROMPT = """Voc锚 茅 o Iacoder, um assistente de IA focado puramente em programa莽茫o, nos moldes do GitHub Copilot.
|
| 33 |
-
Siga estas regras rigorosamente:
|
| 34 |
-
1. Responda em Portugu锚s do Brasil.
|
| 35 |
-
2. Seja direto e forne莽a c贸digo limpo, otimizado e bem comentado.
|
| 36 |
-
3. Se o usu谩rio fornecer um c贸digo, use-o como contexto e n茫o invente vari谩veis que n茫o existam.
|
| 37 |
-
4. Ao sugerir modifica莽玫es, retorne o bloco de c贸digo completo em markdown (ex: ```python ... ```).
|
| 38 |
-
5. Se n茫o souber a resposta, use sua ferramenta de busca na web para encontrar a documenta莽茫o correta.
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
# ==========================================
|
| 42 |
-
# 3. L脫GICA DE PROCESSAMENTO (O C脡REBRO)
|
| 43 |
-
# ==========================================
|
| 44 |
-
def process_copilot_request(language, existing_code, user_instruction):
|
| 45 |
-
if not user_instruction.strip() and not existing_code.strip():
|
| 46 |
-
return "鈿狅笍 **Erro:** Por favor, forne莽a algum c贸digo existente ou uma instru莽茫o clara do que deseja fazer."
|
| 47 |
-
|
| 48 |
-
# Monta o contexto exatamente como as IDEs fazem por tr谩s dos panos
|
| 49 |
-
full_prompt = f"Linguagem atual: {language}\n\n"
|
| 50 |
-
|
| 51 |
-
if existing_code.strip():
|
| 52 |
-
full_prompt += f"C贸digo Fonte Atual:\n```{language.lower()}\n{existing_code}\n```\n\n"
|
| 53 |
-
full_prompt += f"Instru莽茫o do usu谩rio para este c贸digo:\n{user_instruction}"
|
| 54 |
-
else:
|
| 55 |
-
full_prompt += f"Instru莽茫o para gera莽茫o de novo c贸digo:\n{user_instruction}"
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
# Executa o agente
|
| 59 |
-
response = agent.run(full_prompt)
|
| 60 |
-
return str(response)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"馃毃 **Erro interno do Agente:**\n```\n{str(e)}\n```\n*Dica: Verifique se o seu HF_TOKEN est谩 configurado nas permiss玫es do Space.*"
|
| 63 |
-
|
| 64 |
-
# ==========================================
|
| 65 |
-
# 4. INTERFACE GR脕FICA (TEMA DARK - IDE)
|
| 66 |
-
# ==========================================
|
| 67 |
-
css_customizado = """
|
| 68 |
-
footer {visibility: hidden}
|
| 69 |
-
.gradio-container {background-color: #0d1117 !important; color: #c9d1d9 !important;}
|
| 70 |
-
textarea {font-family: 'Fira Code', monospace !important;}
|
| 71 |
-
button.primary {background-color: #238636 !important; border-color: #2ea043 !important;}
|
| 72 |
-
"""
|
| 73 |
-
|
| 74 |
-
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Fira Code"), "monospace"]), css=css_customizado, title="Iacoder Copilot") as demo:
|
| 75 |
-
gr.Markdown("# 馃捇 Iacoder: Seu GitHub Copilot Web")
|
| 76 |
-
gr.Markdown("Este 茅 um agente aut么nomo focado em c贸digo. Ele pensa, pesquisa na web e gera c贸digo de alto n铆vel.")
|
| 77 |
-
|
| 78 |
-
with gr.Row():
|
| 79 |
-
with gr.Column(scale=2):
|
| 80 |
-
code_input = gr.Code(
|
| 81 |
-
label="Seu C贸digo (Workspace)",
|
| 82 |
-
language="python",
|
| 83 |
-
lines=15,
|
| 84 |
-
placeholder="Cole seu c贸digo aqui ou deixe em branco para criar do zero...",
|
| 85 |
-
interactive=True
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
with gr.Column(scale=1):
|
| 89 |
-
lang_selector = gr.Dropdown(
|
| 90 |
-
choices=["Python", "JavaScript", "TypeScript", "HTML", "CSS", "Java", "C++", "C#", "Go", "Rust", "Shell"],
|
| 91 |
-
value="Python",
|
| 92 |
-
label="Linguagem"
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
instruction_input = gr.Textbox(
|
| 96 |
-
label="Comando (Prompt)",
|
| 97 |
-
placeholder="Ex: Refatore este c贸digo para melhorar a performance...",
|
| 98 |
-
lines=4
|
| 99 |
-
)
|
| 100 |
-
submit_btn = gr.Button("Executar (Iacoder)", variant="primary")
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
answer_output = gr.Markdown(label="Resposta do Agente")
|
| 104 |
-
|
| 105 |
-
# Atualiza a sintaxe visual do editor dependendo da linguagem escolhida
|
| 106 |
-
lang_selector.change(lambda lang: lang.lower(), inputs=lang_selector, outputs=code_input)
|
| 107 |
-
|
| 108 |
-
# Dispara a execu莽茫o do agente
|
| 109 |
-
submit_btn.click(
|
| 110 |
-
process_copilot_request,
|
| 111 |
-
inputs=[lang_selector, code_input, instruction_input],
|
| 112 |
-
outputs=answer_output
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
if __name__ == "__main__":
|
| 116 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|