Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -52,7 +52,10 @@ def get_finetuned_lm_model():
|
|
52 |
global _finetuned_lm_tokenizer, _finetuned_lm_model
|
53 |
if _finetuned_lm_tokenizer is None or _finetuned_lm_model is None:
|
54 |
_finetuned_lm_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
|
|
|
|
55 |
_finetuned_lm_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium", device_map="auto", low_cpu_mem_usage=True)
|
|
|
56 |
return _finetuned_lm_tokenizer, _finetuned_lm_model
|
57 |
|
58 |
# Enhanced Emotional States
|
@@ -168,16 +171,20 @@ def predict_emotion(context):
|
|
168 |
|
169 |
return emotion_mapping.get(predicted_emotion, 'neutral')
|
170 |
|
171 |
-
def generate_text(prompt, emotion=None, max_length=100):
|
172 |
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
if torch.cuda.is_available():
|
176 |
input_ids = input_ids.cuda()
|
177 |
finetuned_lm_model = finetuned_lm_model.cuda()
|
178 |
|
179 |
-
attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=input_ids.device)
|
180 |
-
|
181 |
# Set up the emotion-specific generation parameters
|
182 |
if emotion:
|
183 |
# You can adjust these parameters based on the emotion
|
|
|
52 |
global _finetuned_lm_tokenizer, _finetuned_lm_model
|
53 |
if _finetuned_lm_tokenizer is None or _finetuned_lm_model is None:
|
54 |
_finetuned_lm_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
55 |
+
_finetuned_lm_tokenizer.padding_side = 'left' # Set padding to left
|
56 |
+
_finetuned_lm_tokenizer.pad_token = _finetuned_lm_tokenizer.eos_token # Set pad token to eos token
|
57 |
_finetuned_lm_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium", device_map="auto", low_cpu_mem_usage=True)
|
58 |
+
_finetuned_lm_model.config.pad_token_id = _finetuned_lm_model.config.eos_token_id # Set pad token id in the model config
|
59 |
return _finetuned_lm_tokenizer, _finetuned_lm_model
|
60 |
|
61 |
# Enhanced Emotional States
|
|
|
171 |
|
172 |
return emotion_mapping.get(predicted_emotion, 'neutral')
|
173 |
|
174 |
+
def generate_text(prompt, chat_history, emotion=None, max_length=100):
|
175 |
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
|
176 |
+
|
177 |
+
full_prompt = ""
|
178 |
+
for turn in chat_history[-5:]: # Consider last 5 turns for context
|
179 |
+
full_prompt += f"{finetuned_lm_tokenizer.eos_token}{turn[0]}{finetuned_lm_tokenizer.eos_token}{turn[1]}"
|
180 |
+
full_prompt += f"{finetuned_lm_tokenizer.eos_token}{prompt}"
|
181 |
+
|
182 |
+
input_ids = finetuned_lm_tokenizer.encode(full_prompt, return_tensors='pt', truncation=True, max_length=1024)
|
183 |
|
184 |
if torch.cuda.is_available():
|
185 |
input_ids = input_ids.cuda()
|
186 |
finetuned_lm_model = finetuned_lm_model.cuda()
|
187 |
|
|
|
|
|
188 |
# Set up the emotion-specific generation parameters
|
189 |
if emotion:
|
190 |
# You can adjust these parameters based on the emotion
|