Allen Park commited on
Commit
af1a93c
·
1 Parent(s): 17a12ac

.to(device) for model and handle other checks

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -11,7 +11,7 @@ else:
11
  device = "cpu"
12
 
13
  tokenizer = AutoTokenizer.from_pretrained("PatronusAI/Llama-3-Patronus-Lynx-8B-Instruct")
14
- model = AutoModelForCausalLM.from_pretrained("PatronusAI/Llama-3-Patronus-Lynx-8B-Instruct", torch_dtype=torch.float16, device_map="auto")
15
 
16
  PROMPT = """
17
  Given the following QUESTION, DOCUMENT and ANSWER you must analyze the provided answer and determine whether it is faithful to the contents of the DOCUMENT. The ANSWER must not offer new information beyond the context provided in the DOCUMENT. The ANSWER also must not contradict information provided in the DOCUMENT. Output your final verdict by strictly following this format: "PASS" if the answer is faithful to the DOCUMENT and "FAIL" if the answer is not faithful to the DOCUMENT. Show your reasoning.
@@ -35,17 +35,19 @@ Your output should be in JSON FORMAT with the keys "REASONING" and "SCORE":
35
  """
36
  @spaces.GPU()
37
  def model_call(question, document, answer):
 
38
  NEW_FORMAT = PROMPT.format(question=question, document=document, answer=answer)
39
- inputs = tokenizer(NEW_FORMAT, return_tensors="pt")
40
- input_ids = inputs.input_ids.to(device)
41
- attention_mask = inputs.attention_mask.to(device)
42
  generate_kwargs = dict(
43
  input_ids=input_ids,
44
  do_sample=True,
45
  attention_mask=attention_mask,
46
  pad_token_id=tokenizer.eos_token_id,
47
  )
48
- outputs = model.generate(**generate_kwargs)
 
49
  generated_text = tokenizer.decode(outputs[0])
50
  print(generated_text)
51
  return generated_text
 
11
  device = "cpu"
12
 
13
  tokenizer = AutoTokenizer.from_pretrained("PatronusAI/Llama-3-Patronus-Lynx-8B-Instruct")
14
+ model = AutoModelForCausalLM.from_pretrained("PatronusAI/Llama-3-Patronus-Lynx-8B-Instruct", torch_dtype=torch.float16, device_map="auto").to(device)
15
 
16
  PROMPT = """
17
  Given the following QUESTION, DOCUMENT and ANSWER you must analyze the provided answer and determine whether it is faithful to the contents of the DOCUMENT. The ANSWER must not offer new information beyond the context provided in the DOCUMENT. The ANSWER also must not contradict information provided in the DOCUMENT. Output your final verdict by strictly following this format: "PASS" if the answer is faithful to the DOCUMENT and "FAIL" if the answer is not faithful to the DOCUMENT. Show your reasoning.
 
35
  """
36
  @spaces.GPU()
37
  def model_call(question, document, answer):
38
+ device = next(model.parameters()).device
39
  NEW_FORMAT = PROMPT.format(question=question, document=document, answer=answer)
40
+ inputs = tokenizer(NEW_FORMAT, return_tensors="pt").to(device)
41
+ input_ids = inputs.input_ids
42
+ attention_mask = inputs.attention_mask
43
  generate_kwargs = dict(
44
  input_ids=input_ids,
45
  do_sample=True,
46
  attention_mask=attention_mask,
47
  pad_token_id=tokenizer.eos_token_id,
48
  )
49
+ with torch.no_grad():
50
+ outputs = model.generate(**generate_kwargs)
51
  generated_text = tokenizer.decode(outputs[0])
52
  print(generated_text)
53
  return generated_text