Sephfox commited on
Commit
b7412b9
·
verified ·
1 Parent(s): b58817e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -43
app.py CHANGED
@@ -155,51 +155,68 @@ 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 = predictions[0]
159
- emotion_pred = emotion_classes[np.argmax(emotion_scores)]
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').to(finetuned_lm_model.device)
165
-
166
- if emotion is not None:
167
- emotion_intensity = emotions[emotion]['intensity']
168
- top_p = 0.95 - (emotion_intensity / 10) # Adjust top_p based on emotion intensity
169
- temperature = 0.7 + (emotion_intensity / 5) # Adjust temperature based on emotion intensity
170
- else:
171
- top_p = 0.95
172
- temperature = 0.7
173
-
174
- with torch.no_grad():
175
- output = finetuned_lm_model.generate(
176
- input_ids,
177
- max_length=max_length,
178
- num_return_sequences=1,
179
- no_repeat_ngram_size=2,
180
- do_sample=True,
181
- top_k=50,
182
- top_p=top_p,
183
- temperature=temperature
184
- )
185
- generated_text = finetuned_lm_tokenizer.decode(output[0], skip_special_tokens=True)
186
- return generated_text
187
-
188
- def generate_response(context, emotion=None):
189
- prompt = context
190
- generated_text = generate_text(prompt, emotion=emotion)
191
- return generated_text
192
-
193
- with gr.Blocks() as demo:
194
- gr.Markdown("# Emotion-Aware Language Model")
195
-
196
- context_input = gr.Textbox(label="Enter a context")
197
- predict_btn = gr.Button("Predict Emotion and Generate Text")
198
 
199
- with gr.Row():
200
- emotion_output = gr.Textbox(label="Predicted Emotion", show_label=True)
201
- generated_text_output = gr.Textbox(label="Generated Text", show_label=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
- predict_btn.click(fn=lambda context: (predict_emotion(context), generate_response(context, emotion=predict_emotion(context))), inputs=context_input, outputs=[emotion_output, generated_text_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
- demo.launch()
 
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 = {emotion['label']: emotion['score'] for emotion in predictions}
159
+ emotion_pred = emotion_classes[np.argmax([emotion_scores.get(label, 0) for label in emotion_classes])]
160
  return emotion_pred
161
 
162
+ def generate_text(prompt, model, tokenizer, max_length=50):
163
+ input_ids = tokenizer.encode(prompt, return_tensors='pt')
164
+ output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
165
+ return tokenizer.decode(output[0], skip_special_tokens=True)
166
+
167
+ def optimize_ai_model(emotion_history):
168
+ X = [normalize_context(e['context']) for e in emotion_history]
169
+ y = [e['emotion'] for e in emotion_history]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
172
+
173
+ rf = RandomForestClassifier(n_estimators=100, random_state=42)
174
+ rf.fit(X_train, y_train)
175
+ score = rf.score(X_test, y_test)
176
+ return rf, score
177
+
178
+ def ai_response(input_text, emotion_model):
179
+ emotion_pred = predict_emotion(input_text)
180
+ context_emotion = {'context': input_text, 'emotion': emotion_pred}
181
+ emotion_history.append(context_emotion)
182
+ save_historical_data(emotion_history)
183
+
184
+ percentage_change = random.randint(1, 10)
185
+ intensity_change = random.randint(1, 10)
186
+ update_emotion(emotion_pred, percentage_change, intensity_change)
187
+
188
+ evolve_emotions()
189
 
190
+ finetuned_tokenizer, finetuned_model = get_finetuned_lm_model()
191
+ response_text = generate_text(input_text, finetuned_model, finetuned_tokenizer)
192
+
193
+ return response_text
194
+
195
+ def run_genetic_algorithm_parallel():
196
+ num_cores = mp.cpu_count()
197
+ results = Parallel(n_jobs=num_cores)(delayed(evolve_emotions)() for _ in range(3))
198
+
199
+ def clear_emotion_history():
200
+ global emotion_history
201
+ emotion_history = []
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
+ with gr.Blocks() as app:
211
+ gr.Markdown("# Advanced AI Model")
212
+ input_text = gr.Textbox(label="Input Text")
213
+ response_text = gr.Textbox(label="Response Text", interactive=False)
214
+ model_score = gr.Number(label="Model Accuracy", interactive=False)
215
+
216
+ respond_button = gr.Button("Respond")
217
+ respond_button.click(fn=respond, inputs=[input_text], outputs=[response_text, model_score])
218
+
219
+ clear_button = gr.Button("Clear Emotion History")
220
+ clear_button.click(fn=clear_emotion_history, inputs=[], outputs=[])
221
 
222
+ app.launch()