System prompt
Browse files
app.py
CHANGED
|
@@ -1,23 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import spaces
|
| 4 |
-
from transformers import GemmaTokenizer, AutoModelForCausalLM
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
# Set an environment variable
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 10 |
|
| 11 |
-
|
| 12 |
DESCRIPTION = '''
|
| 13 |
<div>
|
| 14 |
-
<h1 style="text-align: center;">
|
| 15 |
</div>
|
| 16 |
'''
|
| 17 |
|
| 18 |
LICENSE = """
|
| 19 |
<p/>
|
| 20 |
-
|
| 21 |
---
|
| 22 |
"""
|
| 23 |
|
|
@@ -28,7 +25,6 @@ PLACEHOLDER = """
|
|
| 28 |
</div>
|
| 29 |
"""
|
| 30 |
|
| 31 |
-
|
| 32 |
css = """
|
| 33 |
h1 {
|
| 34 |
text-align: center;
|
|
@@ -45,7 +41,8 @@ h1 {
|
|
| 45 |
|
| 46 |
# Load the tokenizer and model
|
| 47 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Ministral-8B-Instruct-2410")
|
| 48 |
-
model = AutoModelForCausalLM.from_pretrained("mistralai/Ministral-8B-Instruct-2410", device_map="auto")
|
|
|
|
| 49 |
terminators = [
|
| 50 |
tokenizer.eos_token_id,
|
| 51 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
|
@@ -53,23 +50,30 @@ terminators = [
|
|
| 53 |
|
| 54 |
@spaces.GPU(duration=120)
|
| 55 |
def chat_llama3_8b(message: str,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
"""
|
| 61 |
-
Generate a streaming response using the
|
| 62 |
Args:
|
| 63 |
message (str): The input message.
|
| 64 |
history (list): The conversation history used by ChatInterface.
|
| 65 |
temperature (float): The temperature for generating the response.
|
| 66 |
max_new_tokens (int): The maximum number of new tokens to generate.
|
|
|
|
| 67 |
Returns:
|
| 68 |
str: The generated response.
|
| 69 |
"""
|
| 70 |
conversation = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
for user, assistant in history:
|
| 72 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
|
|
|
| 73 |
conversation.append({"role": "user", "content": message})
|
| 74 |
|
| 75 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
|
@@ -77,14 +81,14 @@ def chat_llama3_8b(message: str,
|
|
| 77 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 78 |
|
| 79 |
generate_kwargs = dict(
|
| 80 |
-
input_ids=
|
| 81 |
streamer=streamer,
|
| 82 |
max_new_tokens=max_new_tokens,
|
| 83 |
do_sample=True,
|
| 84 |
temperature=temperature,
|
| 85 |
eos_token_id=terminators,
|
| 86 |
)
|
| 87 |
-
|
| 88 |
if temperature == 0:
|
| 89 |
generate_kwargs['do_sample'] = False
|
| 90 |
|
|
@@ -94,45 +98,37 @@ def chat_llama3_8b(message: str,
|
|
| 94 |
outputs = []
|
| 95 |
for text in streamer:
|
| 96 |
outputs.append(text)
|
| 97 |
-
#print(outputs)
|
| 98 |
yield "".join(outputs)
|
| 99 |
|
| 100 |
|
| 101 |
# Gradio block
|
| 102 |
-
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 103 |
|
| 104 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 105 |
|
| 106 |
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
gr.ChatInterface(
|
| 108 |
fn=chat_llama3_8b,
|
| 109 |
chatbot=chatbot,
|
| 110 |
fill_height=True,
|
| 111 |
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
| 112 |
additional_inputs=[
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
label="Temperature",
|
| 118 |
-
render=False),
|
| 119 |
-
gr.Slider(minimum=128,
|
| 120 |
-
maximum=4096,
|
| 121 |
-
step=1,
|
| 122 |
-
value=4096,
|
| 123 |
-
label="Max new tokens",
|
| 124 |
-
render=False ),
|
| 125 |
-
],
|
| 126 |
examples=[
|
| 127 |
-
['
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
],
|
| 133 |
-
cache_examples=False,
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
if __name__ == "__main__":
|
| 137 |
demo.launch()
|
| 138 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import spaces
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
# Set an environment variable
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 9 |
|
|
|
|
| 10 |
DESCRIPTION = '''
|
| 11 |
<div>
|
| 12 |
+
<h1 style="text-align: center;">Mistral Chat</h1>
|
| 13 |
</div>
|
| 14 |
'''
|
| 15 |
|
| 16 |
LICENSE = """
|
| 17 |
<p/>
|
|
|
|
| 18 |
---
|
| 19 |
"""
|
| 20 |
|
|
|
|
| 25 |
</div>
|
| 26 |
"""
|
| 27 |
|
|
|
|
| 28 |
css = """
|
| 29 |
h1 {
|
| 30 |
text-align: center;
|
|
|
|
| 41 |
|
| 42 |
# Load the tokenizer and model
|
| 43 |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Ministral-8B-Instruct-2410")
|
| 44 |
+
model = AutoModelForCausalLM.from_pretrained("mistralai/Ministral-8B-Instruct-2410", device_map="auto")
|
| 45 |
+
|
| 46 |
terminators = [
|
| 47 |
tokenizer.eos_token_id,
|
| 48 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
|
|
|
| 50 |
|
| 51 |
@spaces.GPU(duration=120)
|
| 52 |
def chat_llama3_8b(message: str,
|
| 53 |
+
history: list,
|
| 54 |
+
temperature: float,
|
| 55 |
+
max_new_tokens: int,
|
| 56 |
+
system_prompt: str) -> str:
|
| 57 |
"""
|
| 58 |
+
Generate a streaming response using the Mistral-8B model.
|
| 59 |
Args:
|
| 60 |
message (str): The input message.
|
| 61 |
history (list): The conversation history used by ChatInterface.
|
| 62 |
temperature (float): The temperature for generating the response.
|
| 63 |
max_new_tokens (int): The maximum number of new tokens to generate.
|
| 64 |
+
system_prompt (str): The system prompt to guide the assistant's behavior.
|
| 65 |
Returns:
|
| 66 |
str: The generated response.
|
| 67 |
"""
|
| 68 |
conversation = []
|
| 69 |
+
|
| 70 |
+
# Include system prompt at the beginning if provided
|
| 71 |
+
if system_prompt:
|
| 72 |
+
conversation.append({"role": "system", "content": system_prompt})
|
| 73 |
+
|
| 74 |
for user, assistant in history:
|
| 75 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
| 76 |
+
|
| 77 |
conversation.append({"role": "user", "content": message})
|
| 78 |
|
| 79 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
|
|
|
| 81 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 82 |
|
| 83 |
generate_kwargs = dict(
|
| 84 |
+
input_ids=input_ids,
|
| 85 |
streamer=streamer,
|
| 86 |
max_new_tokens=max_new_tokens,
|
| 87 |
do_sample=True,
|
| 88 |
temperature=temperature,
|
| 89 |
eos_token_id=terminators,
|
| 90 |
)
|
| 91 |
+
|
| 92 |
if temperature == 0:
|
| 93 |
generate_kwargs['do_sample'] = False
|
| 94 |
|
|
|
|
| 98 |
outputs = []
|
| 99 |
for text in streamer:
|
| 100 |
outputs.append(text)
|
|
|
|
| 101 |
yield "".join(outputs)
|
| 102 |
|
| 103 |
|
| 104 |
# Gradio block
|
| 105 |
+
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
| 106 |
|
| 107 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
| 108 |
|
| 109 |
gr.Markdown(DESCRIPTION)
|
| 110 |
+
|
| 111 |
+
system_prompt_input = gr.Textbox(
|
| 112 |
+
label="System Prompt",
|
| 113 |
+
placeholder="Enter system instructions for the model...",
|
| 114 |
+
lines=2
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
gr.ChatInterface(
|
| 118 |
fn=chat_llama3_8b,
|
| 119 |
chatbot=chatbot,
|
| 120 |
fill_height=True,
|
| 121 |
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
| 122 |
additional_inputs=[
|
| 123 |
+
system_prompt_input,
|
| 124 |
+
gr.Slider(minimum=0, maximum=1, step=0.1, value=0.8, label="Temperature", render=False),
|
| 125 |
+
gr.Slider(minimum=128, maximum=4096, step=1, value=4096, label="Max new tokens", render=False),
|
| 126 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
examples=[
|
| 128 |
+
['Are you a sentient being?']
|
| 129 |
+
],
|
| 130 |
+
cache_examples=False
|
| 131 |
+
)
|
| 132 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
if __name__ == "__main__":
|
| 134 |
demo.launch()
|
|
|