Instructions to use razerblade072611/EleutherAI2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use razerblade072611/EleutherAI2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="razerblade072611/EleutherAI2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("razerblade072611/EleutherAI2") model = AutoModelForCausalLM.from_pretrained("razerblade072611/EleutherAI2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use razerblade072611/EleutherAI2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "razerblade072611/EleutherAI2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "razerblade072611/EleutherAI2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/razerblade072611/EleutherAI2
- SGLang
How to use razerblade072611/EleutherAI2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "razerblade072611/EleutherAI2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "razerblade072611/EleutherAI2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "razerblade072611/EleutherAI2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "razerblade072611/EleutherAI2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use razerblade072611/EleutherAI2 with Docker Model Runner:
docker model run hf.co/razerblade072611/EleutherAI2
Commit ·
82f5e81
1
Parent(s): 6c8a6af
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,81 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import sys
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import nltk
|
| 7 |
-
import numpy as np
|
| 8 |
import pyttsx3
|
| 9 |
import spacy
|
| 10 |
-
import spacy as nlp
|
| 11 |
import speech_recognition as sr
|
| 12 |
import torch
|
| 13 |
-
|
| 14 |
-
from nltk.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
from
|
| 18 |
-
from
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
print("nltk version:", nltk.__version__)
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
|
| 36 |
# Download necessary NLTK resources
|
| 37 |
nltk.download('punkt')
|
| 38 |
-
nltk.download('stopwords')
|
| 39 |
nltk.download('wordnet')
|
| 40 |
-
nltk.download('
|
| 41 |
-
|
| 42 |
-
# Load the spaCy model
|
| 43 |
-
nlp = spacy.load('en_core_web_sm')
|
| 44 |
-
|
| 45 |
-
# Define a text input
|
| 46 |
-
text = "Example text to process"
|
| 47 |
-
|
| 48 |
-
# Process the text using the nlp object
|
| 49 |
-
doc = nlp(text)
|
| 50 |
-
|
| 51 |
-
# Extract named entities from the processed text
|
| 52 |
-
named_entities = []
|
| 53 |
-
for entity in doc.ents:
|
| 54 |
-
if entity.label_ in ['PERSON', 'ORG', 'GPE']:
|
| 55 |
-
named_entities.append(entity.text)
|
| 56 |
-
|
| 57 |
-
# Print the extracted named entities
|
| 58 |
-
print(named_entities)
|
| 59 |
-
|
| 60 |
-
# Load the API key from the environment file
|
| 61 |
-
dotenv_path = './API_KEY.env'
|
| 62 |
-
(dotenv_path)
|
| 63 |
|
| 64 |
# Check if GPU is available and set the device accordingly
|
| 65 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Initialize the speech engine
|
| 68 |
speech_engine = pyttsx3.init()
|
| 69 |
|
| 70 |
# Get the list of available voices
|
| 71 |
-
voices = speech_engine.getProperty('voices')
|
| 72 |
-
for voice in voices:
|
| 73 |
-
print(voice.id, voice.name)
|
| 74 |
-
|
| 75 |
-
# Set the desired voice
|
| 76 |
-
voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-GB_HAZEL_11.0 Microsoft Hazel Desktop - English (Great Britain)"
|
| 77 |
-
speech_engine.setProperty('voice', voice_id)
|
| 78 |
-
|
| 79 |
voices = speech_engine.getProperty('voices')
|
| 80 |
for voice in voices:
|
| 81 |
print(voice.id, voice.name)
|
|
@@ -96,28 +73,45 @@ if voice_id:
|
|
| 96 |
else:
|
| 97 |
print("Desired voice not found.")
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
class CommonModule:
|
| 101 |
-
def __init__(self, model, name, param1, param2):
|
| 102 |
# Initialize the instance variables using the provided arguments
|
| 103 |
-
self.
|
| 104 |
self.name = name
|
| 105 |
self.param1 = param1
|
| 106 |
self.param2 = param2
|
| 107 |
-
self.
|
|
|
|
|
|
|
| 108 |
self.tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
| 109 |
-
self.gpt3_model =
|
| 110 |
self.gpt3_model.to(device) # Move model to the device (GPU or CPU)
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
self.sentiment_module = SentimentAnalysisModule()
|
| 113 |
self.speech_engine = speech_engine # Assign the initialized speech engine
|
| 114 |
|
| 115 |
-
self.max_sequence_length =
|
| 116 |
self.num_beams = 4 # Reduce the value for faster response
|
| 117 |
self.no_repeat_ngram_size = 2
|
| 118 |
self.temperature = 0.3
|
| 119 |
self.response_cache = {} # Cache for storing frequently occurring responses
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
def reset_conversation(self):
|
| 122 |
self.memory_module.reset_memory()
|
| 123 |
|
|
@@ -128,30 +122,41 @@ class CommonModule:
|
|
| 128 |
return self.response_cache.get(entity)
|
| 129 |
return None
|
| 130 |
|
| 131 |
-
def
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
prompt,
|
| 137 |
-
do_sample=True,
|
| 138 |
-
min_length=10,
|
| 139 |
-
max_length=50,
|
| 140 |
-
num_return_sequences=2,
|
| 141 |
-
temperature=0.3
|
| 142 |
-
)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
return generated_response
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
def process_input(self, input_text, conversation_history):
|
| 151 |
named_entities = list(self.memory_module.get_named_entities())
|
| 152 |
for entity in named_entities:
|
| 153 |
if entity in input_text:
|
| 154 |
-
response =
|
| 155 |
self.memory_module.add_to_memory(response)
|
| 156 |
return response
|
| 157 |
|
|
@@ -159,198 +164,160 @@ class CommonModule:
|
|
| 159 |
if '?' in input_text:
|
| 160 |
return "You're making me angry, you wouldn't like me when I'm angry."
|
| 161 |
|
| 162 |
-
# Check if the input
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
else:
|
| 169 |
-
return "
|
| 170 |
|
| 171 |
-
# Retrieve
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
self.cache_response(input_text, response)
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
return response
|
| 183 |
|
|
|
|
| 184 |
self.memory_module.add_to_memory(response)
|
|
|
|
| 185 |
return response
|
| 186 |
|
| 187 |
-
def cache_response(self, input_text, response):
|
| 188 |
-
self.response_cache[input_text] = response
|
| 189 |
|
| 190 |
-
|
| 191 |
-
if conversation_history is None:
|
| 192 |
-
conversation_history = []
|
| 193 |
-
conversation_history.append(text)
|
| 194 |
-
full_text = "\n".join(conversation_history)
|
| 195 |
-
print(text)
|
| 196 |
-
self.speech_engine.say(text)
|
| 197 |
-
self.speech_engine.runAndWait()
|
| 198 |
|
| 199 |
-
def listen(self):
|
| 200 |
-
recognizer = sr.Recognizer()
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
|
| 206 |
-
try:
|
| 207 |
-
user_input = recognizer.recognize_google(audio)
|
| 208 |
-
print("You said:", user_input)
|
| 209 |
-
return user_input
|
| 210 |
-
except sr.UnknownValueError:
|
| 211 |
-
print("Sorry, I could not understand your speech.")
|
| 212 |
-
except sr.RequestError as e:
|
| 213 |
-
print("Sorry, an error occurred while processing your request. Please try again.")
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
|
| 218 |
-
self.reset_conversation()
|
| 219 |
-
self.speak("Hey, what's up bro? I'm {}".format(self.name))
|
| 220 |
|
| 221 |
-
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
self.speak("Nice to meet you, {}! I'm still {}".format(named_entities[0], self.name),
|
| 234 |
-
conversation_history)
|
| 235 |
|
| 236 |
-
conversation_history.append(user_input)
|
| 237 |
|
| 238 |
-
# Check if the conversation is over (you can define your own condition here)
|
| 239 |
-
if user_input == "bye":
|
| 240 |
-
self.save_memory('C:\\Users\\withe\PycharmProjects\\no hope\\Chat_Bot_Main\\save_memory.json')
|
| 241 |
-
break
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
}
|
| 248 |
-
with open(file_path, 'w') as file:
|
| 249 |
-
json.dump(data, file)
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
self.memory_module.named_entities = set(memory_data['named_entities'])
|
| 254 |
|
| 255 |
|
| 256 |
class MemoryModule:
|
| 257 |
-
def __init__(self):
|
| 258 |
self.memory = []
|
| 259 |
-
self.
|
| 260 |
-
self.
|
| 261 |
-
self.
|
| 262 |
-
self.named_entities = set() # Set to store named entities like names
|
| 263 |
|
| 264 |
-
def
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
def preprocess_text(self, text):
|
| 268 |
-
tokens = self.tokenizer(text.lower())
|
| 269 |
-
tokens = [self.lemmatizer.lemmatize(token) for token in tokens if token.isalnum()]
|
| 270 |
-
preprocessed_text = ' '.join(tokens)
|
| 271 |
-
return preprocessed_text
|
| 272 |
-
|
| 273 |
-
def add_to_memory(self, text):
|
| 274 |
-
preprocessed_text = self.preprocess_text(text)
|
| 275 |
-
self.memory.append(preprocessed_text)
|
| 276 |
-
|
| 277 |
-
# Update named entities if any
|
| 278 |
-
named_entity = self.extract_named_entity(text)
|
| 279 |
-
if named_entity:
|
| 280 |
-
self.named_entities.add(named_entity)
|
| 281 |
-
|
| 282 |
-
def extract_named_entity(self, text):
|
| 283 |
-
doc = nlp(text)
|
| 284 |
-
for entity in doc.ents:
|
| 285 |
-
if entity.label_ in ['PERSON', 'ORG', 'GPE']:
|
| 286 |
-
return entity.text
|
| 287 |
-
return None
|
| 288 |
-
|
| 289 |
-
def search_memory(self, keyword):
|
| 290 |
-
preprocessed_keyword = self.preprocess_text(keyword)
|
| 291 |
-
vectorized_memory = self.vectorizer.transform(self.memory)
|
| 292 |
-
vectorized_keyword = self.vectorizer.transform([preprocessed_keyword])
|
| 293 |
-
similarity_scores = np.dot(vectorized_memory, vectorized_keyword.T).toarray().flatten()
|
| 294 |
-
sorted_indices = np.argsort(similarity_scores)[::-1]
|
| 295 |
-
matches = [self.memory[i] for i in sorted_indices if similarity_scores[i] > 0.5]
|
| 296 |
-
return matches
|
| 297 |
|
| 298 |
def reset_memory(self):
|
| 299 |
self.memory = []
|
| 300 |
-
self.
|
| 301 |
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
def
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
return 'negative'
|
| 317 |
-
else:
|
| 318 |
-
return 'neutral'
|
| 319 |
|
| 320 |
|
| 321 |
-
# Define an exit handler function
|
| 322 |
-
def exit_handler(common_module):
|
| 323 |
-
memory_data = {
|
| 324 |
-
'memory': common_module.memory_module.memory,
|
| 325 |
-
'named_entities': list(common_module.memory_module.named_entities)
|
| 326 |
-
}
|
| 327 |
-
common_module.save_memory('C:\\Users\\withe\\PycharmProjects\\no hope2\\Chat_Bot1\\save_memory.json')
|
| 328 |
-
print("Memory data saved successfully.")
|
| 329 |
|
| 330 |
-
return memory_data
|
| 331 |
|
|
|
|
| 332 |
|
| 333 |
-
# Define a method to check if the load_memory.json file exists
|
| 334 |
-
def check_memory_file(file_path):
|
| 335 |
-
return os.path.isfile(file_path)
|
| 336 |
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
name = "Chat bot1"
|
| 342 |
-
param1 = 'value1'
|
| 343 |
-
param2 = 'value2'
|
| 344 |
-
common_module = CommonModule(model, name, param1, param2)
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
-
# Register the exit handler
|
| 353 |
-
atexit.register(exit_handler, common_module)
|
| 354 |
|
| 355 |
-
|
| 356 |
-
common_module.save_memory(memory_file_path)
|
|
|
|
| 1 |
+
MAIN_SCRIPT_MODULE
|
| 2 |
+
(common_module)
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
|
| 5 |
+
import atexit
|
| 6 |
import nltk
|
|
|
|
| 7 |
import pyttsx3
|
| 8 |
import spacy
|
|
|
|
| 9 |
import speech_recognition as sr
|
| 10 |
import torch
|
| 11 |
+
from transformers import GPTNeoForCausalLM, AutoTokenizer
|
| 12 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 13 |
+
import os
|
| 14 |
+
import json
|
| 15 |
+
from memory_module import MemoryModule
|
| 16 |
+
from sentiment_module import SentimentAnalysisModule
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Get the current directory
|
| 19 |
+
current_directory = os.getcwd()
|
| 20 |
|
| 21 |
+
# Get a list of files and directories in the current directory
|
| 22 |
+
file_list = os.listdir(current_directory)
|
|
|
|
| 23 |
|
| 24 |
+
# Print the list
|
| 25 |
+
for file_name in file_list:
|
| 26 |
+
print(file_name)
|
| 27 |
|
| 28 |
+
sia = SentimentIntensityAnalyzer()
|
| 29 |
+
sentence = "This is a positive sentence."
|
| 30 |
+
sentiment = sia.polarity_scores(sentence)
|
| 31 |
|
| 32 |
+
# Access sentiment scores
|
| 33 |
+
compound_score = sentiment['compound']
|
| 34 |
+
positive_score = sentiment['pos']
|
| 35 |
+
negative_score = sentiment['neg']
|
| 36 |
|
| 37 |
+
model_directory = "EleutherAI/gpt-neo-125m"
|
| 38 |
|
| 39 |
# Download necessary NLTK resources
|
| 40 |
nltk.download('punkt')
|
|
|
|
| 41 |
nltk.download('wordnet')
|
| 42 |
+
nltk.download('stopwords')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Check if GPU is available and set the device accordingly
|
| 45 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 46 |
+
if torch.cuda.is_available():
|
| 47 |
+
current_device = torch.cuda.current_device()
|
| 48 |
+
print(f"Using GPU: {torch.cuda.get_device_name(current_device)}")
|
| 49 |
+
else:
|
| 50 |
+
print("No GPU available, using CPU.")
|
| 51 |
|
| 52 |
# Initialize the speech engine
|
| 53 |
speech_engine = pyttsx3.init()
|
| 54 |
|
| 55 |
# Get the list of available voices
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
voices = speech_engine.getProperty('voices')
|
| 57 |
for voice in voices:
|
| 58 |
print(voice.id, voice.name)
|
|
|
|
| 73 |
else:
|
| 74 |
print("Desired voice not found.")
|
| 75 |
|
| 76 |
+
# Load the spaCy English model
|
| 77 |
+
nlp = spacy.load('en_core_web_sm')
|
| 78 |
+
|
| 79 |
+
# Update the CommonModule instantiation
|
| 80 |
+
load_memory_file = "load_memory.json"
|
| 81 |
+
save_memory_file = "save_memory.json"
|
| 82 |
|
| 83 |
class CommonModule:
|
| 84 |
+
def __init__(self, model, name, param1, param2, load_memory_file, save_memory_file):
|
| 85 |
# Initialize the instance variables using the provided arguments
|
| 86 |
+
self.memory = [] # Initialize memory as a list
|
| 87 |
self.name = name
|
| 88 |
self.param1 = param1
|
| 89 |
self.param2 = param2
|
| 90 |
+
self.model = GPTNeoForCausalLM.from_pretrained(model_directory)
|
| 91 |
+
|
| 92 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_directory)
|
| 93 |
self.tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
| 94 |
+
self.gpt3_model = GPTNeoForCausalLM.from_pretrained(model_directory)
|
| 95 |
self.gpt3_model.to(device) # Move model to the device (GPU or CPU)
|
| 96 |
+
|
| 97 |
+
self.load_memory_file = "C:\\Users\\withe\\PycharmProjects\\no hope2\\Chat_Bot4\\load_memory.json"
|
| 98 |
+
self.save_memory_file = "C:\\Users\\withe\\PycharmProjects\\no hope2\\Chat_Bot4\\save_memory.json"
|
| 99 |
+
self.memory_module = MemoryModule(self.load_memory_file, self.save_memory_file)
|
| 100 |
+
|
| 101 |
self.sentiment_module = SentimentAnalysisModule()
|
| 102 |
self.speech_engine = speech_engine # Assign the initialized speech engine
|
| 103 |
|
| 104 |
+
self.max_sequence_length = 200 # Decrease the value for faster response
|
| 105 |
self.num_beams = 4 # Reduce the value for faster response
|
| 106 |
self.no_repeat_ngram_size = 2
|
| 107 |
self.temperature = 0.3
|
| 108 |
self.response_cache = {} # Cache for storing frequently occurring responses
|
| 109 |
|
| 110 |
+
# Initialize speech recognition
|
| 111 |
+
self.recognizer = sr.Recognizer()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
def reset_conversation(self):
|
| 116 |
self.memory_module.reset_memory()
|
| 117 |
|
|
|
|
| 122 |
return self.response_cache.get(entity)
|
| 123 |
return None
|
| 124 |
|
| 125 |
+
def generate_gpt2_response(self, input_text, conversation_history):
|
| 126 |
+
# Prepare the conversation history for GPT-2 input format
|
| 127 |
+
if len(conversation_history) == 0:
|
| 128 |
+
gpt2_input = "USER: " + input_text + "\n"
|
| 129 |
+
else:
|
| 130 |
+
gpt2_input = "USER: " + conversation_history[-1] + "\n" # Append the user's query
|
| 131 |
+
gpt2_input += "BOT: " + conversation_history[-2] + "\n" # Append the bot's previous response
|
| 132 |
+
|
| 133 |
+
# Append the rest of the conversation history in reverse order
|
| 134 |
+
for i in range(len(conversation_history) - 3, -1, -2):
|
| 135 |
+
gpt2_input += "USER: " + conversation_history[i] + "\n"
|
| 136 |
+
gpt2_input += "BOT: " + conversation_history[i - 1] + "\n"
|
| 137 |
|
| 138 |
+
# Append the current user input to the conversation history
|
| 139 |
+
gpt2_input += "USER: " + input_text + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Tokenize the input text
|
| 142 |
+
input_ids = self.tokenizer.encode(gpt2_input, return_tensors='pt')
|
|
|
|
| 143 |
|
| 144 |
+
# Generate response using the GPT-2 model
|
| 145 |
+
with torch.no_grad():
|
| 146 |
+
output = self.model.generate(input_ids, max_length=100, num_return_sequences=1)
|
| 147 |
+
|
| 148 |
+
# Decode the generated response
|
| 149 |
+
generated_text = self.tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 150 |
+
|
| 151 |
+
# Process the GPT-2 response
|
| 152 |
+
response = generated_text.strip().split("\n")[-1] # Extract the last line (bot's response)
|
| 153 |
+
return response
|
| 154 |
|
| 155 |
def process_input(self, input_text, conversation_history):
|
| 156 |
named_entities = list(self.memory_module.get_named_entities())
|
| 157 |
for entity in named_entities:
|
| 158 |
if entity in input_text:
|
| 159 |
+
response = self.generate_gpt2_response(input_text, conversation_history)
|
| 160 |
self.memory_module.add_to_memory(response)
|
| 161 |
return response
|
| 162 |
|
|
|
|
| 164 |
if '?' in input_text:
|
| 165 |
return "You're making me angry, you wouldn't like me when I'm angry."
|
| 166 |
|
| 167 |
+
# Check if the input is a greeting
|
| 168 |
+
greetings = ['hello', 'hi', 'hey', 'hola']
|
| 169 |
+
for greeting in greetings:
|
| 170 |
+
if greeting in input_text.lower():
|
| 171 |
+
return "Hello! How can I assist you today?"
|
| 172 |
+
|
| 173 |
+
# Check if the input is a statement about the model
|
| 174 |
+
if self.name.lower() in input_text.lower():
|
| 175 |
+
return "Yes, I am {}. How can I assist you today?".format(self.name)
|
| 176 |
+
|
| 177 |
+
# Check if the input is a statement about the creator
|
| 178 |
+
if 'creator' in input_text.lower():
|
| 179 |
+
return "I was created by {}.".format(self.param1)
|
| 180 |
+
|
| 181 |
+
# Check if the input is a sentiment analysis request
|
| 182 |
+
if 'sentiment' in input_text.lower():
|
| 183 |
+
sentiment = self.sentiment_module.analyze_sentiment(input_text)
|
| 184 |
+
if sentiment == 'positive':
|
| 185 |
+
return "The sentiment of the text is positive."
|
| 186 |
+
elif sentiment == 'negative':
|
| 187 |
+
return "The sentiment of the text is negative."
|
| 188 |
else:
|
| 189 |
+
return "The sentiment of the text is neutral."
|
| 190 |
|
| 191 |
+
# Retrieve a cached response if available
|
| 192 |
+
cached_response = self.retrieve_cached_response(input_text)
|
| 193 |
+
if cached_response:
|
| 194 |
+
return cached_response
|
| 195 |
|
| 196 |
+
# Generate a response using GPT-2
|
| 197 |
+
response = self.generate_gpt2_response(input_text, conversation_history)
|
|
|
|
| 198 |
|
| 199 |
+
# Update the conversation history and cache the response
|
| 200 |
+
conversation_history.append(input_text)
|
| 201 |
+
conversation_history.append(response)
|
| 202 |
+
self.response_cache[input_text] = response
|
|
|
|
| 203 |
|
| 204 |
+
# Update memory with the generated response
|
| 205 |
self.memory_module.add_to_memory(response)
|
| 206 |
+
|
| 207 |
return response
|
| 208 |
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
common_module = CommonModule(model_directory, "Chatbot", "John Doe", "Jane Smith", load_memory_file, save_memory_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
def text_to_speech(text):
|
| 214 |
+
common_module.speech_engine.say(text)
|
| 215 |
+
common_module.speech_engine.runAndWait()
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
def exit_handler():
|
| 219 |
+
common_module.reset_conversation()
|
| 220 |
+
|
| 221 |
|
| 222 |
+
atexit.register(exit_handler)
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
recognizer = sr.Recognizer()
|
| 225 |
|
| 226 |
+
while True:
|
| 227 |
+
with sr.Microphone() as source:
|
| 228 |
+
print("Listening...")
|
| 229 |
+
audio = recognizer.listen(source)
|
| 230 |
|
| 231 |
+
try:
|
| 232 |
+
user_input = recognizer.recognize_google(audio)
|
| 233 |
+
print("User:", user_input)
|
| 234 |
+
except sr.UnknownValueError:
|
| 235 |
+
print("Sorry, I could not understand your speech.")
|
| 236 |
+
continue
|
| 237 |
+
except sr.RequestError:
|
| 238 |
+
print("Sorry, the speech recognition service is currently unavailable.")
|
| 239 |
+
continue
|
| 240 |
|
| 241 |
+
response = common_module.process_input(user_input, [])
|
| 242 |
+
print("Bot:", response)
|
| 243 |
+
text_to_speech(response)
|
|
|
|
|
|
|
| 244 |
|
|
|
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
MEMORY_MODULE
|
| 248 |
+
|
| 249 |
+
import json
|
| 250 |
+
import spacy
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
+
# Load the spaCy English model
|
| 253 |
+
nlp = spacy.load('en_core_web_sm')
|
|
|
|
| 254 |
|
| 255 |
|
| 256 |
class MemoryModule:
|
| 257 |
+
def __init__(self, load_file, save_file):
|
| 258 |
self.memory = []
|
| 259 |
+
self.load_file = load_file
|
| 260 |
+
self.save_file = save_file
|
| 261 |
+
self.load_memory()
|
|
|
|
| 262 |
|
| 263 |
+
def add_to_memory(self, statement):
|
| 264 |
+
self.memory.append(statement)
|
| 265 |
+
self.save_memory()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
def reset_memory(self):
|
| 268 |
self.memory = []
|
| 269 |
+
self.save_memory()
|
| 270 |
|
| 271 |
+
def save_memory(self):
|
| 272 |
+
with open(self.save_file, 'w') as file:
|
| 273 |
+
json.dump(self.memory, file)
|
| 274 |
|
| 275 |
+
def load_memory(self):
|
| 276 |
+
try:
|
| 277 |
+
with open(self.load_file, 'r') as file:
|
| 278 |
+
loaded_memory = json.load(file)
|
| 279 |
+
if isinstance(loaded_memory, list):
|
| 280 |
+
self.memory = loaded_memory
|
| 281 |
+
else:
|
| 282 |
+
print("Loaded memory is not a list. Starting with an empty memory.")
|
| 283 |
+
except FileNotFoundError:
|
| 284 |
+
print("Load memory file not found. Starting with an empty memory.")
|
| 285 |
|
| 286 |
+
def get_named_entities(self):
|
| 287 |
+
named_entities = set()
|
| 288 |
+
for statement in self.memory:
|
| 289 |
+
doc = nlp(statement)
|
| 290 |
+
for entity in doc.ents:
|
| 291 |
+
if entity.label_:
|
| 292 |
+
named_entities.add(entity.text)
|
| 293 |
+
return named_entities
|
| 294 |
|
| 295 |
+
|
| 296 |
+
memory_module = MemoryModule(
|
| 297 |
+
r"C:\Users\withe\PycharmProjects\no hope2\Chat_Bot4\load_memory.json",
|
| 298 |
+
r"C:\Users\withe\PycharmProjects\no hope2\Chat_Bot4\save_memory.json"
|
| 299 |
+
)
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
|
|
|
| 303 |
|
| 304 |
+
SENTIMENT_MODULE
|
| 305 |
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
|
| 308 |
+
class SentimentAnalysisModule:
|
| 309 |
+
def __init__(self):
|
| 310 |
+
self.sia = SentimentIntensityAnalyzer()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
def analyze_sentiment(self, text):
|
| 313 |
+
sentiment = self.sia.polarity_scores(text)
|
| 314 |
+
compound_score = sentiment['compound']
|
| 315 |
+
if compound_score >= 0.05:
|
| 316 |
+
return 'positive'
|
| 317 |
+
elif compound_score <= -0.05:
|
| 318 |
+
return 'negative'
|
| 319 |
+
else:
|
| 320 |
+
return 'neutral'
|
| 321 |
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
|
|
|