Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +106 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tqdm import tqdm
|
2 |
+
from itertools import islice
|
3 |
+
from youtube_comment_downloader import *
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
5 |
+
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import csv
|
8 |
+
import streamlit as st
|
9 |
+
import pandas as pd
|
10 |
+
import base64
|
11 |
+
|
12 |
+
|
13 |
+
# Inisialisasi model dan tokenizer
|
14 |
+
pretrained= "mdhugol/indonesia-bert-sentiment-classification"
|
15 |
+
model = AutoModelForSequenceClassification.from_pretrained(pretrained)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(pretrained)
|
17 |
+
sentiment_analysis = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
18 |
+
label_index = {'LABEL_0': 'positive', 'LABEL_1': 'neutral', 'LABEL_2': 'negative'}
|
19 |
+
|
20 |
+
st.title("Youtube Comment Sentimen Analisis")
|
21 |
+
|
22 |
+
# Input URL video
|
23 |
+
video_url = st.text_input("Masukkan URL video YouTube:")
|
24 |
+
|
25 |
+
# Input jumlah komentar yang ingin diambil
|
26 |
+
num_comments = st.number_input("Jumlah komentar yang ingin diambil:", min_value=1, value=10)
|
27 |
+
|
28 |
+
# Fungsi untuk analisis sentimen
|
29 |
+
def analisis_sentimen(text):
|
30 |
+
result = sentiment_analysis(text)
|
31 |
+
label = label_index[result[0]['label']]
|
32 |
+
score = result[0]['score'] * 100
|
33 |
+
return label, score
|
34 |
+
|
35 |
+
if st.button("Mulai Analisis"):
|
36 |
+
# Inisialisasi YoutubeCommentDownloader
|
37 |
+
downloader = YoutubeCommentDownloader()
|
38 |
+
|
39 |
+
# Mendapatkan komentar
|
40 |
+
comments = downloader.get_comments_from_url(video_url, sort_by=SORT_BY_POPULAR)
|
41 |
+
|
42 |
+
# Membuka file CSV untuk menulis
|
43 |
+
with open('comments.csv', mode='w', encoding='utf-8', newline='') as file:
|
44 |
+
# Membuat objek writer
|
45 |
+
writer = csv.DictWriter(file, fieldnames=['cid', 'text', 'time', 'author', 'channel', 'votes', 'photo', 'heart', 'reply'])
|
46 |
+
|
47 |
+
# Menulis header
|
48 |
+
writer.writeheader()
|
49 |
+
|
50 |
+
# Menulis data komentar
|
51 |
+
for comment in tqdm(islice(comments, num_comments)):
|
52 |
+
# Menghapus kolom 'time_parsed' dari komentar
|
53 |
+
comment.pop('time_parsed', None)
|
54 |
+
writer.writerow(comment)
|
55 |
+
|
56 |
+
st.success(f"Komentar berhasil diunduh dan disimpan dalam file 'comments.csv'")
|
57 |
+
|
58 |
+
# Membaca data dari file CSV
|
59 |
+
comments_df = pd.read_csv('comments.csv')
|
60 |
+
|
61 |
+
#analisis sentimen
|
62 |
+
st.info("Memulai analisis sentimen....")
|
63 |
+
|
64 |
+
# List untuk menyimpan hasil analisis sentimen
|
65 |
+
hasil_analisis = []
|
66 |
+
|
67 |
+
# Membaca data dari file CSV
|
68 |
+
with open('comments.csv', mode='r', encoding='utf-8') as file:
|
69 |
+
reader = csv.DictReader(file)
|
70 |
+
for row in tqdm(reader):
|
71 |
+
comment_text = row['text']
|
72 |
+
label, score = analisis_sentimen(comment_text)
|
73 |
+
hasil_analisis.append((comment_text, label, score))
|
74 |
+
|
75 |
+
# Menampilkan hasil analisis sentimen
|
76 |
+
st.subheader("Hasil Analisis Sentimen")
|
77 |
+
#st.write(hasil_analisis)
|
78 |
+
|
79 |
+
# Menampilkan histogram
|
80 |
+
labels, scores = zip(*[(label, score) for _, label, score in hasil_analisis])
|
81 |
+
plt.hist(labels, bins=30, color='blue', alpha=0.7, edgecolor='black')
|
82 |
+
plt.xlabel('Skor Sentimen')
|
83 |
+
plt.ylabel('Jumlah Komentar')
|
84 |
+
plt.title('Distribusi Sentimen Komentar')
|
85 |
+
st.pyplot(plt)
|
86 |
+
|
87 |
+
# Menghitung jumlah dan persentase
|
88 |
+
jumlah_positif = labels.count('positive')
|
89 |
+
jumlah_negatif = labels.count('negative')
|
90 |
+
jumlah_netral = labels.count('neutral')
|
91 |
+
total_komentar = len(labels)
|
92 |
+
persentase_positif = (jumlah_positif / total_komentar) * 100
|
93 |
+
persentase_negatif = (jumlah_negatif / total_komentar) * 100
|
94 |
+
persentase_netral = (jumlah_netral / total_komentar) * 100
|
95 |
+
|
96 |
+
st.write(f"Total Komentar: {total_komentar}")
|
97 |
+
st.write(f"Persentase Komentar Positif: {persentase_positif:.2f}% / {jumlah_positif} Komentar")
|
98 |
+
st.write(f"Persentase Komentar Negatif: {persentase_negatif:.2f}% / {jumlah_negatif} Komentar")
|
99 |
+
st.write(f"Persentase Komentar Netral: {persentase_netral:.2f}% / {jumlah_netral} Komentar")
|
100 |
+
|
101 |
+
# Menampilkan tabel dengan menggunakan st.table()
|
102 |
+
st.subheader("Data Komentar")
|
103 |
+
st.table(comments_df)
|
104 |
+
|
105 |
+
|
106 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
youtube-comment-downloader
|
2 |
+
tqdm
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
matplotlib
|