Spaces:
Sleeping
Sleeping
| 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() |