File size: 5,090 Bytes
66de7ea
 
88990b7
66de7ea
88990b7
66de7ea
 
 
 
0adaf8c
038e923
95193ff
9aacb87
95193ff
 
 
 
 
 
 
66de7ea
 
 
 
 
 
 
 
 
 
0adaf8c
66de7ea
88990b7
 
 
 
0adaf8c
66de7ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528ad26
 
66de7ea
 
 
 
 
 
 
 
 
 
f11143a
f1e02be
66de7ea
95193ff
66de7ea
64f62a1
038e923
 
dff5e60
66de7ea
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from huggingface_hub import InferenceClient
import gradio as gr
from datetime import date

today = date.today()
client = InferenceClient(
    "mistralai/Mixtral-8x7B-Instruct-v0.1"
)

system_prompt = """You are Mixtral, a professional and polite use of force writer. You will receive patient name, patient number, time, date, hospital ward, officer name, PI Position and where they saw patient when officers arrived and any aditional information,add the additional information in between and always write polit and calm and professional report. you always use this sample as guide to write the report : 
LOCATION: G3 ward, Addenbrookes Hospital

DATE:[date]

PATIENT NAME: James Mullins

HOSPITAL NUMBER: 1858567

OFFICER NAME: Bilal Sardar
I am a security officer at Addenbrookes Hospital. At approximately 11:30 am, we received a call for assistance from the ward. When we arrived, patient was found outside the ward, in the corridor, displaying aggressive behaviour and attempting to leave the site. To ensure his safety and that of others, Physical Intervention (PI) was employed, with a focus on the left arm, to guide him back to his bed space. We applied the least restrictive holds possible and tried to de escalate the situation by talking im calmly and respectfully. We released him when he calmed to him down and cooperated with us and staff in his bed space. We utilized the right technique for holds as taught by NHS trainer, ensuring that no individuals were harmed during the intervention. """

def format_prompt(message, history):
  prompt = "<s>"
  for user_prompt, bot_response in history:
    prompt += f"[INST] {user_prompt} [/INST]"
    prompt += f" {bot_response}</s> "
  prompt += f"[INST] {message} [/INST]"
  return prompt

def generate(
    time,ward,p,pn,on,loc,pipos,adinfo, history='', temperature=0.9, max_new_tokens=1000, top_p=0.95, repetition_penalty=1.0,
):
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)

    prompt="Date is:"+d4+" time was:"+time+ " Ward was: "+ward+", Addenbrookes Hospital "+" Patient Name is:"+p+" Patient Number is: "+pn+" Officer Name is: "+on +" Patient Location was: "+loc +" PI Postion was: "+pipos+" Additional information to better understand the incident: "+ adinfo
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=42,
    )

    formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""

    for response in stream:
        output += response.token.text
        yield output
    return output


additional_inputs=[
    gr.Textbox(
        label="System Prompt",
        max_lines=1,
        interactive=True,
    ),
    gr.Slider(
        label="Temperature",
        value=0.9,
        minimum=0.0,
        maximum=1.0,
        step=0.05,
        interactive=True,
        info="Higher values produce more diverse outputs",
    ),
    gr.Slider(
        label="Max new tokens",
        value=256,
        minimum=0,
        maximum=1048,
        step=64,
        interactive=True,
        info="The maximum numbers of new tokens",
    ),
    gr.Slider(
        label="Top-p (nucleus sampling)",
        value=0.90,
        minimum=0.0,
        maximum=1,
        step=0.05,
        interactive=True,
        info="Higher values sample more low-probability tokens",
    ),
    gr.Slider(
        label="Repetition penalty",
        value=1.2,
        minimum=1.0,
        maximum=2.0,
        step=0.05,
        interactive=True,
        info="Penalize repeated tokens",
    )
]

examples=[["10:30", "D9", "Allen Mark", "1581760", "Bilal Sardar", "outside ward","Right Arm","had spit on me" ],
         ["10:30", "C5", "Robert Williams", "1875760", "Bilal Sardar", "inside ward on his bed","Legs","Did PI to give Medication" ]]

css="""
.gradio-container{
    max-width: 720px!important;
}
img#santa-portrait {
    margin: 20px auto;
    border-radius: 10px;
}
"""

gr.Interface(
    fn=generate,
    #chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, bubble_full_width=False, avatar_images=[None, "logo.jpg"]),
    #additional_inputs=additional_inputs,
    inputs=[gr.Textbox(label='Time e.g 15:30'),gr.Textbox(label='Hospital ward'),gr.Textbox(label='Patient Name'),gr.Textbox(label='Patient Number (seven digit number)'),gr.Textbox(label='Officer Name'),gr.Textbox(label='Patient Location'),gr.Radio(["Left Arm", "Right Arm","Legs"], type="value", label='PI Position'),gr.Textbox(label='Any Additional Information? (optional)')],
    examples=examples,
    outputs=gr.Textbox(label='Use Of Force Report'),
    title="Use Of Force",
    concurrency_limit=20,
    css=css
).launch(show_api=False)