Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
import torch
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
|
4 |
-
#
|
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 |
-
#
|
9 |
model_a = AutoModelForCausalLM.from_pretrained(
|
10 |
-
"
|
11 |
torch_dtype=TORCH_DTYPE
|
12 |
)
|
13 |
-
tokenizer_a = AutoTokenizer.from_pretrained("
|
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 |
-
#
|
25 |
model_b = AutoModelForCausalLM.from_pretrained(
|
26 |
-
"
|
27 |
torch_dtype=TORCH_DTYPE
|
28 |
)
|
29 |
-
tokenizer_b = AutoTokenizer.from_pretrained("
|
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 |
-
#
|
41 |
-
def
|
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
|
55 |
-
|
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)
|