Spaces:
Sleeping
Sleeping
atualizar app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from functools import lru_cache
|
|
| 5 |
import markdown
|
| 6 |
|
| 7 |
# --- Configurações do Modelo --- #
|
| 8 |
-
MODEL_NAME = "bigcode/starcoder2-7b"
|
| 9 |
LANGUAGES = {
|
| 10 |
"python": {"ext": "py", "icon": "🐍"},
|
| 11 |
"javascript": {"ext": "js", "icon": "📜"},
|
|
@@ -31,42 +31,34 @@ def load_model():
|
|
| 31 |
except Exception as e:
|
| 32 |
raise RuntimeError(f"Falha ao carregar modelo: {str(e)}")
|
| 33 |
|
| 34 |
-
# --- Geração de Código --- #
|
| 35 |
-
def generate_response(
|
| 36 |
try:
|
| 37 |
model = load_model()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
Gere apenas código {language} funcional e bem documentado.
|
| 42 |
-
Requisitos: {prompt}"""
|
| 43 |
|
| 44 |
start_time = time.time()
|
| 45 |
output = model(
|
| 46 |
-
|
| 47 |
max_length=1024,
|
| 48 |
temperature=0.3,
|
| 49 |
do_sample=True
|
| 50 |
)[0]["generated_text"]
|
| 51 |
|
| 52 |
-
# Pós-processamento
|
| 53 |
code = output.split("```")[1][len(language)+1:] if "```" in output else output
|
| 54 |
exec_time = time.time() - start_time
|
| 55 |
|
| 56 |
-
|
| 57 |
-
response = f"""```{language}
|
| 58 |
{code.strip()}
|
| 59 |
|
| 60 |
⏱️ Gerado em {exec_time:.2f}s | {LANGUAGES[language]['icon']} {language.upper()}"""
|
| 61 |
-
chat_history.append((prompt, response))
|
| 62 |
-
return "", chat_history
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
-
|
| 66 |
-
chat_history.append((prompt, error_msg))
|
| 67 |
-
return "", chat_history
|
| 68 |
|
| 69 |
-
# --- Interface --- #
|
| 70 |
with gr.Blocks(
|
| 71 |
theme=gr.themes.Soft(
|
| 72 |
primary_hue="blue",
|
|
@@ -74,7 +66,7 @@ with gr.Blocks(
|
|
| 74 |
font=[gr.themes.GoogleFont("Inter")]
|
| 75 |
),
|
| 76 |
css="""
|
| 77 |
-
.chatbot { font-family: 'Inter', sans-serif;
|
| 78 |
.code { max-height: 500px; overflow-y: auto !important; }
|
| 79 |
"""
|
| 80 |
) as app:
|
|
@@ -89,45 +81,35 @@ with gr.Blocks(
|
|
| 89 |
label="Linguagem de Programação"
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
lines=3
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
with gr.Row():
|
| 104 |
-
submit_btn = gr.Button("Enviar", variant="primary")
|
| 105 |
-
clear_btn = gr.Button("Limpar")
|
| 106 |
|
| 107 |
with gr.Column(scale=2):
|
| 108 |
gr.Markdown("### 📚 Capacidades do Assistente:")
|
| 109 |
gr.Markdown("""
|
| 110 |
-
- Gera código em **
|
| 111 |
-
- Suporta estruturas complexas (
|
| 112 |
-
- Documentação automática
|
| 113 |
-
- Sintaxe
|
| 114 |
-
- Exemplos práticos e testáveis
|
| 115 |
""")
|
| 116 |
|
| 117 |
gr.Examples(
|
| 118 |
examples=[
|
| 119 |
-
["
|
| 120 |
-
["
|
| 121 |
-
["Classe para conexão
|
| 122 |
],
|
| 123 |
-
inputs=[
|
| 124 |
-
label="
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
# Eventos
|
| 128 |
-
msg.submit(generate_response, [msg, language, chatbot], [msg, chatbot])
|
| 129 |
-
submit_btn.click(generate_response, [msg, language, chatbot], [msg, chatbot])
|
| 130 |
-
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
| 5 |
import markdown
|
| 6 |
|
| 7 |
# --- Configurações do Modelo --- #
|
| 8 |
+
MODEL_NAME = "bigcode/starcoder2-7b"
|
| 9 |
LANGUAGES = {
|
| 10 |
"python": {"ext": "py", "icon": "🐍"},
|
| 11 |
"javascript": {"ext": "js", "icon": "📜"},
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
raise RuntimeError(f"Falha ao carregar modelo: {str(e)}")
|
| 33 |
|
| 34 |
+
# --- Geração de Código (Formato OpenAI) --- #
|
| 35 |
+
def generate_response(message, history, language):
|
| 36 |
try:
|
| 37 |
model = load_model()
|
| 38 |
+
prompt = f"""Você é um assistente de programação especializado em {language}.
|
| 39 |
+
Gere código {language} funcional e bem documentado para:
|
| 40 |
+
{message}"""
|
|
|
|
|
|
|
| 41 |
|
| 42 |
start_time = time.time()
|
| 43 |
output = model(
|
| 44 |
+
prompt,
|
| 45 |
max_length=1024,
|
| 46 |
temperature=0.3,
|
| 47 |
do_sample=True
|
| 48 |
)[0]["generated_text"]
|
| 49 |
|
|
|
|
| 50 |
code = output.split("```")[1][len(language)+1:] if "```" in output else output
|
| 51 |
exec_time = time.time() - start_time
|
| 52 |
|
| 53 |
+
return f"""```{language}
|
|
|
|
| 54 |
{code.strip()}
|
| 55 |
|
| 56 |
⏱️ Gerado em {exec_time:.2f}s | {LANGUAGES[language]['icon']} {language.upper()}"""
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
return f"❌ Erro: {str(e)}"
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# --- Interface Atualizada --- #
|
| 62 |
with gr.Blocks(
|
| 63 |
theme=gr.themes.Soft(
|
| 64 |
primary_hue="blue",
|
|
|
|
| 66 |
font=[gr.themes.GoogleFont("Inter")]
|
| 67 |
),
|
| 68 |
css="""
|
| 69 |
+
.chatbot { font-family: 'Inter', sans-serif; height: 500px; }
|
| 70 |
.code { max-height: 500px; overflow-y: auto !important; }
|
| 71 |
"""
|
| 72 |
) as app:
|
|
|
|
| 81 |
label="Linguagem de Programação"
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Chatbot no novo formato
|
| 85 |
+
chatbot = gr.ChatInterface(
|
| 86 |
+
fn=lambda msg, history: generate_response(msg, history, language.value),
|
| 87 |
+
textbox=gr.Textbox(
|
| 88 |
+
placeholder="Descreva o código que precisa (ex: 'função para calcular IMC')",
|
| 89 |
+
lines=3
|
| 90 |
+
),
|
| 91 |
+
additional_inputs=[language]
|
|
|
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
gr.Markdown("### 📚 Capacidades do Assistente:")
|
| 96 |
gr.Markdown("""
|
| 97 |
+
- Gera código em **6 linguagens** diferentes
|
| 98 |
+
- Suporta estruturas complexas (OOP, APIs)
|
| 99 |
+
- Documentação automática incluída
|
| 100 |
+
- Sintaxe otimizada para cada linguagem
|
|
|
|
| 101 |
""")
|
| 102 |
|
| 103 |
gr.Examples(
|
| 104 |
examples=[
|
| 105 |
+
["Função Python para calcular Fibonacci", "python"],
|
| 106 |
+
["Componente React em JSX", "javascript"],
|
| 107 |
+
["Classe Java para conexão JDBC", "java"]
|
| 108 |
],
|
| 109 |
+
inputs=[chatbot.textbox, language],
|
| 110 |
+
label="💡 Exemplos Prontos"
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# --- Config Compatível --- #
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|