File size: 1,251 Bytes
9bdc1f1
f856069
 
32a5ab7
8430ee5
7f1a28e
 
83c726d
 
f856069
83c726d
 
 
 
32a5ab7
8430ee5
7f1a28e
32a5ab7
8430ee5
f856069
4839250
32a5ab7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def generate_response(user_input):
    # Empathy-focused prompt to guide the bot
    response_prompt = f"The user has shared the following: '{user_input}'. Respond with empathy, compassion, and understanding. Acknowledge their sadness and offer comforting, reassuring words. Show that you care and validate their feelings without giving unsolicited advice."
    
    # Generate the response using the GPT-2 model
    input_ids = gpt2_tokenizer.encode(response_prompt, return_tensors='pt')
    response_ids = gpt2_model.generate(
        input_ids,
        max_length=300,
        temperature=0.85,
        top_k=50,
        repetition_penalty=1.2,
        num_return_sequences=1
    )
    
    # Decode the response and clean it up by removing the prompt
    response = gpt2_tokenizer.decode(response_ids[0], skip_special_tokens=True)
    
    # Strip out the prompt portion to get a clean, empathetic message
    cleaned_response = response.replace(f"The user has shared the following: '{user_input}'. Respond with empathy, compassion, and understanding. Acknowledge their sadness and offer comforting, reassuring words. Show that you care and validate their feelings without giving unsolicited advice.", "").strip()
    
    return cleaned_response