File size: 1,837 Bytes
1e7d826
 
 
 
 
 
 
 
 
1ca2187
1e7d826
 
 
 
1ca2187
1e7d826
1ca2187
 
1e7d826
1ca2187
 
 
1e7d826
1ca2187
1e7d826
1ca2187
1e7d826
 
 
 
1ca2187
 
1e7d826
 
 
 
 
 
 
 
1ca2187
 
 
 
 
 
 
 
 
 
 
 
1e7d826
 
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
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

MODEL_ID = "Qwen/Qwen2.5-Coder-7B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float16,   # float16 pour économiser de la mémoire
    device_map="auto",
    trust_remote_code=True,
)

SYSTEM_PROMPT = "You are a helpful expert in programming and mathematics. Think step by step."

def chat(message, history):
    full_history = [{"role": "system", "content": SYSTEM_PROMPT}]
    
    for user_msg, assistant_msg in history:
        full_history.append({"role": "user", "content": user_msg})
        full_history.append({"role": "assistant", "content": assistant_msg})
    
    full_history.append({"role": "user", "content": message})
    
    text = tokenizer.apply_chat_template(full_history, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(text, return_tensors="pt").to(model.device)
    
    outputs = model.generate(
        **inputs,
        max_new_tokens=1024,
        temperature=0.7,
        do_sample=True,
        top_p=0.9,
        repetition_penalty=1.1
    )
    
    response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
    return response

with gr.Blocks(title="🧠 IA Code & Maths") as demo:
    gr.Markdown("# 🧠 IA Code & Math\n\nModèle : Qwen2.5-Coder-7B")

    gr.ChatInterface(
        fn=chat,
        title="Pose ta question en code ou maths",
        description="Le modèle charge lentement la première fois.",
        examples=[
            ["Écris une fonction Python pour calculer la suite de Fibonacci"],
            ["Résous : Quelle est la somme des nombres premiers entre 1 et 100 ?"],
        ]
    )

demo.launch()