Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,16 +8,13 @@ import gradio as gr
|
|
8 |
import torch
|
9 |
import torch.nn as nn
|
10 |
import torch.optim as optim
|
11 |
-
from torch.utils.data import DataLoader,
|
12 |
-
from sklearn.ensemble import
|
13 |
from sklearn.model_selection import train_test_split
|
14 |
from sklearn.preprocessing import OneHotEncoder
|
15 |
-
from sklearn.neural_network import MLPClassifier
|
16 |
from deap import base, creator, tools, algorithms
|
17 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, AutoModelForSequenceClassification
|
18 |
import gc
|
19 |
-
import multiprocessing as mp
|
20 |
-
from joblib import Parallel, delayed
|
21 |
|
22 |
warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
|
23 |
|
@@ -153,7 +150,7 @@ def evolve_emotions():
|
|
153 |
emotions['ideal_state']['percentage'] = ideal_state
|
154 |
|
155 |
def predict_emotion(context):
|
156 |
-
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer,
|
157 |
predictions = emotion_prediction_pipeline(context)
|
158 |
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions[0]}
|
159 |
emotion_pred = max(emotion_scores, key=emotion_scores.get)
|
@@ -187,7 +184,7 @@ def generate_text(prompt, emotion=None, max_length=100):
|
|
187 |
return finetuned_lm_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
188 |
|
189 |
def optimize_ai_model(emotion_history):
|
190 |
-
if
|
191 |
return None, None
|
192 |
|
193 |
contexts = [entry['context'] for entry in emotion_history]
|
@@ -197,9 +194,6 @@ def optimize_ai_model(emotion_history):
|
|
197 |
X = encoder.fit_transform(np.array(contexts).reshape(-1, 1))
|
198 |
y = np.array(pd.Categorical(emotions).codes)
|
199 |
|
200 |
-
if len(X) == 0 or len(y) == 0:
|
201 |
-
return None, None
|
202 |
-
|
203 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
204 |
|
205 |
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
|
@@ -208,7 +202,7 @@ def optimize_ai_model(emotion_history):
|
|
208 |
score = classifier.score(X_test, y_test)
|
209 |
return classifier, score
|
210 |
|
211 |
-
def respond(context,
|
212 |
normalized_context = normalize_context(context)
|
213 |
emotion_pred = predict_emotion(normalized_context)
|
214 |
|
@@ -222,7 +216,7 @@ def respond(context, previous_responses, previous_emotions):
|
|
222 |
|
223 |
evolve_emotions()
|
224 |
|
225 |
-
response_prompt = f"{normalized_context}\n\n{
|
226 |
generated_response = generate_text(response_prompt, emotion=emotion_pred)
|
227 |
|
228 |
return generated_response, emotion_pred
|
@@ -246,3 +240,6 @@ with gr.Blocks() as demo:
|
|
246 |
submit_button.click(respond_wrapper, [context_input, conversation_history, emotion_history_state], [response_output, emotion_output, conversation_history, emotion_history_state])
|
247 |
|
248 |
demo.launch(share=True)
|
|
|
|
|
|
|
|
8 |
import torch
|
9 |
import torch.nn as nn
|
10 |
import torch.optim as optim
|
11 |
+
from torch.utils.data import DataLoader, Dataset
|
12 |
+
from sklearn.ensemble import RandomForestClassifier
|
13 |
from sklearn.model_selection import train_test_split
|
14 |
from sklearn.preprocessing import OneHotEncoder
|
|
|
15 |
from deap import base, creator, tools, algorithms
|
16 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, AutoModelForSequenceClassification
|
17 |
import gc
|
|
|
|
|
18 |
|
19 |
warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
|
20 |
|
|
|
150 |
emotions['ideal_state']['percentage'] = ideal_state
|
151 |
|
152 |
def predict_emotion(context):
|
153 |
+
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
|
154 |
predictions = emotion_prediction_pipeline(context)
|
155 |
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions[0]}
|
156 |
emotion_pred = max(emotion_scores, key=emotion_scores.get)
|
|
|
184 |
return finetuned_lm_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
185 |
|
186 |
def optimize_ai_model(emotion_history):
|
187 |
+
if len(emotion_history) < 2:
|
188 |
return None, None
|
189 |
|
190 |
contexts = [entry['context'] for entry in emotion_history]
|
|
|
194 |
X = encoder.fit_transform(np.array(contexts).reshape(-1, 1))
|
195 |
y = np.array(pd.Categorical(emotions).codes)
|
196 |
|
|
|
|
|
|
|
197 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
198 |
|
199 |
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
|
|
|
202 |
score = classifier.score(X_test, y_test)
|
203 |
return classifier, score
|
204 |
|
205 |
+
def respond(context, conversation_history, emotion_history_state):
|
206 |
normalized_context = normalize_context(context)
|
207 |
emotion_pred = predict_emotion(normalized_context)
|
208 |
|
|
|
216 |
|
217 |
evolve_emotions()
|
218 |
|
219 |
+
response_prompt = f"{normalized_context}\n\n{conversation_history}\n\n{emotion_pred}"
|
220 |
generated_response = generate_text(response_prompt, emotion=emotion_pred)
|
221 |
|
222 |
return generated_response, emotion_pred
|
|
|
240 |
submit_button.click(respond_wrapper, [context_input, conversation_history, emotion_history_state], [response_output, emotion_output, conversation_history, emotion_history_state])
|
241 |
|
242 |
demo.launch(share=True)
|
243 |
+
|
244 |
+
gc.collect()
|
245 |
+
torch.cuda.empty_cache()
|