Update README.md
Browse files
README.md
CHANGED
|
@@ -1,21 +1,89 @@
|
|
| 1 |
-
---
|
| 2 |
-
language:
|
| 3 |
-
- en
|
| 4 |
-
pipeline_tag: text2text-generation
|
| 5 |
-
---
|
| 6 |
-
# My Custom Model
|
| 7 |
-
|
| 8 |
-
This is a custom model for text generation.
|
| 9 |
-
|
| 10 |
-
## Model Details
|
| 11 |
-
|
| 12 |
-
- `model_type`: Sparkoo
|
| 13 |
-
|
| 14 |
-
##
|
| 15 |
-
|
| 16 |
-
```python
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
pipeline_tag: text2text-generation
|
| 5 |
+
---
|
| 6 |
+
# My Custom Model
|
| 7 |
+
|
| 8 |
+
This is a custom model for text generation.
|
| 9 |
+
|
| 10 |
+
## Model Details
|
| 11 |
+
|
| 12 |
+
- `model_type`: Sparkoo
|
| 13 |
+
|
| 14 |
+
## Example usage
|
| 15 |
+
|
| 16 |
+
```python
|
| 17 |
+
import torch
|
| 18 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 19 |
+
|
| 20 |
+
def generate_text(prompt, model_name, max_length=100, num_return_sequences=1):
|
| 21 |
+
"""
|
| 22 |
+
Generate text using the Sparkoo/KateAI model from Hugging Face Hub.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
prompt (str): The input text to start generation from
|
| 26 |
+
model_name (str): Name of the model on Hugging Face Hub
|
| 27 |
+
max_length (int): Maximum length of generated text
|
| 28 |
+
num_return_sequences (int): Number of different sequences to generate
|
| 29 |
+
"""
|
| 30 |
+
# Load model and tokenizer
|
| 31 |
+
print(f"Loading model from {model_name}...")
|
| 32 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 33 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name) # Use original GPT2 tokenizer
|
| 34 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
+
|
| 36 |
+
# Move model to GPU if available
|
| 37 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 38 |
+
model = model.to(device)
|
| 39 |
+
model.eval()
|
| 40 |
+
|
| 41 |
+
# Encode the input prompt
|
| 42 |
+
encoded_prompt = tokenizer(prompt, return_tensors="pt", padding=True).to(device)
|
| 43 |
+
|
| 44 |
+
# Generate text
|
| 45 |
+
print("\nGenerating text...")
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
output_sequences = model.generate(
|
| 48 |
+
input_ids=encoded_prompt["input_ids"],
|
| 49 |
+
attention_mask=encoded_prompt["attention_mask"],
|
| 50 |
+
max_length=max_length,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
top_k=50,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
num_return_sequences=num_return_sequences,
|
| 56 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 57 |
+
eos_token_id=tokenizer.eos_token_id
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Decode and print the generated text
|
| 61 |
+
for idx, sequence in enumerate(output_sequences):
|
| 62 |
+
generated_text = tokenizer.decode(sequence, skip_special_tokens=True)
|
| 63 |
+
print(f"\nGenerated sequence {idx + 1}:")
|
| 64 |
+
print(f"{generated_text}")
|
| 65 |
+
print("-" * 50)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
# Example prompts to test
|
| 69 |
+
prompts = [
|
| 70 |
+
"Once upon a time",
|
| 71 |
+
"The artificial intelligence",
|
| 72 |
+
"In the distant future",
|
| 73 |
+
"The scientist discovered"
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
model_name = "Sparkoo/KateAI"
|
| 77 |
+
|
| 78 |
+
# Generate text for each prompt
|
| 79 |
+
for prompt in prompts:
|
| 80 |
+
print("\n" + "="*50)
|
| 81 |
+
print(f"Prompt: {prompt}")
|
| 82 |
+
print("="*50)
|
| 83 |
+
generate_text(
|
| 84 |
+
prompt=prompt,
|
| 85 |
+
model_name=model_name,
|
| 86 |
+
max_length=200, # Adjust as needed
|
| 87 |
+
num_return_sequences=3
|
| 88 |
+
)
|
| 89 |
+
```
|