Update README.md
Browse files
README.md
CHANGED
@@ -11,12 +11,72 @@ tags:
|
|
11 |
- trl
|
12 |
---
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
11 |
- trl
|
12 |
---
|
13 |
|
14 |
+
## Inference
|
15 |
|
16 |
+
```python
|
17 |
+
from unsloth import FastLanguageModel
|
18 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
19 |
+
model_name = "ImranzamanML/7B_finetuned_Mistral",
|
20 |
+
max_seq_length = 5020,
|
21 |
+
dtype = None,
|
22 |
+
load_in_4bit = True)
|
23 |
+
```
|
24 |
|
25 |
+
## Prompt to use for model answer
|
26 |
+
|
27 |
+
```python
|
28 |
+
data_prompt = """Analyze the provided text from a mental health perspective. Identify any indicators of emotional distress, coping mechanisms, or psychological well-being. Highlight any potential concerns or positive aspects related to mental health, and provide a brief explanation for each observation.
|
29 |
+
|
30 |
+
### Input:
|
31 |
+
{}
|
32 |
+
|
33 |
+
### Response:
|
34 |
+
{}"""
|
35 |
+
|
36 |
+
EOS_TOKEN = tokenizer.eos_token
|
37 |
+
def formatting_prompt(examples):
|
38 |
+
inputs = examples["Context"]
|
39 |
+
outputs = examples["Response"]
|
40 |
+
texts = []
|
41 |
+
for input_, output in zip(inputs, outputs):
|
42 |
+
text = data_prompt.format(input_, output) + EOS_TOKEN
|
43 |
+
texts.append(text)
|
44 |
+
return { "text" : texts, }
|
45 |
+
```
|
46 |
+
|
47 |
+
|
48 |
+
text="I'm going through some things with my feelings and myself. I barely sleep and I do nothing but think about how I'm worthless and how I shouldn't be here. I've never tried or contemplated suicide. I've always wanted to fix my issues, but I never get around to it. How can I change my feeling of being worthless to everyone?"
|
49 |
+
|
50 |
+
<div style="background-color: #f2f2f2; border-left: 5px solid #4CAF50; padding: 15px; margin: 20px 0;">
|
51 |
+
<strong>Note:</strong> Lets use the fine-tuned model for inference in order to generate responses based on mental health-related prompts !
|
52 |
+
</div>
|
53 |
+
|
54 |
+
<h3 style="color: #388e3c; font-family: Arial, sans-serif;">Here is some keys to note:</h3>
|
55 |
+
|
56 |
+
<ol style="margin-left: 20px;">
|
57 |
+
<p>The <code>model = FastLanguageModel.for_inference(model)</code> configures the model specifically for inference, optimizing its performance for generating responses.</p>
|
58 |
+
</li>
|
59 |
+
<p>The input text is tokenized using the <code>tokenizer</code>, it convert the text into a format that model can process. We are using <code>data_prompt</code> to format the input text, while the response placeholder is left empty to get response from model. The <code>return_tensors = "pt"</code> parameter specifies that the output should be in PyTorch tensors, which are then moved to the GPU using <code>.to("cuda")</code> for faster processing.</p>
|
60 |
+
</li>
|
61 |
+
<p>The <code>model.generate</code> method generating response based on the tokenized inputs. The parameters <code>max_new_tokens = 5020</code> and <code>use_cache = True</code> ensure that the model can produce long and coherent responses efficiently by utilizing cached computation from previous layers.</p>
|
62 |
+
</li>
|
63 |
+
</ol>
|
64 |
+
|
65 |
+
```python
|
66 |
+
model = FastLanguageModel.for_inference(model)
|
67 |
+
inputs = tokenizer(
|
68 |
+
[
|
69 |
+
data_prompt.format(
|
70 |
+
#instructions
|
71 |
+
text,
|
72 |
+
#answer
|
73 |
+
"",
|
74 |
+
)
|
75 |
+
], return_tensors = "pt").to("cuda")
|
76 |
+
|
77 |
+
outputs = model.generate(**inputs, max_new_tokens = 5020, use_cache = True)
|
78 |
+
answer=tokenizer.batch_decode(outputs)
|
79 |
+
answer = answer[0].split("### Response:")[-1]
|
80 |
+
print("Answer of the question is:", answer)
|
81 |
+
```
|
82 |
|
|