TurtleLiu commited on
Commit
cb60b9e
1 Parent(s): 68de809

Create app.py

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