from transformers import pipeline import matplotlib.pyplot as plt import twitter_scraper as ts import gradio as gr from gradio.mix import Parallel pretrained_sentiment = "w11wo/indonesian-roberta-base-sentiment-classifier" pretrained_ner = "cahya/bert-base-indonesian-NER" sentiment_pipeline = pipeline( "sentiment-analysis", model=pretrained_sentiment, tokenizer=pretrained_sentiment, return_all_scores=True ) ner_pipeline = pipeline( "ner", model=pretrained_ner, tokenizer=pretrained_ner ) examples = [ "Jokowi sangat kecewa dengan POLRI atas kerusuhan yang terjadi di Malang", "Lesti marah terhadap perlakuan KDRT yang dilakukan oleh Bilar", "Ungkapan rasa bahagia diutarakan oleh Coki Pardede karena kebabasannya dari penjara" ] def sentiment_analysis(text): output = sentiment_pipeline(text) return {elm["label"]: elm["score"] for elm in output[0]} def ner(text): output = ner_pipeline(text) return {"text": text, "entities": output} def sentiment_ner(text): return sentiment_analysis(text), ner(text) def sentiment_df(df): text_list = list(df["Text"].astype(str).values) result = [sentiment_analysis(text) for text in text_list] df['Label'] = [list(pred.keys())[0] for pred in result] df['Score'] = [round(list(pred.values())[0], 3) for pred in result] return df def twitter_analyzer(keyword, max_tweets): df = ts.scrape_tweets(keyword, max_tweets=max_tweets) df["Text"] = df["Text"].apply(ts.preprocess_text) df = sentiment_df(df) fig = plt.figure() df.groupby(["Label"])["Text"].count().plot.pie(autopct="%.1f%%", figsize=(6,6)) return fig, df[["URL", "Text", "Label", "Score"]] if __name__ == "__main__": with gr.Blocks() as demo: gr.Markdown("""