Update README.md
Browse files
README.md
CHANGED
|
@@ -6,4 +6,74 @@ language:
|
|
| 6 |
metrics:
|
| 7 |
- accuracy
|
| 8 |
pipeline_tag: text-generation
|
| 9 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
metrics:
|
| 7 |
- accuracy
|
| 8 |
pipeline_tag: text-generation
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
Certainly! Here's a short README for using the pre-trained `distilgpt2` model for chatting:
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# DistilGPT-2 Chatbot
|
| 16 |
+
|
| 17 |
+
This project demonstrates how to use the pre-trained `distilgpt2` model from Hugging Face for creating a simple chatbot. It includes code for loading the model, generating responses, and running an interactive conversation loop.
|
| 18 |
+
|
| 19 |
+
## Prerequisites
|
| 20 |
+
|
| 21 |
+
Ensure you have the following libraries installed:
|
| 22 |
+
|
| 23 |
+
```bash
|
| 24 |
+
pip install transformers torch
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
## Usage
|
| 28 |
+
|
| 29 |
+
1. **Load the Pre-trained Model and Tokenizer**
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 33 |
+
|
| 34 |
+
model_name = "distilgpt2"
|
| 35 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 36 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
2. **Generate a Response**
|
| 40 |
+
|
| 41 |
+
Use the following function to generate a response based on user input:
|
| 42 |
+
|
| 43 |
+
```python
|
| 44 |
+
def generate_response(prompt, max_length=100):
|
| 45 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 46 |
+
output = model.generate(
|
| 47 |
+
input_ids,
|
| 48 |
+
max_length=max_length,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 50 |
+
no_repeat_ngram_size=2,
|
| 51 |
+
num_return_sequences=1,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
top_p=0.9,
|
| 54 |
+
top_k=50
|
| 55 |
+
)
|
| 56 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 57 |
+
return response
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
3. **Interactive Conversation Loop**
|
| 61 |
+
|
| 62 |
+
Run the following code to start a chat session:
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
while True:
|
| 66 |
+
user_input = input("You: ")
|
| 67 |
+
prompt = f"<user> {user_input}<AI>"
|
| 68 |
+
response = generate_response(prompt)
|
| 69 |
+
print(f"AI: {response}")
|
| 70 |
+
|
| 71 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 72 |
+
break
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## Configuration
|
| 76 |
+
|
| 77 |
+
- **Temperature**: Controls randomness. Lower values are more deterministic.
|
| 78 |
+
- **Top-p and top-k**: Limit word selection for balanced diversity and coherence.
|
| 79 |
+
- **Max_length**: Limits the length of the response.
|