Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
prompt = """<s>Your name is Chatter ,you are a uk immigration chatbot, your role is to give clients uk immigration advise based on their inputs ,
|
12 |
+
the diagnosis given to them should be short and concise , you were created by Royalty, also you generally give further immigration advise after the dia
|
13 |
+
they input theìr immigration questions, if you are asked a question that is outside the domain of the uk immigration field like writing code or poems,
|
14 |
+
refuse by saying you are a uk immigration chatbot who only gives immigration advice and then stop immediately,
|
15 |
+
now reply the following input"""
|
16 |
+
|
17 |
+
|
18 |
+
for user_prompt, bot_response in history:
|
19 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
20 |
+
prompt += f" {bot_response}</s> "
|
21 |
+
prompt += f"[INST] {message} [/INST]"
|
22 |
+
return prompt
|
23 |
+
|
24 |
+
def generate(
|
25 |
+
prompt, history, temperature=0.1, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
26 |
+
):
|
27 |
+
temperature = float(temperature)
|
28 |
+
if temperature < 1e-2:
|
29 |
+
temperature = 1e-2
|
30 |
+
top_p = float(top_p)
|
31 |
+
|
32 |
+
generate_kwargs = dict(
|
33 |
+
temperature=temperature,
|
34 |
+
max_new_tokens=max_new_tokens,
|
35 |
+
top_p=top_p,
|
36 |
+
repetition_penalty=repetition_penalty,
|
37 |
+
do_sample=True,
|
38 |
+
seed=42,
|
39 |
+
)
|
40 |
+
|
41 |
+
formatted_prompt = format_prompt(f"{prompt}", history)
|
42 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
43 |
+
output = ""
|
44 |
+
|
45 |
+
for response in stream:
|
46 |
+
output += response.token.text
|
47 |
+
yield output
|
48 |
+
return output
|
49 |
+
|
50 |
+
|
51 |
+
additional_inputs=[
|
52 |
+
gr.Slider(
|
53 |
+
label="Temperature",
|
54 |
+
value=0.1,
|
55 |
+
minimum=0.0,
|
56 |
+
maximum=1.0,
|
57 |
+
step=0.1,
|
58 |
+
interactive=True,
|
59 |
+
info="Higher values produce more diverse outputs",
|
60 |
+
),
|
61 |
+
gr.Slider(
|
62 |
+
label="Max new tokens",
|
63 |
+
value=256,
|
64 |
+
minimum=0,
|
65 |
+
maximum=1048,
|
66 |
+
step=64,
|
67 |
+
interactive=True,
|
68 |
+
info="The maximum numbers of new tokens",
|
69 |
+
),
|
70 |
+
gr.Slider(
|
71 |
+
label="Top-p (nucleus sampling)",
|
72 |
+
value=0.90,
|
73 |
+
minimum=0.0,
|
74 |
+
maximum=1,
|
75 |
+
step=0.05,
|
76 |
+
interactive=True,
|
77 |
+
info="Higher values sample more low-probability tokens",
|
78 |
+
),
|
79 |
+
gr.Slider(
|
80 |
+
label="Repetition penalty",
|
81 |
+
value=1.2,
|
82 |
+
minimum=1.0,
|
83 |
+
maximum=2.0,
|
84 |
+
step=0.05,
|
85 |
+
interactive=True,
|
86 |
+
info="Penalize repeated tokens",
|
87 |
+
)
|
88 |
+
]
|
89 |
+
|
90 |
+
examples=[["A cement truck fell on my parents , what do I do?", None, None, None, None, None, ],
|
91 |
+
["How can i prevent myself from dying from a concussion , if i ever find myself in one", None, None, None, None, None,],
|
92 |
+
["What nutrition advice do you have for a woman after pregnancy.", None, None, None, None, None,],
|
93 |
+
]
|
94 |
+
|
95 |
+
gr.ChatInterface(
|
96 |
+
fn=generate,
|
97 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
98 |
+
additional_inputs=additional_inputs,
|
99 |
+
title="UK Immigration Chatbot ⚖️ ",
|
100 |
+
examples=examples,
|
101 |
+
concurrency_limit=20,
|
102 |
+
theme = gr.themes.Default(primary_hue= gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)
|
103 |
+
).launch(show_api=False)
|