import requests import gradio as gr import preprocessor as tweet_cleaner # from transformers import pipeline # pretrained_name = "w11wo/indonesian-roberta-base-sentiment-classifier" # sentiment = pipeline( # "sentiment-analysis", # model=pretrained_name, # tokenizer=pretrained_name, # max_length=512, # truncation=True, # ) API_URL = "https://api-inference.huggingface.co/models/w11wo/indonesian-roberta-base-sentiment-classifier" headers = {"Authorization": "Bearer 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"} def format_sentiment(predictions): formatted_output = dict() for p in predictions: if p['label'] == 'positive': formatted_output['Positif'] = p['score'] elif p['label'] == 'negative': formatted_output['Negatif'] = p['score'] else: formatted_output['Netral'] = p['score'] return formatted_output def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() def clean_tweet(tweet): return tweet_cleaner.clean(tweet) def get_sentiment(input_text): res = query({"inputs": clean_tweet(input_text)}) formatted_output = format_sentiment(res[0]) return formatted_output examples = list() examples.append("Semoga saja pelayanan BPJS ke depannya semakin baik. #BPJSKesehatan #TerimaKasihBPJS #BPJSMelayani https://t.co/iDETFSXFJR") examples.append("min ini mau bayar ko ga bisa yaa m banking sama shopee nya kenapa. Help min udah tenggat nih") examples.append("Kenaikan harga bpjs yg makin mahal bikin rakyat jadi tambah sengsara pak!") iface = gr.Interface( fn = get_sentiment, inputs = 'text', outputs = ['label'], title = 'Analisis Sentimen Twitter', description="Dapatkan sentimen positif, negatif, atau netral untuk tweet yang dimasukkan.", examples=examples ) iface.launch(inline = False)