Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,68 @@
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
3 |
|
4 |
+
client = InferenceClient(
|
5 |
+
"TurtleLiu/mistral7b_psychology_bot"
|
6 |
+
)
|
7 |
+
|
8 |
+
|
9 |
+
def format_prompt(message, history):
|
10 |
+
prompt = "<s>"
|
11 |
+
for user_prompt, bot_response in history:
|
12 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
13 |
+
prompt += f" {bot_response}</s> "
|
14 |
+
prompt += f"[INST] {message} [/INST]"
|
15 |
+
return prompt
|
16 |
+
|
17 |
+
|
18 |
+
'''
|
19 |
+
def format_prompt(message, history):
|
20 |
+
prompt = "<s>"
|
21 |
+
for user_prompt, bot_response in history:
|
22 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
23 |
+
prompt += f" {bot_response}</s> "
|
24 |
+
prompt += f"[INST] As a psychology counselor assistant, provide an assessment and plan for the following counseling notes. Please present a summary, don't make it so long. Present in lines.: {message} [/INST]"
|
25 |
+
return prompt
|
26 |
+
'''
|
27 |
+
|
28 |
+
def generate(
|
29 |
+
prompt, history, temperature=0.9, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
|
30 |
+
):
|
31 |
+
temperature = float(temperature)
|
32 |
+
if temperature < 1e-2:
|
33 |
+
temperature = 1e-2
|
34 |
+
top_p = float(top_p)
|
35 |
+
|
36 |
+
generate_kwargs = dict(
|
37 |
+
temperature=temperature,
|
38 |
+
max_new_tokens=max_new_tokens,
|
39 |
+
top_p=top_p,
|
40 |
+
repetition_penalty=repetition_penalty,
|
41 |
+
do_sample=True,
|
42 |
+
seed=42,
|
43 |
+
)
|
44 |
+
|
45 |
+
formatted_prompt = format_prompt(f"{prompt}", history)
|
46 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
47 |
+
output = ""
|
48 |
+
|
49 |
+
for response in stream:
|
50 |
+
output += response.token.text
|
51 |
+
yield output
|
52 |
+
return output
|
53 |
+
|
54 |
+
|
55 |
+
examples=[
|
56 |
+
["Patient is feeling stressed due to work and has trouble sleeping.", None, None, None, None, None],
|
57 |
+
["Client is dealing with relationship issues and is seeking advice on communication strategies.", None, None, None, None, None],
|
58 |
+
["Individual has recently experienced a loss and is having difficulty coping with grief.", None, None, None, None, None],
|
59 |
+
]
|
60 |
+
|
61 |
+
gr.ChatInterface(
|
62 |
+
fn=generate,
|
63 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
64 |
+
title="Psychological Assistant: Expert in Assessment and Strategic Planning",
|
65 |
+
description="Enter counseling notes to generate an assessment and plan.",
|
66 |
+
examples=examples,
|
67 |
+
concurrency_limit=20,
|
68 |
+
).launch(show_api=False, debug=True)
|