Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- README.md +50 -12
- app.py +24 -0
- models/best_model.pt +3 -0
- requirements.txt +4 -0
- scripts/gpt2_train_per.py +1 -0
- src/inference.py +21 -0
- src/utils.py +41 -0
README.md
CHANGED
|
@@ -1,12 +1,50 @@
|
|
| 1 |
-
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GPT-2 Spaces Application
|
| 2 |
+
|
| 3 |
+
This project is a Hugging Face Spaces application that utilizes a trained GPT-2 model to generate text based on user input. The application provides an interactive interface for users to input prompts and receive generated outputs.
|
| 4 |
+
|
| 5 |
+
## Project Structure
|
| 6 |
+
|
| 7 |
+
- `app.py`: Main entry point for the application, setting up the Gradio interface.
|
| 8 |
+
- `requirements.txt`: Lists the dependencies required for the project.
|
| 9 |
+
- `README.md`: Documentation for the project, including setup and usage instructions.
|
| 10 |
+
- `.gitignore`: Specifies files and directories to be ignored by Git.
|
| 11 |
+
- `models/best_model.pt`: Contains the weights of the best model saved during training.
|
| 12 |
+
- `src/inference.py`: Logic for loading the trained model and generating text.
|
| 13 |
+
- `src/utils.py`: Utility functions for tasks such as tokenization and decoding.
|
| 14 |
+
- `scripts/gpt2_train_per.py`: Training script for the GPT model.
|
| 15 |
+
|
| 16 |
+
## Setup Instructions
|
| 17 |
+
|
| 18 |
+
1. **Clone the Repository**
|
| 19 |
+
```bash
|
| 20 |
+
git clone <repository-url>
|
| 21 |
+
cd gpt2-spaces-app
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
2. **Install Dependencies**
|
| 25 |
+
Make sure you have Python installed, then install the required packages:
|
| 26 |
+
```bash
|
| 27 |
+
pip install -r requirements.txt
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
3. **Run the Application**
|
| 31 |
+
Start the application using:
|
| 32 |
+
```bash
|
| 33 |
+
python app.py
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
4. **Access the Application**
|
| 37 |
+
Open your web browser and go to `http://localhost:7860` to interact with the model.
|
| 38 |
+
|
| 39 |
+
## Usage
|
| 40 |
+
|
| 41 |
+
- Enter a prompt in the input box and click the "Generate" button.
|
| 42 |
+
- The model will generate text based on the provided prompt and display the output below.
|
| 43 |
+
|
| 44 |
+
## Model Training
|
| 45 |
+
|
| 46 |
+
The model can be retrained using the script located in `scripts/gpt2_train_per.py`. Ensure you have the necessary data and adjust the training parameters as needed.
|
| 47 |
+
|
| 48 |
+
## License
|
| 49 |
+
|
| 50 |
+
This project is licensed under the MIT License. See the LICENSE file for more details.
|
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the model and tokenizer
|
| 8 |
+
model = GPT2LMHeadModel.from_pretrained('./models/best_model.pt')
|
| 9 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
@app.route('/generate', methods=['POST'])
|
| 13 |
+
def generate_text():
|
| 14 |
+
input_text = request.json.get('input_text', '')
|
| 15 |
+
inputs = tokenizer.encode(input_text, return_tensors='pt')
|
| 16 |
+
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model.generate(inputs, max_length=30, num_return_sequences=1)
|
| 19 |
+
|
| 20 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return jsonify({'generated_text': generated_text})
|
| 22 |
+
|
| 23 |
+
if __name__ == '__main__':
|
| 24 |
+
app.run(debug=True)
|
models/best_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c30cc8d0758ccf4154a7857ae971917f379a2b781a4149c88c3b2d1bc654a452
|
| 3 |
+
size 40
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.13.0
|
| 2 |
+
transformers==4.21.1
|
| 3 |
+
gradio==3.1.4
|
| 4 |
+
tiktoken==0.2.0
|
scripts/gpt2_train_per.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# This file is intentionally left blank.
|
src/inference.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class GPT2Inference:
|
| 5 |
+
def __init__(self, model_path):
|
| 6 |
+
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 7 |
+
self.model = GPT2LMHeadModel.from_pretrained(model_path)
|
| 8 |
+
self.model.eval()
|
| 9 |
+
|
| 10 |
+
def generate_text(self, prompt, max_length=30, num_return_sequences=1):
|
| 11 |
+
input_ids = self.tokenizer.encode(prompt, return_tensors='pt')
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
outputs = self.model.generate(input_ids, max_length=max_length, num_return_sequences=num_return_sequences)
|
| 14 |
+
return [self.tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
| 15 |
+
|
| 16 |
+
def load_model():
|
| 17 |
+
model_path = 'models/best_model.pt'
|
| 18 |
+
inference_model = GPT2Inference(model_path)
|
| 19 |
+
return inference_model
|
| 20 |
+
|
| 21 |
+
inference_model = load_model()
|
src/utils.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import tiktoken
|
| 3 |
+
|
| 4 |
+
def load_model(model_path):
|
| 5 |
+
"""Load the trained model from the specified path."""
|
| 6 |
+
from src.inference import GPT
|
| 7 |
+
from src.utils import GPTConfig
|
| 8 |
+
|
| 9 |
+
config = GPTConfig()
|
| 10 |
+
model = GPT(config)
|
| 11 |
+
model.load_state_dict(torch.load(model_path))
|
| 12 |
+
model.eval()
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
def tokenize_input(text):
|
| 16 |
+
"""Tokenize the input text using the GPT-2 tokenizer."""
|
| 17 |
+
enc = tiktoken.get_encoding('gpt2')
|
| 18 |
+
tokens = enc.encode(text)
|
| 19 |
+
return torch.tensor(tokens).unsqueeze(0) # Add batch dimension
|
| 20 |
+
|
| 21 |
+
def decode_output(tokens):
|
| 22 |
+
"""Decode the generated tokens back to text."""
|
| 23 |
+
enc = tiktoken.get_encoding('gpt2')
|
| 24 |
+
return enc.decode(tokens.tolist())
|
| 25 |
+
|
| 26 |
+
def generate_text(model, input_text, max_length=30):
|
| 27 |
+
"""Generate text using the trained model based on the input text."""
|
| 28 |
+
input_tokens = tokenize_input(input_text)
|
| 29 |
+
generated_tokens = input_tokens
|
| 30 |
+
|
| 31 |
+
while generated_tokens.size(1) < max_length:
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
logits = model(generated_tokens)[0]
|
| 34 |
+
logits = logits[:, -1, :]
|
| 35 |
+
probs = torch.softmax(logits, dim=-1)
|
| 36 |
+
topk_probs, topk_indices = torch.topk(probs, 50, dim=-1)
|
| 37 |
+
ix = torch.multinomial(topk_probs, 1)
|
| 38 |
+
xcol = torch.gather(topk_indices, -1, ix)
|
| 39 |
+
generated_tokens = torch.cat((generated_tokens, xcol), dim=1)
|
| 40 |
+
|
| 41 |
+
return decode_output(generated_tokens[0]) # Return the decoded output for the first sequence
|