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