Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,131 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
max_tokens
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Configuração GLM-4.6
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 8 |
+
MODEL_ID = "zai-org/GLM-4.6"
|
| 9 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 10 |
|
| 11 |
+
class CodeAgentGLM:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 14 |
+
|
| 15 |
+
def generate_code(self, prompt, language="python", max_tokens=1024):
|
| 16 |
+
"""Gera código usando GLM-4.6"""
|
| 17 |
+
full_prompt = f"""You are an expert code generation AI. Generate high-quality {language} code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
Request: {prompt}
|
| 20 |
|
| 21 |
+
Provide only the code without explanations:"""
|
| 22 |
+
|
| 23 |
+
payload = {
|
| 24 |
+
"inputs": full_prompt,
|
| 25 |
+
"parameters": {
|
| 26 |
+
"max_new_tokens": max_tokens,
|
| 27 |
+
"temperature": 0.3,
|
| 28 |
+
"top_p": 0.9,
|
| 29 |
+
"do_sample": True
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(API_URL, headers=self.headers, json=payload, timeout=30)
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
result = response.json()
|
| 37 |
+
if isinstance(result, list) and len(result) > 0:
|
| 38 |
+
return result[0].get("generated_text", "").split("Provide only the code without explanations:")[-1].strip()
|
| 39 |
+
return f"Erro: {response.status_code}"
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Erro na chamada: {str(e)}"
|
| 42 |
+
|
| 43 |
+
def explain_code(self, code):
|
| 44 |
+
"""Explica código"""
|
| 45 |
+
prompt = f"Explique brevemente este código:\n\n{code}"
|
| 46 |
+
payload = {
|
| 47 |
+
"inputs": prompt,
|
| 48 |
+
"parameters": {"max_new_tokens": 500, "temperature": 0.5}
|
| 49 |
+
}
|
| 50 |
+
try:
|
| 51 |
+
response = requests.post(API_URL, headers=self.headers, json=payload)
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
result = response.json()
|
| 54 |
+
return result[0].get("generated_text", "") if isinstance(result, list) else str(result)
|
| 55 |
+
return f"Erro: {response.status_code}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Erro: {str(e)}"
|
| 58 |
|
| 59 |
+
agent = CodeAgentGLM()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Interface Gradio
|
| 62 |
+
with gr.Blocks(title="GLM-4.6 Code Agent", theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown("""
|
| 64 |
+
# 💻 GLM-4.6 Code Agent
|
| 65 |
+
**KILOcode Agent Integration** - Geração e explicação de código com GLM-4.6
|
| 66 |
+
|
| 67 |
+
- 🚀 Segue padrões GPT
|
| 68 |
+
- 📚 Suporta 200K tokens de contexto
|
| 69 |
+
- 💪 Excelente em coding tasks
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
with gr.Tabs():
|
| 73 |
+
with gr.Tab("🔨 Code Generation"):
|
| 74 |
+
code_prompt = gr.Textbox(
|
| 75 |
+
label="📝 Descrição do código",
|
| 76 |
+
placeholder="Ex: Crie uma função que calcula fibonacci com memoização",
|
| 77 |
+
lines=3
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
language = gr.Dropdown(
|
| 81 |
+
choices=["python", "javascript", "java", "cpp", "go", "rust"],
|
| 82 |
+
value="python",
|
| 83 |
+
label="🔤 Linguagem"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
max_tokens_slider = gr.Slider(
|
| 87 |
+
minimum=256,
|
| 88 |
+
maximum=2048,
|
| 89 |
+
value=1024,
|
| 90 |
+
step=256,
|
| 91 |
+
label="📏 Max tokens"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
generate_btn = gr.Button("Gerar Código", variant="primary")
|
| 95 |
+
code_output = gr.Code(language="python", label="💾 Código Gerado")
|
| 96 |
+
|
| 97 |
+
def generate(prompt, lang, tokens):
|
| 98 |
+
return agent.generate_code(prompt, lang, tokens)
|
| 99 |
+
|
| 100 |
+
generate_btn.click(generate, [code_prompt, language, max_tokens_slider], code_output)
|
| 101 |
+
|
| 102 |
+
with gr.Tab("📖 Code Explanation"):
|
| 103 |
+
code_to_explain = gr.Code(
|
| 104 |
+
language="python",
|
| 105 |
+
label="Código para explicar",
|
| 106 |
+
lines=10
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
explain_btn = gr.Button("Explicar", variant="primary")
|
| 110 |
+
explanation_output = gr.Textbox(label="Explicação", lines=6)
|
| 111 |
+
|
| 112 |
+
explain_btn.click(agent.explain_code, code_to_explain, explanation_output)
|
| 113 |
+
|
| 114 |
+
with gr.Tab("⚙️ Configuração"):
|
| 115 |
+
gr.Markdown("""
|
| 116 |
+
## Configuração do KILOcode Agent
|
| 117 |
+
|
| 118 |
+
### Modelo: GLM-4.6
|
| 119 |
+
- **Tamanho**: 357B parâmetros
|
| 120 |
+
- **Contexto**: 200K tokens
|
| 121 |
+
- **Formato**: BF16
|
| 122 |
+
- **Performance**: ⭐⭐⭐⭐⭐ para coding
|
| 123 |
+
|
| 124 |
+
### Como usar com KILOcode:
|
| 125 |
+
1. Adicione seu HF_TOKEN nos secrets
|
| 126 |
+
2. Use os endpoints de geração e explicação
|
| 127 |
+
3. Suporta múltiplas linguagens de programação
|
| 128 |
+
""")
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
+
demo.launch(share=True)
|