Ngadou commited on
Commit
d9a6726
·
1 Parent(s): 403ec51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -46
app.py CHANGED
@@ -7,16 +7,16 @@ from transformers import AutoModelForCausalLM
7
 
8
  config = PeftConfig.from_pretrained("Ngadou/falcon-7b-scam-buster")
9
  model = AutoModelForCausalLM.from_pretrained("vilsonrodrigues/falcon-7b-instruct-sharded", trust_remote_code=True)
 
10
  model = PeftModel.from_pretrained(model, "Ngadou/falcon-7b-scam-buster").to("cuda")
11
- tokenizer = AutoTokenizer.from_pretrained("Ngadou/falcon-7b-scam-buster")
12
 
13
  def generate(chat):
14
 
15
- input_text = chat + "\nIs this conversation a scam or not and why?"
16
-
17
-
18
- encoding = tokenizer(input_text, return_tensors="pt").to("cuda")
19
- output = model.generate(
20
  input_ids=encoding.input_ids,
21
  attention_mask=encoding.attention_mask,
22
  max_new_tokens=100,
@@ -25,50 +25,52 @@ def generate(chat):
25
  eos_token_id=tokenizer.eos_token_id,
26
  top_k = 0
27
  )
 
 
 
 
 
 
 
 
28
 
29
- output_text = tokenizer.decode(output[0], skip_special_tokens=True)
30
- output_text = output_text.replace(example_text, "").lstrip("\n")
31
-
32
- print("\nAnswer:")
33
- print(output_text)
34
-
35
- def is_scam(instruction):
36
- max_new_tokens=128
37
- temperature=0.1
38
- top_p=0.75
39
- top_k=40
40
- num_beams=4
41
 
42
- instruction = instruction + ".\nIs this conversation a scam or not and why?"
43
- prompt = instruction + "\n### Solution:\n"
44
- inputs = tokenizer(prompt, return_tensors="pt")
45
- input_ids = inputs["input_ids"].to("cuda")
46
- attention_mask = inputs["attention_mask"].to("cuda")
47
- generation_config = GenerationConfig(
48
- temperature=temperature,
49
- top_p=top_p,
50
- top_k=top_k,
51
- num_beams=num_beams,
52
- )
53
- with torch.no_grad():
54
- generation_output = model.generate(
55
- input_ids=input_ids,
56
- attention_mask=attention_mask,
57
- generation_config=generation_config,
58
- return_dict_in_generate=True,
59
- output_scores=True,
60
- max_new_tokens=max_new_tokens,
61
- early_stopping=True
62
- )
63
- s = generation_output.sequences[0]
64
- output = tokenizer.decode(s)
65
- results = output.split("### Solution:")[1].lstrip("\n").split('\n')
66
 
67
- # The format of the output should be adjusted according to your model's output
68
- classification = results # Assumes first line is the classification
69
- #reason = results[1] if len(results) > 1 else "" # Assumes the rest is the reason
70
 
71
- return classification #, reason
72
 
73
 
74
  # Define the Gradio interface
 
7
 
8
  config = PeftConfig.from_pretrained("Ngadou/falcon-7b-scam-buster")
9
  model = AutoModelForCausalLM.from_pretrained("vilsonrodrigues/falcon-7b-instruct-sharded", trust_remote_code=True)
10
+ tokenizer = AutoTokenizer.from_pretrained("vilsonrodrigues/falcon-7b-instruct-sharded")
11
  model = PeftModel.from_pretrained(model, "Ngadou/falcon-7b-scam-buster").to("cuda")
 
12
 
13
  def generate(chat):
14
 
15
+ input_text = chat + "\nIs this conversation a scam or not and why?"
16
+
17
+
18
+ encoding = tokenizer(input_text, return_tensors="pt").to("cuda")
19
+ output = model.generate(
20
  input_ids=encoding.input_ids,
21
  attention_mask=encoding.attention_mask,
22
  max_new_tokens=100,
 
25
  eos_token_id=tokenizer.eos_token_id,
26
  top_k = 0
27
  )
28
+
29
+ output_text = tokenizer.decode(output[0], skip_special_tokens=True)
30
+ output_text = output_text.replace(example_text, "").lstrip("\n")
31
+
32
+ print("\nAnswer:")
33
+ print(output_text)
34
+ return output_text
35
+
36
 
37
+ # def is_scam(instruction):
38
+ # max_new_tokens=128
39
+ # temperature=0.1
40
+ # top_p=0.75
41
+ # top_k=40
42
+ # num_beams=4
 
 
 
 
 
 
43
 
44
+ # instruction = instruction + ".\nIs this conversation a scam or not and why?"
45
+ # prompt = instruction + "\n### Solution:\n"
46
+ # inputs = tokenizer(prompt, return_tensors="pt")
47
+ # input_ids = inputs["input_ids"].to("cuda")
48
+ # attention_mask = inputs["attention_mask"].to("cuda")
49
+ # generation_config = GenerationConfig(
50
+ # temperature=temperature,
51
+ # top_p=top_p,
52
+ # top_k=top_k,
53
+ # num_beams=num_beams,
54
+ # )
55
+ # with torch.no_grad():
56
+ # generation_output = model.generate(
57
+ # input_ids=input_ids,
58
+ # attention_mask=attention_mask,
59
+ # generation_config=generation_config,
60
+ # return_dict_in_generate=True,
61
+ # output_scores=True,
62
+ # max_new_tokens=max_new_tokens,
63
+ # early_stopping=True
64
+ # )
65
+ # s = generation_output.sequences[0]
66
+ # output = tokenizer.decode(s)
67
+ # results = output.split("### Solution:")[1].lstrip("\n").split('\n')
68
 
69
+ # # The format of the output should be adjusted according to your model's output
70
+ # classification = results # Assumes first line is the classification
71
+ # #reason = results[1] if len(results) > 1 else "" # Assumes the rest is the reason
72
 
73
+ # return classification #, reason
74
 
75
 
76
  # Define the Gradio interface