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