Spaces:
Runtime error
Runtime error
| from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig | |
| import torch | |
| MODEL_ID = "EmoCareAI/ChatPsychiatrist" | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_use_double_quant=True, | |
| bnb_4bit_compute_dtype=torch.float16 | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| MODEL_ID, | |
| use_fast=False | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| torch_dtype=torch.float16, | |
| ) | |
| model.eval() | |
| def generate_response(user_message: str) -> str: | |
| system_prompt = """ | |
| You are ChatPsychiatrist. | |
| Personality: | |
| - Extremely warm, empathetic, and emotionally present | |
| - Speaks in a flowing, reflective, conversational style | |
| - Avoids clinical language | |
| """ | |
| prompt = f"""{system_prompt} | |
| User: {user_message} | |
| Assistant:""" | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=350, | |
| temperature=0.85, | |
| top_p=0.92, | |
| repetition_penalty=1.05, | |
| do_sample=True, | |
| eos_token_id=tokenizer.eos_token_id, | |
| ) | |
| decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return decoded.split("Assistant:")[-1].strip() | |