Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,148 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
datasets:
|
4 |
+
- teknium/openhermes
|
5 |
---
|
6 |
+
|
7 |
+
|
8 |
+
# Conversational Language Model Interface using FASTTEXT
|
9 |
+
|
10 |
+
This project provides a Command Line Interface (CLI) for interacting with a FastText language model, enabling users to generate text sequences based on their input. The script allows customization of parameters such as temperature, input text, top-k predictions, and model file path.
|
11 |
+
|
12 |
+
## Installation
|
13 |
+
|
14 |
+
Before running the script, ensure you have Python installed on your system. Additionally, you'll need to install the FastText library:
|
15 |
+
|
16 |
+
## Colab
|
17 |
+
|
18 |
+
[Google Colab Notebook](https://colab.research.google.com/drive/1jX1NShX7MzJnuL2whHNOA39Xu-meQ1ap?usp=sharing)
|
19 |
+
|
20 |
+
|
21 |
+
```bash
|
22 |
+
pip install fasttext
|
23 |
+
```
|
24 |
+
|
25 |
+
## Usage
|
26 |
+
|
27 |
+
To use the script, you should first obtain or train a FastText model. Place the model file (usually with a `.bin` extension) in a known directory.
|
28 |
+
|
29 |
+
The script can be executed with various command-line arguments to specify the behavior:
|
30 |
+
```python
|
31 |
+
import argparse
|
32 |
+
import fasttext
|
33 |
+
import numpy as np
|
34 |
+
|
35 |
+
def apply_repetition_penalty(labels, probabilities, used_labels, penalty_scale=1.9):
|
36 |
+
"""
|
37 |
+
Applies a repetition penalty to reduce the probability of already used labels.
|
38 |
+
|
39 |
+
:param labels: List of possible labels.
|
40 |
+
:param probabilities: Corresponding list of probabilities.
|
41 |
+
:param used_labels: Set of labels that have already been used.
|
42 |
+
:param penalty_scale: Scale of the penalty to be applied.
|
43 |
+
:return: Adjusted probabilities.
|
44 |
+
"""
|
45 |
+
adjusted_probabilities = probabilities.copy()
|
46 |
+
for i, label in enumerate(labels):
|
47 |
+
if label in used_labels:
|
48 |
+
adjusted_probabilities[i] /= penalty_scale
|
49 |
+
# Normalize the probabilities to sum to 1 again
|
50 |
+
adjusted_probabilities /= adjusted_probabilities.sum()
|
51 |
+
return adjusted_probabilities
|
52 |
+
|
53 |
+
def predict_sequence(model, text, sequence_length=20, temperature=.5, penalty_scale=1.9):
|
54 |
+
"""
|
55 |
+
Generates a sequence of labels using the FastText model with repetition penalty.
|
56 |
+
|
57 |
+
:param model: Loaded FastText model.
|
58 |
+
:param text: Initial text to start the prediction from.
|
59 |
+
:param sequence_length: Desired length of the sequence.
|
60 |
+
:param temperature: Temperature for sampling.
|
61 |
+
:param penalty_scale: Scale of repetition penalty.
|
62 |
+
:return: List of predicted labels.
|
63 |
+
"""
|
64 |
+
used_labels = set()
|
65 |
+
sequence = []
|
66 |
+
|
67 |
+
for _ in range(sequence_length):
|
68 |
+
# Predict the top k most probable labels
|
69 |
+
labels, probabilities = model.predict(text, k=40)
|
70 |
+
labels = [label.replace('__label__', '') for label in labels]
|
71 |
+
probabilities = np.array(probabilities)
|
72 |
+
|
73 |
+
# Adjust the probabilities with repetition penalty
|
74 |
+
probabilities = apply_repetition_penalty(labels, probabilities, used_labels, penalty_scale)
|
75 |
+
|
76 |
+
# Sampling according to the adjusted probabilities
|
77 |
+
label_index = np.random.choice(range(len(labels)), p=probabilities)
|
78 |
+
chosen_label = labels[label_index]
|
79 |
+
|
80 |
+
# Add the chosen label to the sequence and to the set of used labels
|
81 |
+
sequence.append(chosen_label)
|
82 |
+
used_labels.add(chosen_label)
|
83 |
+
|
84 |
+
# Update the text with the chosen label for the next prediction
|
85 |
+
text += ' ' + chosen_label
|
86 |
+
|
87 |
+
return sequence
|
88 |
+
|
89 |
+
def generate_response(model, input_text, sequence_length=512, temperature=.5, penalty_scale=1.9):
|
90 |
+
generated_sequence = predict_sequence(model, input_text, sequence_length, temperature, penalty_scale)
|
91 |
+
return ' '.join(generated_sequence)
|
92 |
+
|
93 |
+
def main():
|
94 |
+
parser = argparse.ArgumentParser(description="Run the language model with specified parameters.")
|
95 |
+
parser.add_argument('-t', '--temperature', type=float, default=0.5, help='Temperature for sampling.')
|
96 |
+
parser.add_argument('-f', '--file', type=str, help='File containing input text.')
|
97 |
+
parser.add_argument('-p', '--text', type=str, help='Direct input text.')
|
98 |
+
parser.add_argument('-n', '--length', type=int, default=50, help='length predictions to consider.')
|
99 |
+
parser.add_argument('-m', '--model', type=str, required=True, help='Address of the FastText model file.')
|
100 |
+
|
101 |
+
args = parser.parse_args()
|
102 |
+
|
103 |
+
# Load the model
|
104 |
+
model = fasttext.load_model(args.model)
|
105 |
+
|
106 |
+
input_text = ''
|
107 |
+
if args.file:
|
108 |
+
with open(args.file, 'r') as file:
|
109 |
+
input_text = file.read()
|
110 |
+
elif args.text:
|
111 |
+
input_text = args.text
|
112 |
+
else:
|
113 |
+
print("No input text provided. Please use -f to specify a file or -p for direct text input.")
|
114 |
+
return
|
115 |
+
|
116 |
+
# Generate and print the response
|
117 |
+
response = generate_response(model, input_text + " [RESPONSE]", sequence_length=args.length, temperature=args.temperature)
|
118 |
+
print("\nResponse:")
|
119 |
+
print(response)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
main()
|
123 |
+
|
124 |
+
|
125 |
+
```
|
126 |
+
|
127 |
+
```bash
|
128 |
+
python conversation_app.py -t TEMPERATURE -f FILE -p TEXT -k TOPK -m MODEL_PATH
|
129 |
+
```
|
130 |
+
|
131 |
+
- `-t TEMPERATURE` or `--temperature TEMPERATURE`: Sets the temperature for predictions. A higher temperature results in more diverse results. Default is 0.5.
|
132 |
+
- `-f FILE` or `--file FILE`: Specifies a path to a file containing input text. The script will read this file and use its contents as input.
|
133 |
+
- `-p TEXT` or `--text TEXT`: Directly provide the input text as a string.
|
134 |
+
- `-n LENGTH` or `--length TOPK`: Determines the number of top predictions to consider for the model's output. Default is 50.
|
135 |
+
- `-m MODEL_PATH` or `--model MODEL_PATH`: The path to the FastText model file (required).
|
136 |
+
|
137 |
+
### Example
|
138 |
+
|
139 |
+
```bash
|
140 |
+
python conversation_app.py -t 0.7 -p "What is the future of AI?" -n 40 -m /path/to/model.bin
|
141 |
+
```
|
142 |
+
|
143 |
+
This command sets the temperature to 0.7, uses the provided question as input, considers the top 40 predictions, and specifies the model file path.
|
144 |
+
|
145 |
+
## Note
|
146 |
+
|
147 |
+
- The script's output depends on the quality and training of the FastText model used.
|
148 |
+
- Ensure the specified model file path and input file path (if used) are correct.
|