| import os |
| import requests |
| import gradio as gr |
|
|
| |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| API_URL = "https://router.huggingface.co/hf-inference/v1/chat/completions" |
|
|
| |
| MODEL_ID = "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct" |
|
|
| def generate_response(prompt: str): |
| if not prompt.strip(): |
| return "Por favor, digite uma mensagem." |
| |
| try: |
| headers = { |
| "Authorization": f"Bearer {HF_TOKEN}", |
| "Content-Type": "application/json" |
| } |
| |
| payload = { |
| "model": MODEL_ID, |
| "messages": [ |
| { |
| "role": "system", |
| "content": ( |
| "You are an expert coding assistant. " |
| "You write clean, efficient, and well-documented code. " |
| "You can handle Python, JavaScript, Java, C++, and other languages. " |
| "When solving problems, explain your logic briefly before presenting the solution." |
| ) |
| }, |
| {"role": "user", "content": prompt} |
| ], |
| "temperature": 0.3, |
| "max_tokens": 1024 |
| } |
| |
| response = requests.post(API_URL, headers=headers, json=payload) |
| |
| |
| if response.status_code == 402: |
| return "Erro 402: Limite de uso excedido. Tente novamente mais tarde." |
| |
| response.raise_for_status() |
| return response.json()["choices"][0]["message"]["content"].strip() |
| |
| except Exception as e: |
| return f"Erro: {e}" |
|
|
| |
| |
| demo = gr.Interface( |
| fn=generate_response, |
| inputs=gr.Textbox( |
| lines=5, |
| label="Digite sua dúvida de programação", |
| placeholder="Ex: Escreva uma função Python para calcular Fibonacci..." |
| ), |
| outputs=gr.Textbox(lines=15, label="Código Gerado"), |
| title="DeepSeek-Coder-V2-Lite-Instruct", |
| description=( |
| "Assistente especializado em código usando DeepSeek-Coder-V2-Lite-Instruct. " |
| "Modelo leve, rápido e otimizado para programação." |
| ), |
| flagging_mode="never" |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch( |
| server_name="0.0.0.0", |
| server_port=7860 |
| ) |