diegomiranda commited on
Commit
3c58c10
·
1 Parent(s): 1959fe2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +39 -25
README.md CHANGED
@@ -29,39 +29,53 @@ pip install torch==2.0.0
29
  ```
30
 
31
  ```python
 
32
  import torch
33
- from transformers import pipeline
34
 
35
- generate_text = pipeline(
36
- model="diegomiranda/EleutherAI-70M-cypher-generator",
37
- torch_dtype="auto",
38
- trust_remote_code=True,
39
- use_fast=True,
40
- device_map={"": "cuda:0"},
41
- )
42
-
43
- res = generate_text(
44
- "Why is drinking water so healthy?",
45
- min_new_tokens=2,
46
- max_new_tokens=500,
47
- do_sample=False,
48
- num_beams=2,
49
- temperature=float(0.0),
50
- repetition_penalty=float(1.0),
51
- renormalize_logits=True
52
- )
53
- print(res[0]["generated_text"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  ```
55
 
56
- You can print a sample prompt after the preprocessing step to see how it is feed to the tokenizer:
57
 
58
  ```python
59
- print(generate_text.preprocess("Why is drinking water so healthy?")["prompt_text"])
 
 
 
60
  ```
61
 
62
- ```bash
63
- Why is drinking water so healthy?<|endoftext|>
64
- ```
65
 
66
  Alternatively, you can download [h2oai_pipeline.py](h2oai_pipeline.py), store it alongside your notebook, and construct the pipeline yourself from the loaded model and tokenizer. If the model and the tokenizer are fully supported in the `transformers` package, this will allow you to set `trust_remote_code=False`.
67
 
 
29
  ```
30
 
31
  ```python
32
+ from transformers import AutoModelForCausalLM, AutoTokenizer
33
  import torch
 
34
 
35
+ def generate_response(prompt, model_name):
36
+ tokenizer = AutoTokenizer.from_pretrained(
37
+ model_name,
38
+ use_fast=True,
39
+ trust_remote_code=True,
40
+ )
41
+
42
+ model = AutoModelForCausalLM.from_pretrained(
43
+ model_name,
44
+ torch_dtype=torch.float32,
45
+ device_map={"": "cpu"},
46
+ trust_remote_code=True,
47
+ )
48
+ model.cpu().eval()
49
+
50
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cpu")
51
+
52
+ tokens = model.generate(
53
+ input_ids=inputs["input_ids"],
54
+ attention_mask=inputs["attention_mask"],
55
+ min_new_tokens=2,
56
+ max_new_tokens=500,
57
+ do_sample=False,
58
+ num_beams=2,
59
+ temperature=float(0.0),
60
+ repetition_penalty=float(1.0),
61
+ renormalize_logits=True
62
+ )[0]
63
+
64
+ tokens = tokens[inputs["input_ids"].shape[1]:]
65
+ answer = tokenizer.decode(tokens, skip_special_tokens=True)
66
+
67
+ return answer
68
  ```
69
 
70
+ # Example usage
71
 
72
  ```python
73
+ model_name = "diegomiranda/EleutherAI-70M-cypher-generator"
74
+ prompt = "Create a Cypher statement to answer the following question:Retorne os processos de Direito Tributário que se baseiam em lei 939 de 1992?<|endoftext|>"
75
+ response = generate_response(prompt, model_name)
76
+ print(response)
77
  ```
78
 
 
 
 
79
 
80
  Alternatively, you can download [h2oai_pipeline.py](h2oai_pipeline.py), store it alongside your notebook, and construct the pipeline yourself from the loaded model and tokenizer. If the model and the tokenizer are fully supported in the `transformers` package, this will allow you to set `trust_remote_code=False`.
81