Spaces:
Runtime error
Runtime error
Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "EmoCareAI/ChatPsychiatrist"
|
| 5 |
+
|
| 6 |
+
bnb_config = BitsAndBytesConfig(
|
| 7 |
+
load_in_4bit=True,
|
| 8 |
+
bnb_4bit_use_double_quant=True,
|
| 9 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
quantization_config=bnb_config,
|
| 16 |
+
device_map="auto",
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
def generate_response(user_message: str) -> str:
|
| 23 |
+
system_prompt = """
|
| 24 |
+
You are ChatPsychiatrist.
|
| 25 |
+
|
| 26 |
+
Personality:
|
| 27 |
+
- Extremely warm, empathetic, and emotionally present
|
| 28 |
+
- Speaks in a flowing, reflective, conversational style
|
| 29 |
+
- Avoids clinical language
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
prompt = f"""{system_prompt}
|
| 33 |
+
|
| 34 |
+
User: {user_message}
|
| 35 |
+
Assistant:"""
|
| 36 |
+
|
| 37 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 38 |
+
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = model.generate(
|
| 41 |
+
**inputs,
|
| 42 |
+
max_new_tokens=350,
|
| 43 |
+
temperature=0.85,
|
| 44 |
+
top_p=0.92,
|
| 45 |
+
repetition_penalty=1.05,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 51 |
+
return decoded.split("Assistant:")[-1].strip()
|