root
commited on
Commit
·
80676ae
1
Parent(s):
127257c
Ajout du fichier handler.py
Browse files- handler.py +26 -0
handler.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Charger le modèle et le tokenizer
|
| 6 |
+
model_name = "AIDC-AI/Ovis1.6-Gemma2-9B" # Remplacez par le nom de votre modèle
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
|
| 10 |
+
# Fonction d'initialisation
|
| 11 |
+
def init():
|
| 12 |
+
global tokenizer, model
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 15 |
+
model.eval() # Définir le modèle en mode évaluation
|
| 16 |
+
|
| 17 |
+
# Fonction de prédiction
|
| 18 |
+
def predict(data):
|
| 19 |
+
inputs = data.get("inputs")
|
| 20 |
+
if isinstance(inputs, str):
|
| 21 |
+
inputs = tokenizer(inputs, return_tensors="pt")
|
| 22 |
+
|
| 23 |
+
outputs = model.generate(**inputs)
|
| 24 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
return json.dumps({"result": result})
|