Caikejs commited on
Commit
6212a79
·
verified ·
1 Parent(s): 638cce6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,16 +1,16 @@
1
  import torch
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
- # Configuração de dispositivo
5
  DEVICE = 0 if torch.cuda.is_available() else -1
6
  TORCH_DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
7
 
8
- # Carrega modelo A - LLaMA 2
9
  model_a = AutoModelForCausalLM.from_pretrained(
10
- "meta-llama/Llama-2-7b-chat-hf",
11
  torch_dtype=TORCH_DTYPE
12
  )
13
- tokenizer_a = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
14
 
15
  pipe_a = pipeline(
16
  "text-generation",
@@ -21,12 +21,12 @@ pipe_a = pipeline(
21
  pad_token_id=tokenizer_a.eos_token_id
22
  )
23
 
24
- # Carrega modelo B - Falcon 7B Instruct (sem autenticação)
25
  model_b = AutoModelForCausalLM.from_pretrained(
26
- "tiiuae/falcon-7b-instruct",
27
  torch_dtype=TORCH_DTYPE
28
  )
29
- tokenizer_b = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
30
 
31
  pipe_b = pipeline(
32
  "text-generation",
@@ -37,24 +37,20 @@ pipe_b = pipeline(
37
  pad_token_id=tokenizer_b.eos_token_id
38
  )
39
 
40
- # Funções auxiliares para formatar o prompt
41
- def format_llama_prompt(user_input):
42
- return f"[INST] <<SYS>>\nVocê é um assistente útil.\n<</SYS>>\n\n{user_input.strip()} [/INST]"
43
-
44
- def format_falcon_prompt(user_input):
45
  return f"Responda em português: {user_input.strip()}"
46
 
47
- # Interface simples para testar os modelos
48
  if __name__ == "__main__":
49
  while True:
50
  prompt = input("\nDigite uma pergunta (ou 'sair'): ").strip()
51
  if prompt.lower() == "sair":
52
  break
53
 
54
- print("\n=== Resposta do LLaMA 2 ===")
55
- llama_response = pipe_a(format_llama_prompt(prompt), max_new_tokens=200)[0]['generated_text']
56
- print(llama_response)
57
-
58
- print("\n=== Resposta do Falcon 7B ===")
59
- falcon_response = pipe_b(format_falcon_prompt(prompt), max_new_tokens=200)[0]['generated_text']
60
  print(falcon_response)
 
 
 
 
 
1
  import torch
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
+ # Detecta o dispositivo
5
  DEVICE = 0 if torch.cuda.is_available() else -1
6
  TORCH_DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
7
 
8
+ # Modelo A: Falcon 7B Instruct (sem autenticação)
9
  model_a = AutoModelForCausalLM.from_pretrained(
10
+ "tiiuae/falcon-7b-instruct",
11
  torch_dtype=TORCH_DTYPE
12
  )
13
+ tokenizer_a = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
14
 
15
  pipe_a = pipeline(
16
  "text-generation",
 
21
  pad_token_id=tokenizer_a.eos_token_id
22
  )
23
 
24
+ # Modelo B: OpenAssistant Pythia 12B (sem autenticação)
25
  model_b = AutoModelForCausalLM.from_pretrained(
26
+ "OpenAssistant/oasst-sft-1-pythia-12b",
27
  torch_dtype=TORCH_DTYPE
28
  )
29
+ tokenizer_b = AutoTokenizer.from_pretrained("OpenAssistant/oasst-sft-1-pythia-12b")
30
 
31
  pipe_b = pipeline(
32
  "text-generation",
 
37
  pad_token_id=tokenizer_b.eos_token_id
38
  )
39
 
40
+ # Interface de prompt
41
+ def format_prompt(user_input):
 
 
 
42
  return f"Responda em português: {user_input.strip()}"
43
 
 
44
  if __name__ == "__main__":
45
  while True:
46
  prompt = input("\nDigite uma pergunta (ou 'sair'): ").strip()
47
  if prompt.lower() == "sair":
48
  break
49
 
50
+ print("\n=== Resposta do Falcon ===")
51
+ falcon_response = pipe_a(format_prompt(prompt), max_new_tokens=200)[0]['generated_text']
 
 
 
 
52
  print(falcon_response)
53
+
54
+ print("\n=== Resposta do OpenAssistant ===")
55
+ oa_response = pipe_b(format_prompt(prompt), max_new_tokens=200)[0]['generated_text']
56
+ print(oa_response)