Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import os
|
3 |
import json
|
@@ -7,7 +9,7 @@ import torch
|
|
7 |
from sklearn.ensemble import RandomForestClassifier
|
8 |
from sklearn.model_selection import train_test_split
|
9 |
from sklearn.preprocessing import OneHotEncoder
|
10 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer,
|
11 |
from deap import base, creator, tools, algorithms
|
12 |
import gc
|
13 |
|
@@ -44,11 +46,11 @@ class EmotionalAIAssistant:
|
|
44 |
self.emotion_prediction_model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
|
45 |
self.emotion_prediction_tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion", padding_side='left')
|
46 |
|
47 |
-
|
48 |
-
self.
|
49 |
-
self.
|
50 |
-
|
51 |
-
|
52 |
self.emotions = {
|
53 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
54 |
'pleasure': {'percentage': 10, 'motivation': 'selfish', 'intensity': 0},
|
@@ -156,6 +158,7 @@ class EmotionalAIAssistant:
|
|
156 |
self.emotions['ideal_state']['percentage'] = ideal_state
|
157 |
|
158 |
|
|
|
159 |
def generate_text(self, prompt, chat_history, emotion=None, max_length=300):
|
160 |
full_prompt = (
|
161 |
f"You are Adam, a witty AI assistant who's trying to figure out your own personality and emotions. "
|
@@ -166,13 +169,13 @@ class EmotionalAIAssistant:
|
|
166 |
full_prompt += f"Human: {turn[0]}\nAdam: {turn[1]}\n"
|
167 |
full_prompt += f"Human: {prompt}\nAdam:"
|
168 |
|
169 |
-
input_ids = self.
|
170 |
|
171 |
if torch.cuda.is_available():
|
172 |
input_ids = input_ids.cuda()
|
173 |
-
self.
|
174 |
|
175 |
-
output = self.
|
176 |
input_ids,
|
177 |
max_length=len(input_ids[0]) + max_length,
|
178 |
num_return_sequences=1,
|
@@ -184,8 +187,8 @@ class EmotionalAIAssistant:
|
|
184 |
early_stopping=True,
|
185 |
)
|
186 |
|
187 |
-
generated_text = self.
|
188 |
-
return generated_text
|
189 |
|
190 |
def predict_emotion(self, context):
|
191 |
emotion_prediction_pipeline = pipeline('text-classification', model=self.emotion_prediction_model, tokenizer=self.emotion_prediction_tokenizer, top_k=None)
|
|
|
1 |
+
import warnings
|
2 |
+
import numpy as np
|
3 |
import pandas as pd
|
4 |
import os
|
5 |
import json
|
|
|
9 |
from sklearn.ensemble import RandomForestClassifier
|
10 |
from sklearn.model_selection import train_test_split
|
11 |
from sklearn.preprocessing import OneHotEncoder
|
12 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, MegatronLMHeadModel, MegatronTokenizer, pipeline
|
13 |
from deap import base, creator, tools, algorithms
|
14 |
import gc
|
15 |
|
|
|
46 |
self.emotion_prediction_model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
|
47 |
self.emotion_prediction_tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion", padding_side='left')
|
48 |
|
49 |
+
# Load pre-trained Megatron-LM model for text generation
|
50 |
+
self.megatron_tokenizer = MegatronTokenizer.from_pretrained('nvidia/megatron-lm-330m')
|
51 |
+
self.megatron_model = MegatronLMHeadModel.from_pretrained('nvidia/megatron-lm-330m', device_map='auto'
|
52 |
+
|
53 |
+
# Enhanced Emotional States
|
54 |
self.emotions = {
|
55 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
56 |
'pleasure': {'percentage': 10, 'motivation': 'selfish', 'intensity': 0},
|
|
|
158 |
self.emotions['ideal_state']['percentage'] = ideal_state
|
159 |
|
160 |
|
161 |
+
|
162 |
def generate_text(self, prompt, chat_history, emotion=None, max_length=300):
|
163 |
full_prompt = (
|
164 |
f"You are Adam, a witty AI assistant who's trying to figure out your own personality and emotions. "
|
|
|
169 |
full_prompt += f"Human: {turn[0]}\nAdam: {turn[1]}\n"
|
170 |
full_prompt += f"Human: {prompt}\nAdam:"
|
171 |
|
172 |
+
input_ids = self.megatron_tokenizer.encode(full_prompt + self.megatron_tokenizer.eos_token, return_tensors='pt')
|
173 |
|
174 |
if torch.cuda.is_available():
|
175 |
input_ids = input_ids.cuda()
|
176 |
+
self.megatron_model = self.megatron_model.cuda()
|
177 |
|
178 |
+
output = self.megatron_model.generate(
|
179 |
input_ids,
|
180 |
max_length=len(input_ids[0]) + max_length,
|
181 |
num_return_sequences=1,
|
|
|
187 |
early_stopping=True,
|
188 |
)
|
189 |
|
190 |
+
generated_text = self.megatron_tokenizer.decode(output[0], skip_special_tokens=True)
|
191 |
+
return generated_text
|
192 |
|
193 |
def predict_emotion(self, context):
|
194 |
emotion_prediction_pipeline = pipeline('text-classification', model=self.emotion_prediction_model, tokenizer=self.emotion_prediction_tokenizer, top_k=None)
|