Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
client = InferenceClient(
|
5 |
+
"mistralai/Mistral-7B-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 |
+
def generate(
|
18 |
+
prompt, history, max_tokens=2000,
|
19 |
+
):
|
20 |
+
'''temperature = float(temperature)
|
21 |
+
if temperature < 1e-2:
|
22 |
+
temperature = 1e-2
|
23 |
+
top_p = float(top_p)'''
|
24 |
+
|
25 |
+
generate_kwargs = dict(
|
26 |
+
#temperature=temperature,
|
27 |
+
max_tokens=max_tokens,
|
28 |
+
#top_p=top_p,
|
29 |
+
#repetition_penalty=repetition_penalty,
|
30 |
+
#do_sample=True,
|
31 |
+
#seed=42,
|
32 |
+
)
|
33 |
+
|
34 |
+
formatted_prompt = format_prompt(prompt, history)
|
35 |
+
|
36 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
37 |
+
output = ""
|
38 |
+
|
39 |
+
for response in stream:
|
40 |
+
output += response.token.text
|
41 |
+
yield output
|
42 |
+
return output
|
43 |
+
|
44 |
+
|
45 |
+
additional_inputs=[
|
46 |
+
gr.Slider(
|
47 |
+
label="Max tokens",
|
48 |
+
value=2000,
|
49 |
+
minimum=0,
|
50 |
+
maximum=2048,
|
51 |
+
step=64,
|
52 |
+
interactive=True,
|
53 |
+
info="The maximum numbers of new tokens",
|
54 |
+
),
|
55 |
+
|
56 |
+
]
|
57 |
+
|
58 |
+
|
59 |
+
gr.ChatInterface(
|
60 |
+
fn=generate,
|
61 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
62 |
+
additional_inputs=additional_inputs,
|
63 |
+
title="""Mistral 7B"""
|
64 |
+
).launch(show_api=False)
|