Spaces:
Sleeping
Sleeping
File size: 1,149 Bytes
e90e67f c7d0538 530fb5f 102bc86 e90e67f 102bc86 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_NAME = "microsoft/Phi-3.5-mini-instruct"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
device_map="auto",
)
model.eval()
def generate_support_message(text: str, emotion: str) -> str:
prompt = (
f"The user is feeling {emotion}.\n"
f'User message: "{text}"\n\n'
"Write a short, empathetic, and supportive response. "
"Do not give medical advice."
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs, max_new_tokens=80, do_sample=True, temperature=0.7
)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
if "Assistant:" in decoded:
response = decoded.split("Assistant:")[-1].split("User message:")[0].strip()
else:
response = decoded.strip()
return response
|