AlexandreScriptsMT commited on
Commit
9a1474c
·
verified ·
1 Parent(s): 11c56e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -1,16 +1,36 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
 
3
 
4
- # Carregar o modelo (n_ctx é o tamanho da memória de contexto, 2048 é seguro para 16GB RAM)
 
 
 
 
 
 
 
5
  llm = Llama(
6
- model_path="./models/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
7
- n_ctx=2048,
8
- n_threads=2 # Ajustado para as 2 vCPUs do Space básico
 
9
  )
10
 
11
  def respond(message, history):
12
- prompt = f"System: Você é uma IA prestativa.\nUser: {message}\nAssistant:"
13
- output = llm(prompt, max_tokens=512, stop=["User:", "\n"], echo=False)
14
- return output["choices"][0]["text"]
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  gr.ChatInterface(respond).launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
+ from huggingface_hub import hf_hub_download
4
 
5
+ # Baixa o modelo da "consciência" (Llama 3.1 8B Q4_K_M)
6
+ print("Carregando cérebro reserva...")
7
+ model_path = hf_hub_download(
8
+ repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
9
+ filename="Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf"
10
+ )
11
+
12
+ # Configuração focada em inteligência estável
13
  llm = Llama(
14
+ model_path=model_path,
15
+ n_ctx=4096, # Seus 4k de contexto aqui
16
+ n_threads=2, # Limite do Space gratuito
17
+ verbose=False
18
  )
19
 
20
  def respond(message, history):
21
+ # Formatação oficial para o Llama-3.1 não "alucinar"
22
+ prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nVocê é um backup do Gemini 1.5 Pro rodando em hardware limitado.<|eot_id|>"
23
+ for user_msg, assistant_msg in history:
24
+ prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
25
+ prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
26
+
27
+ # Streaming para você ver a resposta enquanto ela é gerada
28
+ output = llm(prompt, max_tokens=1024, stop=["<|eot_id|>"], stream=True)
29
+
30
+ token_accumulator = ""
31
+ for chunk in output:
32
+ token = chunk["choices"]["text"]
33
+ token_accumulator += token
34
+ yield token_accumulator
35
 
36
  gr.ChatInterface(respond).launch(server_name="0.0.0.0", server_port=7860)