Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import warnings
|
2 |
-
import numpy as np
|
3 |
import pandas as pd
|
4 |
import os
|
5 |
import json
|
@@ -9,7 +7,7 @@ import torch
|
|
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,
|
13 |
from deap import base, creator, tools, algorithms
|
14 |
import gc
|
15 |
|
@@ -45,11 +43,11 @@ class EmotionalAIAssistant:
|
|
45 |
# Load pre-trained BERT model for emotion prediction
|
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 GPT-
|
50 |
-
self.
|
51 |
-
self.
|
52 |
-
|
53 |
# Enhanced Emotional States
|
54 |
self.emotions = {
|
55 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
@@ -168,13 +166,13 @@ class EmotionalAIAssistant:
|
|
168 |
full_prompt += f"Human: {turn[0]}\nAdam: {turn[1]}\n"
|
169 |
full_prompt += f"Human: {prompt}\nAdam:"
|
170 |
|
171 |
-
input_ids = self.
|
172 |
|
173 |
if torch.cuda.is_available():
|
174 |
input_ids = input_ids.cuda()
|
175 |
-
self.
|
176 |
|
177 |
-
output = self.
|
178 |
input_ids,
|
179 |
max_length=len(input_ids[0]) + max_length,
|
180 |
num_return_sequences=1,
|
@@ -186,8 +184,9 @@ class EmotionalAIAssistant:
|
|
186 |
early_stopping=True,
|
187 |
)
|
188 |
|
189 |
-
generated_text = self.
|
190 |
return generated_text
|
|
|
191 |
def predict_emotion(self, context):
|
192 |
emotion_prediction_pipeline = pipeline('text-classification', model=self.emotion_prediction_model, tokenizer=self.emotion_prediction_tokenizer, top_k=None)
|
193 |
predictions = emotion_prediction_pipeline(context)
|
@@ -238,5 +237,4 @@ class EmotionalAIAssistant:
|
|
238 |
|
239 |
|
240 |
if __name__ == "__main__":
|
241 |
-
|
242 |
-
assistant.run_gradio_interface()
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import os
|
3 |
import json
|
|
|
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, GPT3LMHeadModel, GPT3Tokenizer, pipeline
|
11 |
from deap import base, creator, tools, algorithms
|
12 |
import gc
|
13 |
|
|
|
43 |
# Load pre-trained BERT model for emotion prediction
|
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 |
+
# Load pre-trained GPT-3 Curie model for text generation
|
48 |
+
self.gpt3_tokenizer = GPT3Tokenizer.from_pretrained('gpt3-curie')
|
49 |
+
self.gpt3_model = GPT3LMHeadModel.from_pretrained('gpt3-curie', device_map='auto')
|
50 |
+
|
51 |
# Enhanced Emotional States
|
52 |
self.emotions = {
|
53 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
|
|
166 |
full_prompt += f"Human: {turn[0]}\nAdam: {turn[1]}\n"
|
167 |
full_prompt += f"Human: {prompt}\nAdam:"
|
168 |
|
169 |
+
input_ids = self.gpt3_tokenizer.encode(full_prompt + self.gpt3_tokenizer.eos_token, return_tensors='pt')
|
170 |
|
171 |
if torch.cuda.is_available():
|
172 |
input_ids = input_ids.cuda()
|
173 |
+
self.gpt3_model = self.gpt3_model.cuda()
|
174 |
|
175 |
+
output = self.gpt3_model.generate(
|
176 |
input_ids,
|
177 |
max_length=len(input_ids[0]) + max_length,
|
178 |
num_return_sequences=1,
|
|
|
184 |
early_stopping=True,
|
185 |
)
|
186 |
|
187 |
+
generated_text = self.gpt3_tokenizer.decode(output[0], skip_special_tokens=True)
|
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)
|
192 |
predictions = emotion_prediction_pipeline(context)
|
|
|
237 |
|
238 |
|
239 |
if __name__ == "__main__":
|
240 |
+
assistant = EmotionalAIAssistant()
|
|