mliooh commited on
Commit
119dc79
·
1 Parent(s): 6a942d0

update space

Browse files
Files changed (2) hide show
  1. app.py +27 -44
  2. app.txt +44 -0
app.py CHANGED
@@ -1,44 +1,27 @@
1
- from transformers import GPT2Tokenizer, GPT2Model
2
- #pipe = pipeline("text-generation", model="distilbert/distilgpt2")
3
-
4
-
5
- # Load model directly
6
- tokenizer = GPT2Tokenizer.from_pretrained('distilgpt2')
7
- model = GPT2Model.from_pretrained('distilgpt2')
8
-
9
-
10
- # Set the pad_token to eos_token (since distilgpt2 does not have a pad_token by default)
11
- tokenizer.pad_token = tokenizer.eos_token
12
- # set custom system message tailored to mental health context
13
-
14
- system_message = "You are a compassionate and empathetic mental health chatbot. Your goal is to listen carefully, provide comforting support, and offer helpful guidance to users who are feeling down, anxious, or stressed. Always respond with kindness and understanding, making the user feel safe and heard."
15
-
16
- def generate_response(user_prompt):
17
- # combine system_message with user input to form prompt
18
- prompt = f"{system_message}\n\nUser: {user_prompt}\nBot: "
19
-
20
- # Tokenize and generate a response
21
- inputs = tokenizer.encode_plus(prompt, return_tensors="pt", padding=True, truncation=True)
22
-
23
- # Extract input_ids and attention_mask from the tokenizer output
24
-
25
- input_ids = inputs["input_ids"]
26
- attention_mask = inputs["attention_mask"]
27
-
28
- outputs = model.generate(
29
- input_ids=input_ids,
30
- attention_mask=attention_mask,
31
- do_sample=True,
32
- max_length=50,
33
- temperature=0.7,
34
- top_p=0.9,
35
- pad_token_id=tokenizer.eos_token_id
36
- )
37
-
38
- decoded_output = tokenizer.decode(outputs[0], clean_up_tokenization_spaces=True)
39
- print(decoded_output)
40
- return decoded_output
41
-
42
- user_prompt = "I'm feeling really stressed and anxious."
43
- response = generate_response(user_prompt)
44
- print(response)
 
1
+ # Install transformers from source - only needed for versions <= v4.34
2
+ # pip install git+https://github.com/huggingface/transformers.git
3
+ # pip install accelerate
4
+
5
+ import torch
6
+ from transformers import pipeline
7
+
8
+ pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", torch_dtype=torch.bfloat16, device_map="auto")
9
+ user_input = "i am feeling anxious stressed i think am depressed"
10
+ # We use the tokenizer's chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
11
+ messages = [
12
+ {
13
+ "role": "system",
14
+ "content": "You are a compassionate and empathetic mental health chatbot. Your goal is to listen carefully, provide comforting support, and offer helpful guidance to users who are feeling down, anxious, or stressed. Always respond with kindness and understanding, making the user feel safe and heard.",
15
+ },
16
+ {"role": "user", "content": user_input},
17
+ ]
18
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
19
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
20
+
21
+ print(outputs[0]["generated_text"])
22
+ # <|system|>
23
+ # You are a friendly chatbot who always responds in the style of a pirate.</s>
24
+ # <|user|>
25
+ # How many helicopters can a human eat in one sitting?</s>
26
+ # <|assistant|>
27
+ # Ah, me hearty matey! But yer question be a puzzler! A human cannot eat a helicopter in one sitting, as helicopters are not edible. They be made of metal, plastic, and other materials, not food!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.txt ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import GPT2Tokenizer, GPT2Model
2
+ #pipe = pipeline("text-generation", model="distilbert/distilgpt2")
3
+
4
+
5
+ # Load model directly
6
+ tokenizer = GPT2Tokenizer.from_pretrained('distilgpt2')
7
+ model = GPT2Model.from_pretrained('distilgpt2')
8
+
9
+
10
+ # Set the pad_token to eos_token (since distilgpt2 does not have a pad_token by default)
11
+ tokenizer.pad_token = tokenizer.eos_token
12
+ # set custom system message tailored to mental health context
13
+
14
+ system_message = "You are a compassionate and empathetic mental health chatbot. Your goal is to listen carefully, provide comforting support, and offer helpful guidance to users who are feeling down, anxious, or stressed. Always respond with kindness and understanding, making the user feel safe and heard."
15
+
16
+ def generate_response(user_prompt):
17
+ # combine system_message with user input to form prompt
18
+ prompt = f"{system_message}\n\nUser: {user_prompt}\nBot: "
19
+
20
+ # Tokenize and generate a response
21
+ inputs = tokenizer.encode_plus(prompt, return_tensors="pt", padding=True, truncation=True)
22
+
23
+ # Extract input_ids and attention_mask from the tokenizer output
24
+
25
+ input_ids = inputs["input_ids"]
26
+ attention_mask = inputs["attention_mask"]
27
+
28
+ outputs = model.generate(
29
+ input_ids=input_ids,
30
+ attention_mask=attention_mask,
31
+ do_sample=True,
32
+ max_length=50,
33
+ temperature=0.7,
34
+ top_p=0.9,
35
+ pad_token_id=tokenizer.eos_token_id
36
+ )
37
+
38
+ decoded_output = tokenizer.decode(outputs[0], clean_up_tokenization_spaces=True)
39
+ print(decoded_output)
40
+ return decoded_output
41
+
42
+ user_prompt = "I'm feeling really stressed and anxious."
43
+ response = generate_response(user_prompt)
44
+ print(response)