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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
| 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_new_tokens=max_new_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="Temperature",
|
| 48 |
+
value=0.9,
|
| 49 |
+
minimum=0.0,
|
| 50 |
+
maximum=1.0,
|
| 51 |
+
step=0.05,
|
| 52 |
+
interactive=True,
|
| 53 |
+
info="Higher values produce more diverse outputs",
|
| 54 |
+
),
|
| 55 |
+
gr.Slider(
|
| 56 |
+
label="Max new tokens",
|
| 57 |
+
value=256,
|
| 58 |
+
minimum=0,
|
| 59 |
+
maximum=1048,
|
| 60 |
+
step=64,
|
| 61 |
+
interactive=True,
|
| 62 |
+
info="The maximum numbers of new tokens",
|
| 63 |
+
),
|
| 64 |
+
gr.Slider(
|
| 65 |
+
label="Top-p (nucleus sampling)",
|
| 66 |
+
value=0.90,
|
| 67 |
+
minimum=0.0,
|
| 68 |
+
maximum=1,
|
| 69 |
+
step=0.05,
|
| 70 |
+
interactive=True,
|
| 71 |
+
info="Higher values sample more low-probability tokens",
|
| 72 |
+
),
|
| 73 |
+
gr.Slider(
|
| 74 |
+
label="Repetition penalty",
|
| 75 |
+
value=1.2,
|
| 76 |
+
minimum=1.0,
|
| 77 |
+
maximum=2.0,
|
| 78 |
+
step=0.05,
|
| 79 |
+
interactive=True,
|
| 80 |
+
info="Penalize repeated tokens",
|
| 81 |
+
)
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
css = """
|
| 85 |
+
#mkd {
|
| 86 |
+
height: 200px;
|
| 87 |
+
overflow: auto;
|
| 88 |
+
border: 1px solid #ccc;
|
| 89 |
+
}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
with gr.Blocks(css=css) as demo:
|
| 93 |
+
|
| 94 |
+
gr.ChatInterface(
|
| 95 |
+
generate,
|
| 96 |
+
additional_inputs=additional_inputs,
|
| 97 |
+
examples = [
|
| 98 |
+
["π Write a Python Streamlit program that shows a thumbs up and thumbs down button for scoring an evaluation. When the user clicks, maintain a saved text file that tracks and shows the number of clicks with a refresh and sorts responses by the number of clicks."],
|
| 99 |
+
["π Create a Pandas DataFrame and display it using Streamlit. Use emojis to indicate the status of each row (e.g., β
for good, β for bad)."],
|
| 100 |
+
["π Using Gradio, create a simple interface where users can upload a CSV file and filter the data based on selected columns."],
|
| 101 |
+
["π Implement emoji reactions in a Streamlit app. When a user clicks on an emoji, record the click count in a Pandas DataFrame and display the DataFrame."],
|
| 102 |
+
["π Create a program that fetches a dataset from Huggingface Hub and shows basic statistics about it using Pandas in a Streamlit app."],
|
| 103 |
+
["π€ Use Gradio to create a user interface for a text summarizer model from Huggingface Hub."],
|
| 104 |
+
["π Create a Streamlit app to visualize time series data. Use Pandas to manipulate the data and plot it using Streamlitβs native plotting options."],
|
| 105 |
+
["π Implement a voice-activated feature in a Gradio interface. Use a pre-trained model from Huggingface Hub for speech recognition."],
|
| 106 |
+
["π Create a search function in a Streamlit app that filters through a Pandas DataFrame and displays the results."],
|
| 107 |
+
["π€ Write a Python script that uploads a model to Huggingface Hub and then uses it in a Streamlit app."],
|
| 108 |
+
["π Create a Gradio interface for a clapping hands emoji (π) counter. When a user inputs a text, the interface should return the number of clapping hands emojis in the text."],
|
| 109 |
+
["π Use Pandas to read an Excel sheet in a Streamlit app. Allow the user to select which sheet they want to view."],
|
| 110 |
+
["π Implement a login screen in a Streamlit app using Python. Secure the login by hashing the password."],
|
| 111 |
+
["π€© Create a Gradio interface that uses a model from Huggingface Hub to generate creative text based on a userβs input. Add sliders for controlling temperature and other hyperparameters."]
|
| 112 |
+
]
|
| 113 |
+
)
|
| 114 |
+
gr.HTML("""<h2>π€ Mistral Chat - Gradio π€</h2>
|
| 115 |
+
In this demo, you can chat with <a href='https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1'>Mistral-7B-Instruct</a> model. π¬
|
| 116 |
+
Learn more about the model <a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>here</a>. π
|
| 117 |
+
<h2>π Model Features π </h2>
|
| 118 |
+
<ul>
|
| 119 |
+
<li>πͺ Sliding Window Attention with 128K tokens span</li>
|
| 120 |
+
<li>π GQA for faster inference</li>
|
| 121 |
+
<li>π Byte-fallback BPE tokenizer</li>
|
| 122 |
+
</ul>
|
| 123 |
+
<h3>π License π Released under Apache 2.0 License</h3>
|
| 124 |
+
<h3>π¦ Usage π¦</h3>
|
| 125 |
+
<ul>
|
| 126 |
+
<li>π Available on Huggingface Hub</li>
|
| 127 |
+
<li>π Python code snippets for easy setup</li>
|
| 128 |
+
<li>π Expected speedups with Flash Attention 2</li>
|
| 129 |
+
</ul>
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
markdown="""
|
| 133 |
+
| Feature | Description | Byline |
|
| 134 |
+
|---------|-------------|--------|
|
| 135 |
+
| πͺ Sliding Window Attention with 128K tokens span | Enables the model to have a larger context for each token. | Increases model's understanding of context, resulting in more coherent and contextually relevant outputs. |
|
| 136 |
+
| π GQA for faster inference | Graph Query Attention allows faster computation during inference. | Speeds up the model inference time without sacrificing too much on accuracy. |
|
| 137 |
+
| π Byte-fallback BPE tokenizer | Uses Byte Pair Encoding but can fall back to byte-level encoding. | Allows the tokenizer to handle a wider variety of input text while keeping token size manageable. |
|
| 138 |
+
| π License | Released under Apache 2.0 License | Gives you a permissive free software license, allowing you freedom to use, modify, and distribute the code. |
|
| 139 |
+
| π¦ Usage | | |
|
| 140 |
+
| π Available on Huggingface Hub | The model can be easily downloaded and set up from Huggingface. | Makes it easier to integrate the model into various projects. |
|
| 141 |
+
| π Python code snippets for easy setup | Provides Python code snippets for quick and easy model setup. | Facilitates rapid development and deployment, especially useful for prototyping. |
|
| 142 |
+
| π Expected speedups with Flash Attention 2 | Upcoming update expected to bring speed improvements. | Keep an eye out for this update to benefit from performance gains. |
|
| 143 |
+
|
| 144 |
+
# π Model Features and More π
|
| 145 |
+
|
| 146 |
+
## Features
|
| 147 |
+
|
| 148 |
+
- πͺ Sliding Window Attention with 128K tokens span
|
| 149 |
+
- **Byline**: Increases model's understanding of context, resulting in more coherent and contextually relevant outputs.
|
| 150 |
+
|
| 151 |
+
- π GQA for faster inference
|
| 152 |
+
- **Byline**: Speeds up the model inference time without sacrificing too much on accuracy.
|
| 153 |
+
|
| 154 |
+
- π Byte-fallback BPE tokenizer
|
| 155 |
+
- **Byline**: Allows the tokenizer to handle a wider variety of input text while keeping token size manageable.
|
| 156 |
+
|
| 157 |
+
- π License: Released under Apache 2.0 License
|
| 158 |
+
- **Byline**: Gives you a permissive free software license, allowing you freedom to use, modify, and distribute the code.
|
| 159 |
+
|
| 160 |
+
## Usage π¦
|
| 161 |
+
|
| 162 |
+
- π Available on Huggingface Hub
|
| 163 |
+
- **Byline**: Makes it easier to integrate the model into various projects.
|
| 164 |
+
|
| 165 |
+
- π Python code snippets for easy setup
|
| 166 |
+
- **Byline**: Facilitates rapid development and deployment, especially useful for prototyping.
|
| 167 |
+
|
| 168 |
+
- π Expected speedups with Flash Attention 2
|
| 169 |
+
- **Byline**: Keep an eye out for this update to benefit from performance gains.
|
| 170 |
+
"""
|
| 171 |
+
gr.Markdown(markdown)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def SpeechSynthesis(result):
|
| 175 |
+
documentHTML5='''
|
| 176 |
+
<!DOCTYPE html>
|
| 177 |
+
<html>
|
| 178 |
+
<head>
|
| 179 |
+
<title>Read It Aloud</title>
|
| 180 |
+
<script type="text/javascript">
|
| 181 |
+
function readAloud() {
|
| 182 |
+
const text = document.getElementById("textArea").value;
|
| 183 |
+
const speech = new SpeechSynthesisUtterance(text);
|
| 184 |
+
window.speechSynthesis.speak(speech);
|
| 185 |
+
}
|
| 186 |
+
</script>
|
| 187 |
+
</head>
|
| 188 |
+
<body>
|
| 189 |
+
<h1>π Read It Aloud</h1>
|
| 190 |
+
<textarea id="textArea" rows="10" cols="80">
|
| 191 |
+
'''
|
| 192 |
+
documentHTML5 = documentHTML5 + result
|
| 193 |
+
documentHTML5 = documentHTML5 + '''
|
| 194 |
+
</textarea>
|
| 195 |
+
<br>
|
| 196 |
+
<button onclick="readAloud()">π Read Aloud</button>
|
| 197 |
+
</body>
|
| 198 |
+
</html>
|
| 199 |
+
'''
|
| 200 |
+
gr.HTML(documentHTML5)
|
| 201 |
+
# components.html(documentHTML5, width=1280, height=1024)
|
| 202 |
+
#return result
|
| 203 |
+
SpeechSynthesis(markdown)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
demo.queue().launch(debug=True)
|