Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,32 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
|
4 |
-
# Load model
|
5 |
-
model_name = "
|
6 |
-
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
response = nlp(user_input, max_length=150, num_return_sequences=1)
|
12 |
-
return response[0]['generated_text']
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
|
4 |
+
# Load the model and tokenizer
|
5 |
+
model_name = "tanusrich/Mental_Health_Chatbot"
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
+
# Move the model to the appropriate device (CPU or GPU)
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
model.to(device)
|
|
|
|
|
12 |
|
13 |
+
# Generate a response
|
14 |
+
def generate_response(user_input):
|
15 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
16 |
+
with torch.no_grad():
|
17 |
+
output = model.generate(
|
18 |
+
**inputs,
|
19 |
+
max_new_tokens=200,
|
20 |
+
temperature=0.7,
|
21 |
+
top_k=50,
|
22 |
+
top_p=0.9,
|
23 |
+
repetition_penalty=1.2,
|
24 |
+
pad_token_id=tokenizer.eos_token_id
|
25 |
+
)
|
26 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
27 |
+
return response
|
28 |
|
29 |
+
# Example interaction
|
30 |
+
user_input = "I'm feeling lonely and anxious. What can I do?"
|
31 |
+
response = generate_response(user_input)
|
32 |
+
print("Chatbot: ", response)
|