TurtleLiu commited on
Commit
eb80ed2
·
1 Parent(s): 0e74602

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+
4
+ client = InferenceClient(
5
+ "mistralai/Mixtral-8x7B-Instruct-v0.1"
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
+ def format_prompt(message, history):
19
+ prompt = "<s>"
20
+ for user_prompt, bot_response in history:
21
+ prompt += f"[INST] {user_prompt} [/INST]"
22
+ prompt += f" {bot_response}</s> "
23
+ 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]"
24
+ return prompt
25
+ def generate(
26
+ prompt, history, temperature=0.9, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
27
+ ):
28
+ temperature = float(temperature)
29
+ if temperature < 1e-2:
30
+ temperature = 1e-2
31
+ top_p = float(top_p)
32
+
33
+ generate_kwargs = dict(
34
+ temperature=temperature,
35
+ max_new_tokens=max_new_tokens,
36
+ top_p=top_p,
37
+ repetition_penalty=repetition_penalty,
38
+ do_sample=True,
39
+ seed=42,
40
+ )
41
+
42
+ formatted_prompt = format_prompt(f"{prompt}", history)
43
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
44
+ output = ""
45
+
46
+ for response in stream:
47
+ output += response.token.text
48
+ yield output
49
+ return output
50
+
51
+
52
+ examples=[
53
+ ["Patient is feeling stressed due to work and has trouble sleeping.", None, None, None, None, None],
54
+ ["Client is dealing with relationship issues and is seeking advice on communication strategies.", None, None, None, None, None],
55
+ ["Individual has recently experienced a loss and is having difficulty coping with grief.", None, None, None, None, None],
56
+ ]
57
+
58
+ gr.ChatInterface(
59
+ fn=generate,
60
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
61
+ title="Psychological Assistant: Expert in Assessment and Strategic Planning",
62
+ description="Enter counseling notes to generate an assessment and plan.",
63
+ examples=examples,
64
+ concurrency_limit=20,
65
+ ).launch(show_api=False, debug=True)