Use_of_force / app.py
BilalSardar's picture
Update app.py
038e923 verified
raw
history blame
4.66 kB
from huggingface_hub import InferenceClient
import gradio as gr
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 and where they saw patient when officers arrived and any aditional information. you always write a use of force like this sample:
LOCATION: G3 ward, Addenbrookes Hospital
DATE:04-02-2024
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,adinfo, history='', temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
):
prompt="time was:"+time+ " Ward was: "+ward+", Addenbrookes Hospital "+" Patient Name is:"+p+" Patient Number is: "+pn+" Officer Name is: "+on +" Patient Location was: "+loc +" 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","had spit on one of the officers" ],
]
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.Textbox(label='Any Additional Information? (optional)')],
examples=examples,
outputs=gr.Textbox(label='Use Of Force Report'),
title=f"<img id='Mixtral-portrait' src='{{\"file/logo.jpg\"}}' width='30%' /> Chat to Evil Mixtral",
concurrency_limit=20,
css=css
).launch(show_api=False)