Download app.py from Vdphacker/CyberAI: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/Vdphacker/CyberAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/Vdphacker/CyberAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Vdphacker/CyberAI/resolve/main/app.py
1.52 kB
| # Dans app.py — section de chargement du modèle | |
| print("⏳ Chargement du modèle Mistral-7B...") | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
| import torch | |
| model_name = "mistralai/Mistral-7B-Instruct-v0.3" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # Détecter si GPU disponible | |
| use_gpu = torch.cuda.is_available() | |
| print(f"{'✅ GPU détecté.' if use_gpu else '⚠️ Aucun GPU — utilisation du CPU (plus lent).'}") | |
| if use_gpu: | |
| # Charger en 4-bit si GPU | |
| from transformers import BitsAndBytesConfig | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.float16 | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| device = 0 | |
| else: | |
| # Charger en FP16/FP32 sur CPU (nécessite ~14 Go de RAM) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| torch_dtype=torch.float16, # ou torch.float32 si mémoire insuffisante | |
| low_cpu_mem_usage=True, | |
| trust_remote_code=True | |
| ) | |
| device = -1 | |
| pipe = pipeline( | |
| "text-generation", | |
| model=model, | |
| tokenizer=tokenizer, | |
| max_new_tokens=512, | |
| temperature=0.3, | |
| top_p=0.95, | |
| repetition_penalty=1.2, | |
| device=device | |
| ) | |
| def llm_generate(prompt: str) -> str: | |
| response = pipe(prompt) | |
| return response[0]["generated_text"].replace(prompt, "").strip() |