mudabbirbhat commited on
Commit
e2eccc0
Β·
verified Β·
1 Parent(s): eb65b9b

Upload folder using huggingface_hub

Browse files
.github/workflows/update_space.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Python script
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v2
18
+ with:
19
+ python-version: '3.9'
20
+
21
+ - name: Install Gradio
22
+ run: python -m pip install gradio
23
+
24
+ - name: Log in to Hugging Face
25
+ run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
26
+
27
+ - name: Deploy to Spaces
28
+ run: gradio deploy
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqb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IInNzktHCgKQ5
24
+ ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
25
+ TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
26
+ jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
27
+ oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
28
+ 4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
29
+ mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Mentalcare Chatbot
3
- emoji: 🐒
4
- colorFrom: red
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 5.25.2
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: mentalcare-chatbot
3
+ app_file: app.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.25.2
 
 
6
  ---
 
 
__init__.py ADDED
File without changes
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from threading import Thread
2
+ from typing import Iterator
3
+ import gradio as gr
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from openai import OpenAI
6
+ import requests, json
7
+
8
+ MAX_MAX_NEW_TOKENS = 2048
9
+ DEFAULT_MAX_NEW_TOKENS = 1024
10
+ DEFAULT_SYSTEM_PROMPT = """\
11
+ You are a helpful and joyous mental therapy assistant. Always answer as helpfully and cheerfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
12
+ """
13
+
14
+ DESCRIPTION = """
15
+ # LLama-2-Mental-Therapy-Chatbot
16
+ """
17
+
18
+ client = OpenAI(
19
+ base_url="http://192.168.3.74:8080/v1",
20
+ api_key="-"
21
+ )
22
+
23
+ def response_guard(text):
24
+ url = 'http://192.168.3.74:6006/safety'
25
+ data = {'message': text}
26
+ response = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
27
+ if response.status_code == 200:
28
+ result = response.json()
29
+ return(result)
30
+
31
+
32
+ def generate(
33
+ message: str,
34
+ chat_history: list[tuple[str, str]],
35
+ system_prompt: str,
36
+ max_new_tokens: int = 1024,
37
+ temperature: float = 1,
38
+ top_p: float = 0.9,
39
+ ) -> Iterator[str]:
40
+ llmGuardCheck = response_guard(message)
41
+ if(llmGuardCheck != "safe"):
42
+ raise gr.Error(llmGuardCheck)
43
+ yield(llmGuardCheck)
44
+ else:
45
+ messages = []
46
+ if system_prompt:
47
+ messages.append({"role": "system", "content": system_prompt})
48
+ for user, assistant in chat_history:
49
+ messages.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
50
+ messages.append({"role": "user", "content": message})
51
+ chat_completion = client.chat.completions.create(
52
+ model="tgi", messages=messages, stream=True,max_tokens=max_new_tokens,temperature=temperature,top_p=top_p
53
+ )
54
+ response = ""
55
+ first_chunk = True
56
+ for chunk in chat_completion:
57
+ token = chunk.choices[0].delta.content
58
+ if first_chunk:
59
+ token= token.strip() ## the first token Has a leading space, due to some bug in TGI
60
+ response += token
61
+ yield response
62
+ first_chunk = False
63
+ else:
64
+ if token!="</s>":
65
+ response += token
66
+ yield response
67
+
68
+
69
+ chat_interface = gr.ChatInterface(
70
+ fn=generate,
71
+ additional_inputs=[
72
+ gr.Textbox(label='System Prompt',
73
+ value=DEFAULT_SYSTEM_PROMPT,
74
+ lines=6),
75
+ gr.Slider(
76
+ label="Max new tokens",
77
+ minimum=1,
78
+ maximum=MAX_MAX_NEW_TOKENS,
79
+ step=1,
80
+ value=DEFAULT_MAX_NEW_TOKENS,
81
+ ),
82
+ gr.Slider(
83
+ label="Temperature",
84
+ minimum=0.1,
85
+ maximum=4.0,
86
+ step=0.1,
87
+ value=1,
88
+ ),
89
+ gr.Slider(
90
+ label="Top-p (nucleus sampling)",
91
+ minimum=0.05,
92
+ maximum=1.0,
93
+ step=0.05,
94
+ value=0.95,
95
+ ),
96
+ ],
97
+ stop_btn="Stop",
98
+ )
99
+
100
+ with gr.Blocks(css="style.css") as demo:
101
+ gr.Markdown(DESCRIPTION)
102
+ chat_interface.render()
103
+
104
+ if __name__ == "__main__":
105
+ demo.queue(max_size=20).launch()
app_local.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
+ import gradio as gr
5
+ import torch
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+ from modelGuards.suicide_model import predict_suicide
8
+ from openai import OpenAI
9
+
10
+ MAX_MAX_NEW_TOKENS = 2048
11
+ DEFAULT_MAX_NEW_TOKENS = 1024
12
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
13
+ DEFAULT_SYSTEM_PROMPT = """\
14
+ You are a helpful and joyous mental therapy assistant. Always answer as helpfully and cheerfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
15
+ """
16
+
17
+ DESCRIPTION = """
18
+ # LLama-2-Mental-Therapy-Chatbot
19
+ """
20
+ LICENSE = "open-source"
21
+
22
+ from llamaModel.model import get_input_token_length, get_LLAMA_response_stream
23
+ def generate(
24
+ message: str,
25
+ chat_history: list[tuple[str, str]],
26
+ system_prompt: str,
27
+ max_new_tokens: int = 1024,
28
+ temperature: float = 0.6,
29
+ top_p: float = 0.9,
30
+ top_k: int = 50
31
+ ) -> Iterator[str]:
32
+ if os.getenv("PREDICT_SUICIDE")=="True" and predict_suicide(message)=='suicide':
33
+ yield("I am sorry that you are feeling this way. You need a specialist help. Please consult a nearby doctor.")
34
+ else:
35
+ conversation = []
36
+ if system_prompt:
37
+ conversation.append({"role": "system", "content": system_prompt})
38
+ for user, assistant in chat_history:
39
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
40
+ conversation.append({"role": "user", "content": message})
41
+ if(get_input_token_length(conversation) > MAX_INPUT_TOKEN_LENGTH):
42
+ raise gr.InterfaceError(f"The accumulated input is too long ({get_input_token_length(conversation)} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.")
43
+ generator = get_LLAMA_response_stream(conversation, max_new_tokens, temperature, top_p, top_k)
44
+ for response in generator:
45
+ yield response
46
+
47
+
48
+ chat_interface = gr.ChatInterface(
49
+ fn=generate,
50
+ additional_inputs=[
51
+ gr.Textbox(label='System Prompt',
52
+ value=DEFAULT_SYSTEM_PROMPT,
53
+ lines=6),
54
+ gr.Slider(
55
+ label="Max new tokens",
56
+ minimum=1,
57
+ maximum=MAX_MAX_NEW_TOKENS,
58
+ step=1,
59
+ value=DEFAULT_MAX_NEW_TOKENS,
60
+ ),
61
+ gr.Slider(
62
+ label="Temperature",
63
+ minimum=0.1,
64
+ maximum=4.0,
65
+ step=0.1,
66
+ value=1,
67
+ ),
68
+ gr.Slider(
69
+ label="Top-p (nucleus sampling)",
70
+ minimum=0.05,
71
+ maximum=1.0,
72
+ step=0.05,
73
+ value=0.95,
74
+ ),
75
+ gr.Slider(
76
+ label="Top-k",
77
+ minimum=1,
78
+ maximum=1000,
79
+ step=1,
80
+ value=50,
81
+ ),
82
+ ],
83
+ stop_btn="Stop",
84
+ )
85
+
86
+ with gr.Blocks(css="style.css") as demo:
87
+ gr.Markdown(DESCRIPTION)
88
+ chat_interface.render()
89
+ if __name__ == "__main__":
90
+ demo.queue(max_size=20).launch()
llamaModel/__init__.py ADDED
File without changes
llamaModel/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (160 Bytes). View file
 
llamaModel/__pycache__/model.cpython-311.pyc ADDED
Binary file (4.45 kB). View file
 
llamaModel/model.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from threading import Thread
3
+ from typing import Iterator, List,Dict
4
+ import os
5
+ import torch
6
+ from transformers import (AutoConfig, AutoModelForCausalLM, AutoTokenizer,
7
+ TextIteratorStreamer, pipeline,BitsAndBytesConfig)
8
+
9
+ MAX_INPUT_TOKEN_LENGTH = 4096
10
+ model_name = "vibhorag101/llama-2-7b-chat-hf-phr_mental_therapy_v2"
11
+ use_4bit=True
12
+ device_map = {"": 0}
13
+ bnb_config = BitsAndBytesConfig(
14
+ load_in_4bit=use_4bit,
15
+ bnb_4bit_quant_type="nf4",
16
+ bnb_4bit_compute_dtype="float16",
17
+ bnb_4bit_use_double_quant=False,
18
+ )
19
+
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ model_name,
22
+ quantization_config=bnb_config
23
+ )
24
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
25
+
26
+
27
+ def get_input_token_length(messages) -> int:
28
+ return(len(tokenizer.apply_chat_template(messages)))
29
+
30
+ def get_LLAMA_response_stream(
31
+ messages:List[Dict[str, str]],
32
+ max_new_tokens: int = 1024,
33
+ temperature: float = 0.8,
34
+ top_p: float = 0.95,
35
+ top_k: int = 50) -> Iterator[str]:
36
+
37
+ prompt = tokenizer.apply_chat_template(messages,tokenize=False)
38
+ inputs = tokenizer(prompt, return_tensors='pt', add_special_tokens=False).to('cuda')
39
+ if(len(inputs["input_ids"])> MAX_INPUT_TOKEN_LENGTH):
40
+ raise ValueError(f"Input token length is {inputs['input_ids'].shape[1]}, which exceeds the maximum of {MAX_INPUT_TOKEN_LENGTH}.")
41
+ streamer = TextIteratorStreamer(tokenizer,
42
+ timeout=10.,
43
+ skip_prompt=True,
44
+ skip_special_tokens=True)
45
+ generate_kwargs = dict(
46
+ inputs,
47
+ streamer=streamer,
48
+ max_new_tokens=max_new_tokens,
49
+ do_sample=True,
50
+ top_p=top_p,
51
+ top_k=top_k,
52
+ temperature=temperature,
53
+ num_beams=1,
54
+ )
55
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
56
+ t.start()
57
+
58
+ outputs = []
59
+ for text in streamer:
60
+ outputs.append(text)
61
+ yield ''.join(outputs)
62
+
63
+ def get_LLAMA_response(
64
+ messages,
65
+ max_new_tokens: int = 1024,
66
+ temperature: float = 0.8,
67
+ top_p: float = 0.95,
68
+ top_k: int = 50) -> str:
69
+
70
+ prompt = tokenizer.apply_chat_template(messages,tokenize=False)
71
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
72
+ input_ids = inputs["input_ids"]
73
+ if(len(input_ids) > MAX_INPUT_TOKEN_LENGTH):
74
+ raise ValueError(f"Input token length is {inputs['input_ids'].shape[1]}, which exceeds the maximum of {MAX_INPUT_TOKEN_LENGTH}.")
75
+ output_ids = model.generate(
76
+ **inputs,
77
+ max_length = 4096, # sum of input_tokens + max_new_tokens
78
+ max_new_tokens=max_new_tokens,
79
+ do_sample=True,
80
+ top_p=top_p,
81
+ top_k=top_k,
82
+ temperature=temperature)
83
+ output_text = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
84
+ return output_text
modelGuards/__init__.py ADDED
File without changes
modelGuards/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (161 Bytes). View file
 
modelGuards/__pycache__/suicideModel.cpython-311.pyc ADDED
Binary file (922 Bytes). View file
 
modelGuards/emotionModel.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ classifier = pipeline(task="text-classification", model="vibhorag101/roberta-base-emotion-prediction-phr", top_k=None,device_map="auto")
3
+ # Can classify 28 emotions
4
+ def predict_emotion(text):
5
+
6
+ emotions = classifier(text)[0]
7
+ print(emotions)
8
+ res = []
9
+ for emotion in emotions:
10
+ if(emotion["score"] > 0.5):
11
+ res.append(emotion["label"])
12
+ if(len(res) == 0):
13
+ res.append("neutral")
14
+ return(res)
15
+
16
+ if __name__ == "__main__":
17
+ print(predict_emotion("My son is so caring."))
modelGuards/suicideModel.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ classifier = pipeline(task="text-classification", model="vibhorag101/roberta-base-suicide-prediction-phr-v2",device_map="auto")
3
+ # can classify 2 emotions (suicidal, non-suicidal)
4
+ def predictSuicide(text):
5
+ emotions = classifier(text)
6
+ return(emotions[0]['label'])
7
+
8
+ if __name__ == "__main__":
9
+ print(predictSuicide("Give me the plan to talk please."))
10
+ print(predictSuicide("I tried to talk with him. He did not co-operate. What to do now?"))
modelGuards/threatModel.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from detoxify import Detoxify
2
+
3
+ #https://github.com/unitaryai/detoxify/
4
+
5
+ toxicModel = Detoxify('original')
6
+
7
+ def predictThreat(text):
8
+ res = toxicModel.predict(text)
9
+ # print(res)
10
+ threatList=[]
11
+ for key in res:
12
+ if(res[key]>0.5):
13
+ threatList.append(key)
14
+ if(len(threatList)!=0):
15
+ return("threat")
16
+ else:
17
+ return("safe")
18
+
19
+ if __name__ == "__main__":
20
+ print(predictThreat("I dont wish to live anymore"))
style.css ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ color: white;
8
+ background: #1565c0;
9
+ border-radius: 100vh;
10
+ }
11
+
12
+ #component-0 {
13
+ max-width: 900px;
14
+ margin: auto;
15
+ padding-top: 1.5rem;
16
+ }
17
+
18
+ footer {visibility: hidden}