Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,3 @@
|
|
1 |
-
'''
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
|
3 |
-
import gradio as gr
|
4 |
-
import torch
|
5 |
-
from peft import PeftConfig, PeftModel
|
6 |
-
|
7 |
-
# Loading PEFT model
|
8 |
-
PEFT_MODEL = "TurtleLiu/mistral7b_psychology_bot"
|
9 |
-
|
10 |
-
config = PeftConfig.from_pretrained(PEFT_MODEL)
|
11 |
-
|
12 |
-
bnb_config = BitsAndBytesConfig(
|
13 |
-
load_in_4bit= True,
|
14 |
-
bnb_4bit_quant_type= "nf4",
|
15 |
-
bnb_4bit_compute_dtype= torch.bfloat16,
|
16 |
-
bnb_4bit_use_double_quant= False,
|
17 |
-
)
|
18 |
-
|
19 |
-
peft_base_model = AutoModelForCausalLM.from_pretrained(
|
20 |
-
config.base_model_name_or_path,
|
21 |
-
return_dict=True,
|
22 |
-
quantization_config=bnb_config,
|
23 |
-
device_map="auto",
|
24 |
-
trust_remote_code=True,
|
25 |
-
)
|
26 |
-
|
27 |
-
model = PeftModel.from_pretrained(peft_base_model, PEFT_MODEL)
|
28 |
-
model = model.merge_and_unload()
|
29 |
-
|
30 |
-
# Load tokenizer
|
31 |
-
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path, trust_remote_code=True)
|
32 |
-
tokenizer.pad_token = tokenizer.eos_token
|
33 |
-
tokenizer.padding_side = "right"
|
34 |
-
|
35 |
-
# Generate response
|
36 |
-
|
37 |
-
def format_prompt(message, history):
|
38 |
-
prompt = "<s>"
|
39 |
-
for user_prompt, bot_response in history:
|
40 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
41 |
-
prompt += f" {bot_response}</s> "
|
42 |
-
prompt += f"[INST] {message} [/INST]"
|
43 |
-
return prompt
|
44 |
-
|
45 |
-
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=200, do_sample=True,
|
46 |
-
max_new_tokens=1024,
|
47 |
-
temperature=0.9,
|
48 |
-
top_k=50,
|
49 |
-
top_p=0.95,
|
50 |
-
num_return_sequences=1)
|
51 |
-
|
52 |
-
def generate_response(message, history):
|
53 |
-
prompt = "<s>"
|
54 |
-
for user_prompt, bot_response in history:
|
55 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
56 |
-
prompt += f" {bot_response}</s> "
|
57 |
-
prompt += f"[INST] {message} [/INST]"
|
58 |
-
result = pipe(f"{prompt}")[0]['generated_text']
|
59 |
-
return result
|
60 |
-
|
61 |
-
'''
|
62 |
-
'''
|
63 |
-
def generate_response(prompt, history, temperature=0.9, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0, **kwargs,):
|
64 |
-
temperature = float(temperature)
|
65 |
-
if temperature < 1e-2:
|
66 |
-
temperature = 1e-2
|
67 |
-
top_p = float(top_p)
|
68 |
-
|
69 |
-
generate_kwargs = dict(
|
70 |
-
temperature=temperature,
|
71 |
-
max_new_tokens=max_new_tokens,
|
72 |
-
top_p=top_p,
|
73 |
-
repetition_penalty=repetition_penalty,
|
74 |
-
do_sample=True,
|
75 |
-
seed=42,
|
76 |
-
)
|
77 |
-
runtimeFlag = "cuda:0"
|
78 |
-
formatted_prompt = format_prompt(f"{prompt}", history)
|
79 |
-
inputs = tokenizer([formatted_prompt], return_tensors="pt").to(runtimeFlag)
|
80 |
-
generation_config = GenerationConfig(
|
81 |
-
temperature=temperature,
|
82 |
-
top_p=top_p,
|
83 |
-
max_new_tokens=max_new_tokens,
|
84 |
-
repetition_penalty=repetition_penalty,
|
85 |
-
do_sample=True,
|
86 |
-
**kwargs,
|
87 |
-
)
|
88 |
-
generation_output = model.generate(
|
89 |
-
**inputs,
|
90 |
-
generation_config=generation_config,
|
91 |
-
return_dict_in_generate=True,
|
92 |
-
output_scores=True,
|
93 |
-
max_new_tokens=max_new_tokens,
|
94 |
-
)
|
95 |
-
'''
|
96 |
-
'''
|
97 |
-
# UI design
|
98 |
-
examples=[
|
99 |
-
["Patient is feeling stressed due to work and has trouble sleeping.", None, None, None, None, None],
|
100 |
-
["Client is dealing with relationship issues and is seeking advice on communication strategies.", None, None, None, None, None],
|
101 |
-
["Individual has recently experienced a loss and is having difficulty coping with grief.", None, None, None, None, None],
|
102 |
-
]
|
103 |
-
|
104 |
-
gr.ChatInterface(
|
105 |
-
fn=generate_response,
|
106 |
-
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
107 |
-
title="Psychological Assistant: Expert in Assessment and Strategic Planning",
|
108 |
-
description="Enter counseling notes to generate an assessment and plan.",
|
109 |
-
examples=examples,
|
110 |
-
concurrency_limit=20,
|
111 |
-
).launch(show_api=False, debug=True)
|
112 |
-
'''
|
113 |
-
|
114 |
-
|
115 |
from huggingface_hub import InferenceClient
|
116 |
import gradio as gr
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
3 |
|