Spaces:
Sleeping
Sleeping
atualize app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,16 @@ from transformers import pipeline
|
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
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": "📜"},
|
| 12 |
-
"java": {"ext": "java", "icon": "☕"},
|
| 13 |
-
"cpp": {"ext": "cpp", "icon": "🖥️"},
|
| 14 |
-
"html": {"ext": "html", "icon": "🌐"},
|
| 15 |
-
"sql": {"ext": "sql", "icon": "🗃️"}
|
| 16 |
-
}
|
| 17 |
|
| 18 |
# --- Cache do Modelo --- #
|
| 19 |
@lru_cache(maxsize=None)
|
| 20 |
def load_model():
|
| 21 |
try:
|
|
|
|
| 22 |
start_time = time.time()
|
| 23 |
model = pipeline(
|
| 24 |
"text-generation",
|
|
@@ -29,45 +22,38 @@ def load_model():
|
|
| 29 |
print(f"✅ Modelo carregado em {time.time()-start_time:.2f}s")
|
| 30 |
return model
|
| 31 |
except Exception as e:
|
| 32 |
-
raise RuntimeError(f"Falha ao carregar modelo: {str(e)}")
|
| 33 |
|
| 34 |
-
# ---
|
| 35 |
def generate_response(message, history, language):
|
| 36 |
try:
|
| 37 |
model = load_model()
|
| 38 |
-
prompt = f"""Você é um
|
| 39 |
-
Gere código {language} funcional e bem documentado para:
|
| 40 |
{message}"""
|
| 41 |
|
| 42 |
start_time = time.time()
|
| 43 |
-
|
| 44 |
prompt,
|
| 45 |
max_length=1024,
|
| 46 |
-
temperature=0.3
|
| 47 |
-
do_sample=True
|
| 48 |
)[0]["generated_text"]
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 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
|
| 62 |
with gr.Blocks(
|
| 63 |
theme=gr.themes.Soft(
|
| 64 |
primary_hue="blue",
|
| 65 |
-
neutral_hue="slate",
|
| 66 |
font=[gr.themes.GoogleFont("Inter")]
|
| 67 |
),
|
| 68 |
css="""
|
| 69 |
-
.chatbot {
|
| 70 |
-
.code {
|
| 71 |
"""
|
| 72 |
) as app:
|
| 73 |
|
|
@@ -76,40 +62,47 @@ with gr.Blocks(
|
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column(scale=3):
|
| 78 |
language = gr.Dropdown(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
textbox=gr.Textbox(
|
| 88 |
-
placeholder="Descreva o código que precisa
|
| 89 |
lines=3
|
| 90 |
),
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
-
gr.Markdown("###
|
| 96 |
gr.Markdown("""
|
| 97 |
-
-
|
| 98 |
-
-
|
| 99 |
-
-
|
| 100 |
-
-
|
|
|
|
|
|
|
| 101 |
""")
|
| 102 |
|
| 103 |
-
gr.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
inputs=[chatbot.textbox, language],
|
| 110 |
-
label="💡 Exemplos Prontos"
|
| 111 |
-
)
|
| 112 |
|
| 113 |
-
# ---
|
| 114 |
if __name__ == "__main__":
|
| 115 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
from functools import lru_cache
|
|
|
|
| 5 |
|
| 6 |
# --- Configurações do Modelo --- #
|
| 7 |
+
MODEL_NAME = "bigcode/starcoder2-7b" # Modelo principal otimizado
|
| 8 |
+
LANGUAGES = ["python", "javascript", "java", "cpp", "html", "sql"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Cache do Modelo --- #
|
| 11 |
@lru_cache(maxsize=None)
|
| 12 |
def load_model():
|
| 13 |
try:
|
| 14 |
+
print("⚡ Carregando modelo...")
|
| 15 |
start_time = time.time()
|
| 16 |
model = pipeline(
|
| 17 |
"text-generation",
|
|
|
|
| 22 |
print(f"✅ Modelo carregado em {time.time()-start_time:.2f}s")
|
| 23 |
return model
|
| 24 |
except Exception as e:
|
| 25 |
+
raise RuntimeError(f"❌ Falha ao carregar modelo: {str(e)}")
|
| 26 |
|
| 27 |
+
# --- Função de Geração no Formato OpenAI --- #
|
| 28 |
def generate_response(message, history, language):
|
| 29 |
try:
|
| 30 |
model = load_model()
|
| 31 |
+
prompt = f"""Você é um especialista em {language}. Gere código {language} funcional para:
|
|
|
|
| 32 |
{message}"""
|
| 33 |
|
| 34 |
start_time = time.time()
|
| 35 |
+
response = model(
|
| 36 |
prompt,
|
| 37 |
max_length=1024,
|
| 38 |
+
temperature=0.3
|
|
|
|
| 39 |
)[0]["generated_text"]
|
| 40 |
|
| 41 |
+
# Extrai o código da resposta
|
| 42 |
+
code = response.split("```")[1][len(language)+1:] if "```" in response else response
|
| 43 |
+
return {"role": "assistant", "content": f"```{language}\n{code.strip()}\n```"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
+
return {"role": "assistant", "content": f"❌ Erro: {str(e)}"}
|
| 47 |
|
| 48 |
+
# --- Interface Moderna --- #
|
| 49 |
with gr.Blocks(
|
| 50 |
theme=gr.themes.Soft(
|
| 51 |
primary_hue="blue",
|
|
|
|
| 52 |
font=[gr.themes.GoogleFont("Inter")]
|
| 53 |
),
|
| 54 |
css="""
|
| 55 |
+
.chatbot {height: 500px;}
|
| 56 |
+
.code {max-height: 400px; overflow-y: auto;}
|
| 57 |
"""
|
| 58 |
) as app:
|
| 59 |
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column(scale=3):
|
| 64 |
language = gr.Dropdown(
|
| 65 |
+
label="Selecione a Linguagem",
|
| 66 |
+
choices=LANGUAGES,
|
| 67 |
+
value="python"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# ChatInterface moderno
|
| 71 |
+
chat_interface = gr.ChatInterface(
|
| 72 |
+
lambda msg, history: generate_response(msg, history, language.value),
|
| 73 |
textbox=gr.Textbox(
|
| 74 |
+
placeholder="Descreva o código que precisa...",
|
| 75 |
lines=3
|
| 76 |
),
|
| 77 |
+
examples=[
|
| 78 |
+
["Função Python para calcular Fibonacci"],
|
| 79 |
+
["Componente React em JavaScript"],
|
| 80 |
+
["Classe Java para conexão MySQL"]
|
| 81 |
+
]
|
| 82 |
)
|
| 83 |
|
| 84 |
with gr.Column(scale=2):
|
| 85 |
+
gr.Markdown("### 📋 Linguagens Suportadas")
|
| 86 |
gr.Markdown("""
|
| 87 |
+
- Python 🐍
|
| 88 |
+
- JavaScript 📜
|
| 89 |
+
- Java ☕
|
| 90 |
+
- C++ 🖥️
|
| 91 |
+
- HTML 🌐
|
| 92 |
+
- SQL 🗃️
|
| 93 |
""")
|
| 94 |
|
| 95 |
+
gr.Markdown("### ⚡ Dicas")
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
1. Seja específico nos requisitos
|
| 98 |
+
2. Mencione bibliotecas se necessário
|
| 99 |
+
3. Use os exemplos para começar
|
| 100 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# --- Configurações de Deploy --- #
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
app.launch(
|
| 105 |
+
server_name="0.0.0.0",
|
| 106 |
+
server_port=7860,
|
| 107 |
+
share=False
|
| 108 |
+
)
|