|
import gradio as gr |
|
import joblib |
|
import numpy as np |
|
from transformers import BertTokenizer, BertModel |
|
import torch |
|
|
|
|
|
svm_model = joblib.load("svm_model5.joblib") |
|
tokenizer = BertTokenizer.from_pretrained("SI2M-Lab/DarijaBERT") |
|
bert_model = BertModel.from_pretrained("SI2M-Lab/DarijaBERT") |
|
|
|
|
|
def get_bert_embedding(text): |
|
with torch.no_grad(): |
|
inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=128) |
|
outputs = bert_model(**inputs) |
|
cls_embedding = outputs.last_hidden_state[:, 0, :].squeeze().cpu().numpy() |
|
return np.array(cls_embedding).reshape(1, -1) |
|
|
|
|
|
def predict(text): |
|
embedding = get_bert_embedding(text) |
|
prediction = svm_model.predict(embedding)[0] |
|
return f"Classe prédite : {prediction}" |
|
|
|
|
|
iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="Classification de texte avec SVM & BERT") |
|
|
|
|
|
iface.launch() |
|
|