CoolFace
Apppublic

TugasDeeplearning/Multitask_Sentiment_MultiBahasa

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py54 linesDownload Raw Back to root
1import gradio as gr2import torch3import joblib4from transformers import AutoTokenizer5from dinstilBert import MultiTaskBERT6 7model = MultiTaskBERT()8model.load_state_dict(torch.load("model.pt", map_location="cpu"))9model.eval()10 11tokenizer = AutoTokenizer.from_pretrained("distilbert-base-multilingual-cased")12le = joblib.load("label_encoder.pkl")13device = torch.device("cuda" if torch.cuda.is_available() else "cpu")14model.to(device)15 16def predict(text):17    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)18    with torch.no_grad():19        sentiment_logits, lang_logits = model(inputs["input_ids"], inputs["attention_mask"])20        pred_sentiment = sentiment_logits.argmax(dim=1).item()21        pred_lang = lang_logits.argmax(dim=1).item()22 23    if pred_sentiment == 2:24        sentiment_label = "positive"25    elif pred_sentiment == 1:26        sentiment_label = "neutral"27    else:28        sentiment_label = "negative"29 30    lang_code_map = {31        'de': 'German',32        'es': 'Espanyol',33        'en': 'English',34        'fr': 'French'35    }36        37    lang_code = le.inverse_transform([pred_lang])[0]38    lang_label = lang_code_map.get(lang_code, "Unknown")39 40    return sentiment_label, lang_label41 42 43interface = gr.Interface(44    fn=predict,45    inputs=gr.Textbox(label="Masukkan Teks Dalam Bahasa (Inggris/Jerman/Spanyol/Perancis)"),46    outputs=[47        gr.Textbox(label="Prediksi Sentiment (Positif/Neutral/Negatif)"),48        gr.Textbox(label="Prediksi Bahasa")49    ],50    title="Multitask DistilBERT: Sentiment + Language",51    description="Prediksi sentimen dan bahasa dari teks menggunakan model multitask DistilBERT."52)53 54interface.launch()