Update app.py
Browse files
app.py
CHANGED
@@ -1,142 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""gradio_chatbot_app.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colab.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/#fileId=https%3A//huggingface.co/spaces/ZamiSanj/therapx/blob/main/gradio_chatbot_app.ipynb
|
8 |
-
"""
|
9 |
-
|
10 |
-
# !pip install gradio==3.41.0 transformers==4.32.0 langchain==0.0.273 -Uqqq
|
11 |
-
# !pip install accelerate bitsandbytes==0.41.1 einops==0.7.0 peft==0.4.0 -Uqqq
|
12 |
-
|
13 |
-
import gradio as gr
|
14 |
-
import torch
|
15 |
-
import re, os, warnings
|
16 |
-
from langchain import PromptTemplate, LLMChain
|
17 |
-
from langchain.llms.base import LLM
|
18 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, GenerationConfig
|
19 |
-
from peft import LoraConfig, get_peft_model, PeftConfig, PeftModel
|
20 |
-
warnings.filterwarnings("ignore")
|
21 |
-
|
22 |
-
# initialize and load PEFT model and tokenizer
|
23 |
-
def init_model_and_tokenizer(PEFT_MODEL):
|
24 |
-
config = PeftConfig.from_pretrained(PEFT_MODEL)
|
25 |
-
bnb_config = BitsAndBytesConfig(
|
26 |
-
load_in_4bit=True,
|
27 |
-
bnb_4bit_quant_type="nf4",
|
28 |
-
bnb_4bit_use_double_quant=True,
|
29 |
-
bnb_4bit_compute_dtype=torch.float16,
|
30 |
-
)
|
31 |
-
|
32 |
-
peft_base_model = AutoModelForCausalLM.from_pretrained(
|
33 |
-
config.base_model_name_or_path,
|
34 |
-
return_dict=True,
|
35 |
-
quantization_config=bnb_config,
|
36 |
-
device_map="auto",
|
37 |
-
trust_remote_code=True,
|
38 |
-
)
|
39 |
-
|
40 |
-
peft_model = PeftModel.from_pretrained(peft_base_model, PEFT_MODEL)
|
41 |
-
|
42 |
-
peft_tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
43 |
-
peft_tokenizer.pad_token = peft_tokenizer.eos_token
|
44 |
-
|
45 |
-
return peft_model, peft_tokenizer
|
46 |
-
|
47 |
-
# custom LLM chain to generate answer from PEFT model for each query
|
48 |
-
def init_llm_chain(peft_model, peft_tokenizer):
|
49 |
-
class CustomLLM(LLM):
|
50 |
-
def _call(self, prompt: str, stop=None, run_manager=None) -> str:
|
51 |
-
device = "cuda:0"
|
52 |
-
peft_encoding = peft_tokenizer(prompt, return_tensors="pt").to(device)
|
53 |
-
peft_outputs = peft_model.generate(input_ids=peft_encoding.input_ids, generation_config=GenerationConfig(max_new_tokens=256, pad_token_id = peft_tokenizer.eos_token_id, \
|
54 |
-
eos_token_id = peft_tokenizer.eos_token_id, attention_mask = peft_encoding.attention_mask, \
|
55 |
-
temperature=0.4, top_p=0.6, repetition_penalty=1.3, num_return_sequences=1,))
|
56 |
-
peft_text_output = peft_tokenizer.decode(peft_outputs[0], skip_special_tokens=True)
|
57 |
-
return peft_text_output
|
58 |
-
|
59 |
-
@property
|
60 |
-
def _llm_type(self) -> str:
|
61 |
-
return "custom"
|
62 |
-
|
63 |
-
llm = CustomLLM()
|
64 |
-
|
65 |
-
template = """Answer the following question truthfully.
|
66 |
-
If you don't know the answer, respond 'Sorry, I don't know the answer to this question.'.
|
67 |
-
If the question is too complex, respond 'Kindly, consult a psychiatrist for further queries.'.
|
68 |
-
|
69 |
-
Example Format:
|
70 |
-
<HUMAN>: question here
|
71 |
-
<ASSISTANT>: answer here
|
72 |
-
|
73 |
-
Begin!
|
74 |
-
|
75 |
-
<HUMAN>: {query}
|
76 |
-
<ASSISTANT>:"""
|
77 |
-
|
78 |
-
prompt = PromptTemplate(template=template, input_variables=["query"])
|
79 |
-
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
80 |
-
|
81 |
-
return llm_chain
|
82 |
-
|
83 |
-
def user(user_message, history):
|
84 |
-
return "", history + [[user_message, None]]
|
85 |
-
|
86 |
-
def bot(history):
|
87 |
-
if len(history) >= 2:
|
88 |
-
query = history[-2][0] + "\n" + history[-2][1] + "\nHere, is the next QUESTION: " + history[-1][0]
|
89 |
-
else:
|
90 |
-
query = history[-1][0]
|
91 |
-
|
92 |
-
bot_message = llm_chain.run(query)
|
93 |
-
bot_message = post_process_chat(bot_message)
|
94 |
-
|
95 |
-
history[-1][1] = ""
|
96 |
-
history[-1][1] += bot_message
|
97 |
-
return history
|
98 |
-
|
99 |
-
def post_process_chat(bot_message):
|
100 |
-
try:
|
101 |
-
bot_message = re.findall(r"<ASSISTANT>:.*?Begin!", bot_message, re.DOTALL)[1]
|
102 |
-
except IndexError:
|
103 |
-
pass
|
104 |
-
|
105 |
-
bot_message = re.split(r'<ASSISTANT>\:?\s?', bot_message)[-1].split("Begin!")[0]
|
106 |
-
|
107 |
-
bot_message = re.sub(r"^(.*?\.)(?=\n|$)", r"\1", bot_message, flags=re.DOTALL)
|
108 |
-
try:
|
109 |
-
bot_message = re.search(r"(.*\.)", bot_message, re.DOTALL).group(1)
|
110 |
-
except AttributeError:
|
111 |
-
pass
|
112 |
-
|
113 |
-
bot_message = re.sub(r"\n\d.$", "", bot_message)
|
114 |
-
bot_message = re.split(r"(Goodbye|Take care|Best Wishes)", bot_message, flags=re.IGNORECASE)[0].strip()
|
115 |
-
bot_message = bot_message.replace("\n\n", "\n")
|
116 |
-
|
117 |
-
return bot_message
|
118 |
-
|
119 |
-
model = "heliosbrahma/falcon-7b-sharded-bf16-finetuned-mental-health-conversational"
|
120 |
-
peft_model, peft_tokenizer = init_model_and_tokenizer(PEFT_MODEL = model)
|
121 |
-
|
122 |
-
with gr.Blocks() as demo:
|
123 |
-
gr.HTML("""<h1>TherapX</h1>""")
|
124 |
-
gr.Markdown(
|
125 |
-
"""Chatbot specifically designed to provide psychoeducation, offer non-judgemental and empathetic support, self-assessment and monitoring.<br>
|
126 |
-
Get instant response for any mental health related queries. If the chatbot seems you need external support, then it will respond appropriately.<br>"""
|
127 |
-
)
|
128 |
-
|
129 |
-
chatbot = gr.Chatbot()
|
130 |
-
query = gr.Textbox(label="Type your query here, then press 'enter' and scroll up for response")
|
131 |
-
clear = gr.Button(value="Clear Chat History!")
|
132 |
-
clear.style(size="sm")
|
133 |
-
|
134 |
-
llm_chain = init_llm_chain(peft_model, peft_tokenizer)
|
135 |
-
|
136 |
-
query.submit(user, [query, chatbot], [query, chatbot], queue=False).then(bot, chatbot, chatbot)
|
137 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
138 |
-
|
139 |
-
demo.queue().launch()
|
140 |
-
|
141 |
-
# !gradio deploy
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|