import spaces import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM import torch from datetime import datetime import os # 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 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](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" # Get the token from environment variables hf_token = os.getenv('READTOKEN') if not hf_token: raise ValueError("Please set the READTOKEN environment variable") # Initialize tokenizer and model with token authentication tokenizer = AutoTokenizer.from_pretrained( model_id, token=hf_token, trust_remote_code=True ) model = AutoModelForCausalLM.from_pretrained( model_id, token=hf_token, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True ) def format_model_info(config): info = [] important_keys = [ "model_type", "vocab_size", "hidden_size", "num_attention_heads", "num_hidden_layers", "max_position_embeddings", "torch_dtype" ] for key in important_keys: if key in config: info.append(f"**{key}:** {config[key]}") return "\n".join(info) @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} <|user|>{user_prompt} <|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(Title) with gr.Row(): with gr.Column(): with gr.Group(): gr.Markdown("### Model Configuration") gr.Markdown(format_model_info(config_json)) with gr.Column(): with gr.Group(): gr.Markdown("### Tokenizer Configuration") gr.Markdown(f""" **Vocabulary Size:** {tokenizer.vocab_size} **Model Max Length:** {tokenizer.model_max_length} **Padding Token:** {tokenizer.pad_token} **EOS Token:** {tokenizer.eos_token} """) with gr.Row(): gr.Markdown(join_us) 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 with all parameters gr.Examples( examples=[ # Format: [system_prompt, user_prompt, temperature, max_tokens, top_p, rep_penalty, top_k] [ "Tu es Lucie, une assistante IA française serviable et amicale.", "Bonjour! Comment vas-tu aujourd'hui?", 0.7, # temperature 512, # max_new_tokens 0.9, # top_p 1.2, # repetition_penalty 50 # top_k ], [ "Tu es une experte en intelligence artificielle.", "Peux-tu m'expliquer ce qu'est l'intelligence artificielle?", 0.8, # higher temperature for more creative explanation 1024, # longer response 0.95, # higher top_p for more diverse output 1.1, # lower repetition penalty 40 # lower top_k for more focused output ], [ "Tu es une poétesse française.", "Écris un court poème sur Paris.", 0.9, # higher temperature for more creativity 256, # shorter for poetry 0.95, # higher top_p for more creative language 1.3, # higher repetition penalty for unique words 60 # higher top_k for more varied vocabulary ], [ "Tu es une experte en gastronomie française.", "Quels sont les plats traditionnels français les plus connus?", 0.7, # moderate temperature for factual response 768, # medium length 0.9, # balanced top_p 1.2, # standard repetition penalty 50 # standard top_k ], [ "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.", 0.6, # lower temperature for more factual response 1024, # longer for historical context 0.85, # lower top_p for more focused output 1.1, # lower repetition penalty 30 # lower top_k for more consistent output ] ], inputs=[ system_prompt, user_prompt, temperature, max_new_tokens, top_p, repetition_penalty, top_k ], outputs=output, label="Exemples de prompts avec paramètres optimisés" ) # 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)