Update app.py
Browse files
app.py
CHANGED
|
@@ -2,46 +2,72 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 12 |
llm = Llama(
|
| 13 |
model_path=model_path,
|
| 14 |
-
n_ctx=
|
| 15 |
-
n_batch=
|
| 16 |
-
n_gpu_layers=0, #
|
| 17 |
verbose=False
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
| 21 |
def format_prompt(message, history):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
prompt
|
|
|
|
|
|
|
|
|
|
| 28 |
return prompt
|
| 29 |
|
| 30 |
def chat(message, history):
|
| 31 |
prompt = format_prompt(message, history)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
| 43 |
gr.ChatInterface(
|
| 44 |
-
chat,
|
| 45 |
-
title="
|
| 46 |
-
description=
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=2048, # menor contexto pra CPU básica
|
| 19 |
+
n_batch=256, # batch menor
|
| 20 |
+
n_gpu_layers=0, # força CPU
|
| 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 |
+
Formata o prompt de forma simples:
|
| 30 |
+
User: pergunta
|
| 31 |
+
Assistant: resposta
|
| 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 |
+
response = ""
|
| 42 |
+
|
| 43 |
+
# Geração em blocos para evitar truncamento
|
| 44 |
+
for _ in range(3): # gera até 3 blocos se necessário
|
| 45 |
+
output = llm(
|
| 46 |
+
prompt + response,
|
| 47 |
+
max_tokens=512,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
top_p=0.95,
|
| 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 |
+
fn=chat,
|
| 67 |
+
title="Nidum LLaMA 3.2 3B Uncensored (CPU Básica)",
|
| 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()
|