SpeedStar101
commited on
Commit
•
98e8cbd
1
Parent(s):
1fa2eec
Update README.md
Browse files
README.md
CHANGED
@@ -53,83 +53,38 @@ If you're eager to have a conversation with VergilGPT2, you can utilize the foll
|
|
53 |
|
54 |
```python
|
55 |
import torch
|
56 |
-
import re
|
57 |
-
import random
|
58 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
59 |
|
60 |
-
def filter_english_words(text):
|
61 |
-
# Initialize an empty list to store the filtered words
|
62 |
-
filtered_text = []
|
63 |
-
# Split the input text into individual words
|
64 |
-
words = text.split()
|
65 |
-
# Iterate through the words
|
66 |
-
for word in words:
|
67 |
-
# Check if the word is a valid English word or a punctuation
|
68 |
-
if word.isalpha() or re.match(r'^[!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~]$', word):
|
69 |
-
# If it is, append it to the filtered text list
|
70 |
-
filtered_text.append(word)
|
71 |
-
# Return the filtered text as a string
|
72 |
-
return ' '.join(filtered_text)
|
73 |
-
|
74 |
-
def generate_response(model, tokenizer, input_text, max_length=300, min_length=20, num_return_sequences=1, temperature=0.2, top_k=50, top_p=0.9, num_beams=10, repetition_penalty=1.0):
|
75 |
-
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
76 |
-
|
77 |
-
# Set model to eval mode
|
78 |
-
model.eval()
|
79 |
-
|
80 |
-
# Generate responses using different decoding strategies
|
81 |
-
try:
|
82 |
-
output = model.generate(
|
83 |
-
input_ids=input_ids,
|
84 |
-
max_length=max_length,
|
85 |
-
min_length=min_length,
|
86 |
-
num_return_sequences=num_return_sequences,
|
87 |
-
temperature=temperature,
|
88 |
-
top_k=top_k,
|
89 |
-
top_p=top_p,
|
90 |
-
num_beams=num_beams,
|
91 |
-
no_repeat_ngram_size=2,
|
92 |
-
do_sample=True,
|
93 |
-
)
|
94 |
-
|
95 |
-
# Decode the generated responses
|
96 |
-
responses = [tokenizer.decode(o, skip_special_tokens=True) for o in output]
|
97 |
-
# Remove input_text from the responses
|
98 |
-
responses = [response[len(tokenizer.decode(input_ids[0], skip_special_tokens=True)):] for response in responses]
|
99 |
-
|
100 |
-
# Filter out non-English words
|
101 |
-
responses = [filter_english_words(response) for response in responses]
|
102 |
-
|
103 |
-
return responses
|
104 |
-
|
105 |
-
except RuntimeError:
|
106 |
-
return ["I'm sorry, I encountered an error while generating a response."]
|
107 |
-
|
108 |
-
|
109 |
# Load pre-trained model and tokenizer
|
110 |
access_token = "REPLACE_WITH_ACCESS_TOKEN"
|
111 |
-
model_id = "Starcodium/
|
112 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id,
|
113 |
-
model = AutoModelForCausalLM.from_pretrained(model_id,
|
114 |
|
115 |
tokenizer.pad_token = tokenizer.eos_token
|
116 |
model.config.pad_token_id = model.config.eos_token_id
|
117 |
|
118 |
# Get user input and generate responses
|
119 |
while True:
|
120 |
-
|
121 |
-
|
|
|
122 |
break
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
# Print the
|
132 |
-
print(
|
133 |
```
|
134 |
|
135 |
This code snippet allows you to engage in conversations with VergilGPT2. Simply enter your input text, and VergilGPT2 will generate responses based on the provided context. Experiment with different values of the variables temperature, top_k, and top_p to customize the response generation process according to your desired preferences.
|
|
|
53 |
|
54 |
```python
|
55 |
import torch
|
|
|
|
|
56 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Load pre-trained model and tokenizer
|
59 |
access_token = "REPLACE_WITH_ACCESS_TOKEN"
|
60 |
+
model_id = "Starcodium/Vergil_GPT-2"
|
61 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=access_token)
|
62 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=access_token)
|
63 |
|
64 |
tokenizer.pad_token = tokenizer.eos_token
|
65 |
model.config.pad_token_id = model.config.eos_token_id
|
66 |
|
67 |
# Get user input and generate responses
|
68 |
while True:
|
69 |
+
# Get user input
|
70 |
+
prompt = input("\nEnter your prompt (or 'exit' to quit): ")
|
71 |
+
if prompt.lower() == 'exit':
|
72 |
break
|
73 |
|
74 |
+
prompt_template = f"""A chat between a curious user and an artificial intelligence assistant named 'Vergil'. Vergil gives helpful, detailed, and polite answers to the user's questions.
|
75 |
+
|
76 |
+
USER: {prompt}
|
77 |
+
VERGIL:
|
78 |
+
"""
|
79 |
+
|
80 |
+
print("\n\nGenerating")
|
81 |
+
|
82 |
+
input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.cuda()
|
83 |
+
output = model.generate(inputs=input_ids, temperature=0.7, max_new_tokens=512)
|
84 |
+
response = tokenizer.decode(output[0]).replace(prompt_template,"").replace("<s> ", "").replace("</s>", "").split("VERGIL: ")[-1].strip() # Only keep the model's response
|
85 |
|
86 |
+
# Print only the model's response, without the conversation history
|
87 |
+
print(response)
|
88 |
```
|
89 |
|
90 |
This code snippet allows you to engage in conversations with VergilGPT2. Simply enter your input text, and VergilGPT2 will generate responses based on the provided context. Experiment with different values of the variables temperature, top_k, and top_p to customize the response generation process according to your desired preferences.
|