SpeedStar101 commited on
Commit
41f3c42
1 Parent(s): 97dd7a1

Delete Chatbot.md

Browse files
Files changed (1) hide show
  1. Chatbot.md +0 -104
Chatbot.md DELETED
@@ -1,104 +0,0 @@
1
- ## Requirements
2
- Be sure to install necessary libraries.
3
-
4
- ```python
5
- pip install torch
6
- pip install transformers
7
- pip install nltk
8
- pip install pyspellchecker
9
- ```
10
-
11
- ## Engaging with VergilGPT2
12
- If you're eager to have a conversation with VergilGPT2, you can utilize the following code snippet. Feel free to experiment with the variables 'temperature', 'top_k', 'top_p', to customize the response generation according to your preferences.
13
-
14
- ```python
15
- import torch
16
- import re
17
- import random
18
- import nltk
19
- from transformers import AutoTokenizer, AutoModelForCausalLM
20
- from nltk.corpus import words
21
- import string
22
- from spellchecker import SpellChecker
23
-
24
- spell = SpellChecker(language='en') # specify the language as English
25
-
26
- def filter_english_words(text):
27
- # Tokenize the input text
28
- tokenized_text = nltk.word_tokenize(text)
29
- # Initialize an empty list to store the filtered words
30
- filtered_text = []
31
- # Iterate through the tokenized text
32
- for word in tokenized_text:
33
- # Check if the word is in the English words set or is a punctuation
34
- if word.lower() in spell or word in string.punctuation:
35
- # If it is, append it to the filtered text list
36
- filtered_text.append(word)
37
- # Return the filtered text as a string
38
- return ' '.join(filtered_text)
39
-
40
- 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):
41
- input_ids = tokenizer.encode(input_text, return_tensors='pt')
42
-
43
- # Set model to eval mode
44
- model.eval()
45
-
46
- # Generate responses using different decoding strategies
47
- try:
48
- output = model.generate(
49
- input_ids=input_ids,
50
- max_length=max_length,
51
- min_length=min_length,
52
- num_return_sequences=num_return_sequences,
53
- temperature=temperature,
54
- top_k=top_k,
55
- top_p=top_p,
56
- num_beams=num_beams,
57
- no_repeat_ngram_size=2,
58
- do_sample=True,
59
- repetition_penalty=repetition_penalty # Added repetition_penalty
60
- )
61
-
62
- # Decode the generated responses
63
- responses = [tokenizer.decode(o, skip_special_tokens=True) for o in output]
64
- # Remove input_text from the responses
65
- responses = [response[len(tokenizer.decode(input_ids[0], skip_special_tokens=True)):] for response in responses]
66
-
67
- # Filter out non-English words
68
- responses = [filter_english_words(response) for response in responses]
69
-
70
- return responses
71
-
72
- except RuntimeError:
73
- return ["I'm sorry, I encountered an error while generating a response."]
74
-
75
-
76
- # Load pre-trained model and tokenizer
77
- access_token = "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"
78
- model_id = "Starcodium/VergilGPT2"
79
- tokenizer = AutoTokenizer.from_pretrained(model_id, revision="main", use_auth_token=access_token)
80
- model = AutoModelForCausalLM.from_pretrained(model_id, revision="main", use_auth_token=access_token)
81
-
82
- tokenizer.pad_token = tokenizer.eos_token
83
- model.config.pad_token_id = model.config.eos_token_id
84
-
85
- # Get user input and generate responses
86
- while True:
87
- input_text = input("Type 'quit' or 'exit' to stop runtime.\nEnter your input text: ")
88
- if input_text.lower() in ["quit", "exit"]:
89
- break
90
-
91
- responses = generate_response(model, tokenizer, input_text, max_length=100, min_length=20, num_return_sequences=1, temperature=0.7, top_k=40, top_p=0.5, num_beams=1, repetition_penalty=1.2)
92
- responses = [r for r in responses if r.strip() != '']
93
- if responses:
94
- response = responses[0]
95
- else:
96
- response = "I'm sorry, I don't have a response at the moment."
97
-
98
- # Print the bot's response
99
- print("Vergil: "+response)
100
- ```
101
-
102
- 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.
103
-
104
- Please note that the code assumes you have access to the Starcodium/VergilGPT2 model and its associated tokenizer. Ensure you have the required authentication token (access_token) to access the model and tokenizer.