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|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# This allows the model to consider its prior context
|
| 8 |
+
def format_prompt(message, history):
|
| 9 |
+
prompt = "<s>"
|
| 10 |
+
for user_prompt, bot_response in history:
|
| 11 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 12 |
+
prompt += f" {bot_response}</s> "
|
| 13 |
+
prompt += f"[INST] {message} [/INST]"
|
| 14 |
+
return prompt
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def generate(
|
| 18 |
+
prompt,
|
| 19 |
+
history,
|
| 20 |
+
system_prompt,
|
| 21 |
+
temperature=0.9,
|
| 22 |
+
max_new_tokens=256,
|
| 23 |
+
top_p=0.95,
|
| 24 |
+
repetition_penalty=1.0,
|
| 25 |
+
):
|
| 26 |
+
temperature = float(temperature)
|
| 27 |
+
if temperature < 1e-2:
|
| 28 |
+
temperature = 1e-2
|
| 29 |
+
top_p = float(top_p)
|
| 30 |
+
|
| 31 |
+
generate_kwargs = dict(
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
max_new_tokens=max_new_tokens,
|
| 34 |
+
top_p=top_p,
|
| 35 |
+
repetition_penalty=repetition_penalty,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
seed=42,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 41 |
+
stream = client.text_generation(
|
| 42 |
+
formatted_prompt,
|
| 43 |
+
**generate_kwargs,
|
| 44 |
+
stream=True,
|
| 45 |
+
details=True,
|
| 46 |
+
return_full_text=False,
|
| 47 |
+
)
|
| 48 |
+
output = ""
|
| 49 |
+
|
| 50 |
+
# How does streaming works
|
| 51 |
+
for response in stream:
|
| 52 |
+
output += response.token.text
|
| 53 |
+
yield output
|
| 54 |
+
return output
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
additional_inputs = [
|
| 58 |
+
gr.Textbox(
|
| 59 |
+
label="System Prompt",
|
| 60 |
+
max_lines=1,
|
| 61 |
+
interactive=True,
|
| 62 |
+
),
|
| 63 |
+
gr.Slider(
|
| 64 |
+
label="Temperature",
|
| 65 |
+
value=0.9,
|
| 66 |
+
minimum=0.0,
|
| 67 |
+
maximum=1.0,
|
| 68 |
+
step=0.05,
|
| 69 |
+
interactive=True,
|
| 70 |
+
info="Higher values produce more diverse outputs",
|
| 71 |
+
),
|
| 72 |
+
gr.Slider(
|
| 73 |
+
label="Max new tokens",
|
| 74 |
+
value=256,
|
| 75 |
+
minimum=0,
|
| 76 |
+
maximum=1048,
|
| 77 |
+
step=64,
|
| 78 |
+
interactive=True,
|
| 79 |
+
info="The maximum numbers of new tokens",
|
| 80 |
+
),
|
| 81 |
+
gr.Slider(
|
| 82 |
+
label="Top-p (nucleus sampling)",
|
| 83 |
+
value=0.90,
|
| 84 |
+
minimum=0.0,
|
| 85 |
+
maximum=1,
|
| 86 |
+
step=0.05,
|
| 87 |
+
interactive=True,
|
| 88 |
+
info="Higher values sample more low-probability tokens",
|
| 89 |
+
),
|
| 90 |
+
gr.Slider(
|
| 91 |
+
label="Repetition penalty",
|
| 92 |
+
value=1.2,
|
| 93 |
+
minimum=1.0,
|
| 94 |
+
maximum=2.0,
|
| 95 |
+
step=0.05,
|
| 96 |
+
interactive=True,
|
| 97 |
+
info="Penalize repeated tokens",
|
| 98 |
+
),
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
examples = [
|
| 102 |
+
[
|
| 103 |
+
"I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?",
|
| 104 |
+
None,
|
| 105 |
+
None,
|
| 106 |
+
None,
|
| 107 |
+
None,
|
| 108 |
+
None,
|
| 109 |
+
],
|
| 110 |
+
[
|
| 111 |
+
"Can you write a short story about a time-traveling detective who solves historical mysteries?",
|
| 112 |
+
None,
|
| 113 |
+
None,
|
| 114 |
+
None,
|
| 115 |
+
None,
|
| 116 |
+
None,
|
| 117 |
+
],
|
| 118 |
+
[
|
| 119 |
+
"I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?",
|
| 120 |
+
None,
|
| 121 |
+
None,
|
| 122 |
+
None,
|
| 123 |
+
None,
|
| 124 |
+
None,
|
| 125 |
+
],
|
| 126 |
+
[
|
| 127 |
+
"I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?",
|
| 128 |
+
None,
|
| 129 |
+
None,
|
| 130 |
+
None,
|
| 131 |
+
None,
|
| 132 |
+
None,
|
| 133 |
+
],
|
| 134 |
+
[
|
| 135 |
+
"Can you explain how the QuickSort algorithm works and provide a Python implementation?",
|
| 136 |
+
None,
|
| 137 |
+
None,
|
| 138 |
+
None,
|
| 139 |
+
None,
|
| 140 |
+
None,
|
| 141 |
+
],
|
| 142 |
+
[
|
| 143 |
+
"What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?",
|
| 144 |
+
None,
|
| 145 |
+
None,
|
| 146 |
+
None,
|
| 147 |
+
None,
|
| 148 |
+
None,
|
| 149 |
+
],
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
iface = gr.ChatInterface(
|
| 153 |
+
fn=generate,
|
| 154 |
+
chatbot=gr.Chatbot(
|
| 155 |
+
show_label=False,
|
| 156 |
+
show_share_button=False,
|
| 157 |
+
show_copy_button=True,
|
| 158 |
+
likeable=True,
|
| 159 |
+
layout="panel",
|
| 160 |
+
),
|
| 161 |
+
additional_inputs=additional_inputs,
|
| 162 |
+
title="Mixtral 46.7B",
|
| 163 |
+
examples=examples,
|
| 164 |
+
concurrency_limit=20,
|
| 165 |
+
)
|
| 166 |
+
iface.launch(show_api=False)
|