Update app.py
Browse files
app.py
CHANGED
|
@@ -2,72 +2,47 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
# 1️⃣ Baixa o modelo uncensored (só na primeira vez)
|
| 7 |
-
# --------------------------
|
| 8 |
model_path = hf_hub_download(
|
| 9 |
repo_id="VibeStudio/Nidum-Llama-3.2-3B-Uncensored-GGUF",
|
| 10 |
filename="Nidum-Llama-3.2-3B-Uncensored-GGUF.gguf"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
# 2️⃣ Carrega o modelo em CPU
|
| 15 |
-
# --------------------------
|
| 16 |
llm = Llama(
|
| 17 |
model_path=model_path,
|
| 18 |
-
n_ctx=
|
| 19 |
-
n_batch=
|
| 20 |
-
n_gpu_layers=0, #
|
| 21 |
verbose=False
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
# 3️⃣ Função de chat com histórico e continuação automática
|
| 26 |
-
# --------------------------
|
| 27 |
def format_prompt(message, history):
|
| 28 |
-
""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
""
|
| 33 |
-
prompt = ""
|
| 34 |
-
for user_msg, assistant_msg in history:
|
| 35 |
-
prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
|
| 36 |
-
prompt += f"User: {message}\nAssistant: "
|
| 37 |
return prompt
|
| 38 |
|
| 39 |
def chat(message, history):
|
| 40 |
prompt = format_prompt(message, history)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
repeat_penalty=1.15,
|
| 51 |
-
stop=["User:"]
|
| 52 |
-
)
|
| 53 |
-
new_text = output["choices"][0]["text"]
|
| 54 |
-
if not new_text.strip(): # se não houver nova saída, para
|
| 55 |
-
break
|
| 56 |
-
response += new_text
|
| 57 |
-
# opcional: sair se resposta terminar naturalmente
|
| 58 |
-
if new_text.endswith((".", "!", "?", "\n")):
|
| 59 |
-
break
|
| 60 |
-
return response.strip()
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
# 4️⃣ Interface Gradio
|
| 64 |
-
# --------------------------
|
| 65 |
gr.ChatInterface(
|
| 66 |
-
|
| 67 |
-
title="
|
| 68 |
-
description=
|
| 69 |
-
"Chat com LLaMA 3B em CPU básica.\n"
|
| 70 |
-
"Respostas longas são geradas em blocos para não truncar.\n"
|
| 71 |
-
"Perguntas complexas podem ser divididas em partes."
|
| 72 |
-
)
|
| 73 |
).launch()
|
|
|
|
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# Baixa o modelo uncensored (só na primeira vez)
|
|
|
|
|
|
|
| 6 |
model_path = hf_hub_download(
|
| 7 |
repo_id="VibeStudio/Nidum-Llama-3.2-3B-Uncensored-GGUF",
|
| 8 |
filename="Nidum-Llama-3.2-3B-Uncensored-GGUF.gguf"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Carrega o modelo em CPU (n_gpu_layers=0 força CPU)
|
|
|
|
|
|
|
| 12 |
llm = Llama(
|
| 13 |
model_path=model_path,
|
| 14 |
+
n_ctx=8192, # contexto grande
|
| 15 |
+
n_batch=128, # reduzido para evitar travamentos em CPU
|
| 16 |
+
n_gpu_layers=0, # 0 = só CPU (essencial pro basic)
|
| 17 |
verbose=False
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Template simples pra chat (funciona bem com Llama)
|
|
|
|
|
|
|
| 21 |
def format_prompt(message, history):
|
| 22 |
+
prompt = "<|begin_of_text|>"
|
| 23 |
+
for user, assistant in history:
|
| 24 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n{user}<|eot_id|>"
|
| 25 |
+
prompt += f"<|start_header_id|>assistant<|end_header_id|>\n{assistant}<|eot_id|>"
|
| 26 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n{message}<|eot_id|>"
|
| 27 |
+
prompt += "<|start_header_id|>assistant<|end_header_id|>\n"
|
|
|
|
|
|
|
|
|
|
| 28 |
return prompt
|
| 29 |
|
| 30 |
def chat(message, history):
|
| 31 |
prompt = format_prompt(message, history)
|
| 32 |
+
output = llm(
|
| 33 |
+
prompt,
|
| 34 |
+
max_tokens=2048, # aumentado para respostas mais completas
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
top_p=0.95,
|
| 37 |
+
repeat_penalty=1.15 # evita repetição
|
| 38 |
+
# removido stop=["<|eot_id|>"] para não cortar no meio
|
| 39 |
+
)
|
| 40 |
+
return output["choices"][0]["text"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Interface Gradio
|
|
|
|
|
|
|
| 43 |
gr.ChatInterface(
|
| 44 |
+
chat,
|
| 45 |
+
title="Llama 3.2 3B Uncensored (CPU Básica)",
|
| 46 |
+
description="IA sem censura rodando só em CPU gratuita! Respostas em ~10-20s."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
).launch()
|
| 48 |
+
|