create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Charger le modèle
|
| 6 |
+
model_name = "bigcode/starcoder2-15b-instruct-v0.1"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Fonction pour générer du texte
|
| 15 |
+
def generate_text(prompt):
|
| 16 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 17 |
+
outputs = model.generate(inputs["input_ids"], max_length=200)
|
| 18 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
|
| 20 |
+
# Interface utilisateur Gradio
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=generate_text,
|
| 23 |
+
inputs=gr.Textbox(label="Entrez votre instruction"),
|
| 24 |
+
outputs=gr.Textbox(label="Résultat généré"),
|
| 25 |
+
title="StarCoder2-15B-Instruct"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Lancer l'application
|
| 29 |
+
interface.launch()
|