| import torch |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| import gradio as gr |
|
|
| |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| |
| model_name = "bigcode/starcoder2-15b-instruct-v0.1" |
| tokenizer = AutoTokenizer.from_pretrained(model_name) |
| model = AutoModelForCausalLM.from_pretrained( |
| model_name, |
| torch_dtype=torch.float16 if device == "cuda" else torch.float32 |
| ).to(device) |
|
|
| |
| def generate_text(prompt): |
| inputs = tokenizer(prompt, return_tensors="pt").to(device) |
| outputs = model.generate(inputs["input_ids"], max_length=200) |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
| |
| interface = gr.Interface( |
| fn=generate_text, |
| inputs=gr.Textbox(label="Entrez votre instruction"), |
| outputs=gr.Textbox(label="Résultat généré"), |
| title="StarCoder2-15B-Instruct" |
| ) |
|
|
| |
| interface.launch() |
|
|