File size: 5,467 Bytes
1a1d765 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from datetime import datetime
# Model description
description = """
# 🇫🇷 Lucie-7B-Instruct
Lucie is a French language model based on Mistral-7B, fine-tuned on French data and instructions.
This demo allows you to interact with the model and adjust various generation parameters.
"""
join_us = """
## Join us:
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻
[](https://discord.gg/qdfnvSPcqP)
On 🤗Huggingface: [MultiTransformer](https://huggingface.co/MultiTransformer)
On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)
🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
"""
# Initialize model and tokenizer
model_id = "OpenLLM-France/Lucie-7B-Instruct-v1"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16
)
@spaces.GPU
def generate_response(system_prompt, user_prompt, temperature, max_new_tokens, top_p, repetition_penalty, top_k):
# Construct the full prompt with system and user messages
full_prompt = f"""<|system|>{system_prompt}</s>
<|user|>{user_prompt}</s>
<|assistant|>"""
# Prepare the input prompt
inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
# Generate response
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
top_k=top_k,
repetition_penalty=repetition_penalty,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode and return the response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the assistant's response
return response.split("<|assistant|>")[-1].strip()
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(description)
with gr.Row():
with gr.Column():
# System prompt
system_prompt = gr.Textbox(
label="Message Système",
value="Tu es Lucie, une assistante IA française serviable et amicale. Tu réponds toujours en français de manière précise et utile. Tu es honnête et si tu ne sais pas quelque chose, tu le dis simplement.",
lines=3
)
# User prompt
user_prompt = gr.Textbox(
label="Votre message",
placeholder="Entrez votre texte ici...",
lines=5
)
with gr.Accordion("Paramètres avancés", open=False):
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature"
)
max_new_tokens = gr.Slider(
minimum=1,
maximum=2048,
value=512,
step=1,
label="Longueur maximale"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.1,
label="Top-p"
)
top_k = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Top-k"
)
repetition_penalty = gr.Slider(
minimum=1.0,
maximum=2.0,
value=1.2,
step=0.1,
label="Pénalité de répétition"
)
generate_btn = gr.Button("Générer")
with gr.Column():
# Output component
output = gr.Textbox(
label="Réponse de Lucie",
lines=10
)
# Example prompts
gr.Examples(
examples=[
["Tu es Lucie, une assistante IA française serviable et amicale.", "Bonjour! Comment vas-tu aujourd'hui?"],
["Tu es une experte en intelligence artificielle.", "Peux-tu m'expliquer ce qu'est l'intelligence artificielle?"],
["Tu es une poétesse française.", "Écris un court poème sur Paris."],
["Tu es une experte en gastronomie française.", "Quels sont les plats traditionnels français les plus connus?"],
["Tu es une historienne spécialisée dans l'histoire de France.", "Explique-moi l'histoire de la Révolution française en quelques phrases."]
],
inputs=[system_prompt, user_prompt],
outputs=output,
label="Exemples de prompts"
)
# Set up the generation event
generate_btn.click(
fn=generate_response,
inputs=[system_prompt, user_prompt, temperature, max_new_tokens, top_p, repetition_penalty, top_k],
outputs=output
)
# Launch the demo
if __name__ == "__main__":
demo.launch(ssr_mode=False) |