Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -155,68 +155,94 @@ def evolve_emotions():
|
|
155 |
def predict_emotion(context):
|
156 |
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
|
157 |
predictions = emotion_prediction_pipeline(context)
|
158 |
-
emotion_scores = {
|
159 |
-
emotion_pred =
|
160 |
return emotion_pred
|
161 |
|
162 |
-
def generate_text(prompt,
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
def optimize_ai_model(emotion_history):
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
score = rf.score(X_test, y_test)
|
176 |
-
return rf, score
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
emotion_history.append(context_emotion)
|
182 |
-
save_historical_data(emotion_history)
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
update_emotion(emotion_pred, percentage_change, intensity_change)
|
187 |
|
188 |
-
|
189 |
|
190 |
-
|
191 |
-
|
192 |
|
193 |
-
|
|
|
194 |
|
195 |
-
def
|
196 |
-
|
197 |
-
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
202 |
save_historical_data(emotion_history)
|
203 |
|
204 |
-
# Gradio Interface
|
205 |
-
def respond(input_text):
|
206 |
emotion_model, score = optimize_ai_model(emotion_history)
|
207 |
-
response = ai_response(input_text, emotion_model)
|
208 |
-
return response, score
|
209 |
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
215 |
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
-
|
220 |
-
|
221 |
|
222 |
-
|
|
|
155 |
def predict_emotion(context):
|
156 |
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
|
157 |
predictions = emotion_prediction_pipeline(context)
|
158 |
+
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions}
|
159 |
+
emotion_pred = max(emotion_scores, key=emotion_scores.get)
|
160 |
return emotion_pred
|
161 |
|
162 |
+
def generate_text(prompt, max_length=100, emotion=None):
|
163 |
+
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
|
164 |
+
input_ids = finetuned_lm_tokenizer.encode(prompt, return_tensors='pt')
|
165 |
+
attention_mask = torch.ones(input_ids.shape, dtype=torch.long)
|
166 |
+
|
167 |
+
if torch.cuda.is_available():
|
168 |
+
input_ids = input_ids.cuda()
|
169 |
+
attention_mask = attention_mask.cuda()
|
170 |
+
finetuned_lm_model = finetuned_lm_model.cuda()
|
171 |
+
|
172 |
+
if emotion:
|
173 |
+
emotion_token = emotion_prediction_tokenizer.encode(emotion, add_special_tokens=False)
|
174 |
+
input_ids = torch.cat((input_ids, torch.tensor(emotion_token).unsqueeze(0)), dim=1)
|
175 |
+
|
176 |
+
outputs = finetuned_lm_model.generate(
|
177 |
+
input_ids=input_ids,
|
178 |
+
attention_mask=attention_mask,
|
179 |
+
max_length=max_length,
|
180 |
+
pad_token_id=finetuned_lm_tokenizer.eos_token_id,
|
181 |
+
num_return_sequences=1,
|
182 |
+
temperature=0.7,
|
183 |
+
top_p=0.9,
|
184 |
+
do_sample=True
|
185 |
+
)
|
186 |
+
|
187 |
+
return finetuned_lm_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
188 |
|
189 |
def optimize_ai_model(emotion_history):
|
190 |
+
if not emotion_history:
|
191 |
+
return None, None
|
|
|
|
|
192 |
|
193 |
+
contexts = [entry['context'] for entry in emotion_history]
|
194 |
+
emotions = [entry['emotion'] for entry in emotion_history]
|
|
|
|
|
195 |
|
196 |
+
encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
|
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)
|
206 |
+
classifier.fit(X_train, y_train)
|
207 |
|
208 |
+
score = classifier.score(X_test, y_test)
|
209 |
+
return classifier, score
|
210 |
|
211 |
+
def respond(context, previous_responses, previous_emotions):
|
212 |
+
normalized_context = normalize_context(context)
|
213 |
+
emotion_pred = predict_emotion(normalized_context)
|
214 |
|
215 |
+
emotion_history.append({
|
216 |
+
'context': normalized_context,
|
217 |
+
'emotion': emotion_pred
|
218 |
+
})
|
219 |
save_historical_data(emotion_history)
|
220 |
|
|
|
|
|
221 |
emotion_model, score = optimize_ai_model(emotion_history)
|
|
|
|
|
222 |
|
223 |
+
evolve_emotions()
|
224 |
+
|
225 |
+
response_prompt = f"{normalized_context}\n\n{previous_responses}\n\n{emotion_pred}"
|
226 |
+
generated_response = generate_text(response_prompt, emotion=emotion_pred)
|
227 |
+
|
228 |
+
return generated_response, emotion_pred
|
229 |
|
230 |
+
# GUI with Gradio
|
231 |
+
with gr.Blocks() as demo:
|
232 |
+
context_input = gr.Textbox(label="Context")
|
233 |
+
response_output = gr.Textbox(label="Generated Response")
|
234 |
+
emotion_output = gr.Textbox(label="Detected Emotion")
|
235 |
+
|
236 |
+
conversation_history = gr.State([])
|
237 |
+
emotion_history_state = gr.State([])
|
238 |
+
|
239 |
+
def respond_wrapper(context, conversation_history, emotion_history_state):
|
240 |
+
response, emotion = respond(context, conversation_history, emotion_history_state)
|
241 |
+
conversation_history.append(response)
|
242 |
+
emotion_history_state.append(emotion)
|
243 |
+
return response, emotion, conversation_history, emotion_history_state
|
244 |
|
245 |
+
submit_button = gr.Button("Submit")
|
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)
|