Ryan-PC commited on
Commit
61887df
·
verified ·
1 Parent(s): 8481230

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -26
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
- # 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=512,
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=1024,
35
- temperature=0.7,
36
- top_p=0.95,
37
- repeat_penalty=1.15, # evita repetição
38
- stop=["<|eot_id|>"]
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()
 
 
 
 
 
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()