Spaces:
Sleeping
Sleeping
Commit
·
162ebf1
0
Parent(s):
deploying sentiment analysis & emotion prediction apps
Browse files
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
import base64
|
5 |
+
|
6 |
+
# Load pipelines
|
7 |
+
sentiment_pipe = pipeline("text-classification", model="ayameRushia/bert-base-indonesian-1.5G-sentiment-analysis-smsa")
|
8 |
+
emotion_pipe = pipeline("text-classification", model="azizp128/prediksi-emosi-indobert")
|
9 |
+
|
10 |
+
|
11 |
+
def direct_sentiment_analysis(text):
|
12 |
+
texts = text.split('\n') # Memisahkan teks berdasarkan baris
|
13 |
+
|
14 |
+
# Hasil analisis sentiment
|
15 |
+
results = []
|
16 |
+
for text in texts:
|
17 |
+
if text.strip():
|
18 |
+
result = sentiment_pipe(text)[0] # Melakukan analisis sentiment pada setiap teks
|
19 |
+
results.append((text, result['label'].lower(), result['score']))
|
20 |
+
|
21 |
+
# Ubah ke DataFrame untuk tampilan tabel
|
22 |
+
df = pd.DataFrame(results, columns=['Content', 'Sentiment', 'Score'])
|
23 |
+
return df
|
24 |
+
|
25 |
+
|
26 |
+
def direct_emotion_analysis(text):
|
27 |
+
texts = text.split('\n') # Memisahkan teks berdasarkan baris
|
28 |
+
|
29 |
+
# Hasil analisis sentiment
|
30 |
+
results = []
|
31 |
+
for text in texts:
|
32 |
+
if text.strip():
|
33 |
+
result = emotion_pipe(text)[0] # Melakukan analisis sentiment pada setiap teks
|
34 |
+
results.append((text, result['label'].lower(), result['score']))
|
35 |
+
|
36 |
+
# Ubah ke DataFrame untuk tampilan tabel
|
37 |
+
df = pd.DataFrame(results, columns=['Content', 'Emotion', 'Score'])
|
38 |
+
return df
|
39 |
+
|
40 |
+
def process_file_sentiment(file):
|
41 |
+
if file.name.endswith('.xlsx'):
|
42 |
+
df = pd.read_excel(file) # Baca file XLSX
|
43 |
+
elif file.name.endswith('.csv'):
|
44 |
+
df = pd.read_csv(file) # Baca file CSV
|
45 |
+
else:
|
46 |
+
st.error("Format file tidak didukung. Harap unggah file CSV atau XLSX.")
|
47 |
+
return None
|
48 |
+
|
49 |
+
# Analisis sentimen dan tambahkan hasil ke DataFrame
|
50 |
+
results = []
|
51 |
+
for index, row in df.iterrows():
|
52 |
+
if pd.notna(row['content']) and isinstance(row['content'], str):
|
53 |
+
sentiment, score = analyze_sentiment(row['content'])
|
54 |
+
results.append((row['content'], sentiment, score))
|
55 |
+
else:
|
56 |
+
results.append((row['content'], None, None)) # Menambahkan nilai None jika kosong
|
57 |
+
|
58 |
+
df['Sentimen'] = [r[1] for r in results]
|
59 |
+
df['Skor Sentimen'] = [r[2] for r in results]
|
60 |
+
|
61 |
+
return df
|
62 |
+
|
63 |
+
def process_file_emotion(file):
|
64 |
+
if file.name.endswith('.xlsx'):
|
65 |
+
df = pd.read_excel(file) # Baca file XLSX
|
66 |
+
elif file.name.endswith('.csv'):
|
67 |
+
df = pd.read_csv(file) # Baca file CSV
|
68 |
+
else:
|
69 |
+
st.error("Format file tidak didukung. Harap unggah file CSV atau XLSX.")
|
70 |
+
return None
|
71 |
+
|
72 |
+
# Prediksi emosi dan tambahkan hasil ke DataFrame
|
73 |
+
results = []
|
74 |
+
for index, row in df.iterrows():
|
75 |
+
if pd.notna(row['content']) and isinstance(row['content'], str):
|
76 |
+
emotion, score = emotion_prediction(row['content'])
|
77 |
+
results.append((row['content'], emotion, score))
|
78 |
+
else:
|
79 |
+
results.append((row['content'], None, None)) # Menambahkan nilai None jika kosong
|
80 |
+
|
81 |
+
df['Emosi'] = [r[1] for r in results]
|
82 |
+
df['Skor Emosi'] = [r[2] for r in results]
|
83 |
+
|
84 |
+
return df
|
85 |
+
|
86 |
+
def analyze_sentiment(text):
|
87 |
+
result = sentiment_pipe(text)[0]
|
88 |
+
return result['label'].lower(), result['score']
|
89 |
+
|
90 |
+
def emotion_prediction(text):
|
91 |
+
result = emotion_pipe(text)[0]
|
92 |
+
return result['label'].lower(), result['score']
|
93 |
+
|
94 |
+
def get_download_link_sentiment(df):
|
95 |
+
# Generate a link to download the dataframe with Sentimen and Skor Sentimen as CSV
|
96 |
+
csv = df.to_csv(index=False)
|
97 |
+
b64 = base64.b64encode(csv.encode()).decode() # Encode CSV to base64
|
98 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="analisis_sentimen.csv">Download CSV</a>'
|
99 |
+
return href
|
100 |
+
|
101 |
+
def get_download_link_emotion(df):
|
102 |
+
# Generate a link to download the dataframe with Emosi and Skor Emosi as CSV
|
103 |
+
csv = df.to_csv(index=False)
|
104 |
+
b64 = base64.b64encode(csv.encode()).decode() # Encode CSV to base64
|
105 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="prediksi_emosi.csv">Download CSV</a>'
|
106 |
+
return href
|
107 |
+
|
108 |
+
def main():
|
109 |
+
st.title("Aplikasi Analisis Sentimen dan Prediksi Emosi by Ramdhani")
|
110 |
+
|
111 |
+
# Pilihan Program
|
112 |
+
program = st.sidebar.selectbox("Pilih Program", ["Analisis Sentiment", "Prediksi Emosi"])
|
113 |
+
|
114 |
+
if program == "Analisis Sentiment":
|
115 |
+
# Menu untuk analisis sentimen
|
116 |
+
st.header("Analisis Sentiment")
|
117 |
+
menu_sentiment = st.sidebar.selectbox("Pilih Metode", ["Analisis Langsung", "Import dari File"])
|
118 |
+
|
119 |
+
if menu_sentiment == "Analisis Langsung":
|
120 |
+
# Masukan teks untuk analisis sentimen
|
121 |
+
user_input = st.text_area("Masukkan teks yang ingin dianalisis (pisahkan dengan enter):")
|
122 |
+
|
123 |
+
if st.button("Analisis Sentimen"):
|
124 |
+
df = direct_sentiment_analysis(user_input)
|
125 |
+
st.write("Hasil Analisis Sentimen:")
|
126 |
+
st.write(df)
|
127 |
+
|
128 |
+
# Tambahkan tombol download CSV
|
129 |
+
st.markdown(get_download_link_sentiment(df), unsafe_allow_html=True)
|
130 |
+
|
131 |
+
elif menu_sentiment == "Import dari File":
|
132 |
+
st.subheader("Import dari File")
|
133 |
+
uploaded_file = st.file_uploader("Upload file CSV atau XLSX", type=["csv", "xlsx"])
|
134 |
+
|
135 |
+
if uploaded_file is not None:
|
136 |
+
df = process_file_sentiment(uploaded_file)
|
137 |
+
|
138 |
+
# Tampilkan hasil analisis sentimen
|
139 |
+
st.write("Hasil Analisis Sentimen:")
|
140 |
+
st.write(df)
|
141 |
+
|
142 |
+
# Tambahkan tombol download CSV
|
143 |
+
st.markdown(get_download_link_sentiment(df), unsafe_allow_html=True)
|
144 |
+
|
145 |
+
elif program == "Prediksi Emosi":
|
146 |
+
# Menu untuk prediksi emosi
|
147 |
+
st.header("Prediksi Emosi")
|
148 |
+
menu_emot = st.sidebar.selectbox("Pilih Metode", ["Prediksi Langsung", "Import dari File"])
|
149 |
+
|
150 |
+
if menu_emot == "Prediksi Langsung":
|
151 |
+
user_input = st.text_area("Masukkan teks yang ingin dianalisis (pisahkan dengan enter):")
|
152 |
+
|
153 |
+
if st.button("Analisis Sentimen"):
|
154 |
+
df = direct_emotion_analysis(user_input)
|
155 |
+
st.write("Hasil Analisis Sentimen:")
|
156 |
+
st.write(df)
|
157 |
+
|
158 |
+
# Tambahkan tombol download CSV
|
159 |
+
st.markdown(get_download_link_emotion(df), unsafe_allow_html=True)
|
160 |
+
|
161 |
+
elif menu_emot == "Import dari File":
|
162 |
+
st.subheader("Import dari File")
|
163 |
+
uploaded_file = st.file_uploader("Upload file CSV atau XLSX", type=["csv", "xlsx"])
|
164 |
+
|
165 |
+
if uploaded_file is not None:
|
166 |
+
df = process_file_emotion(uploaded_file)
|
167 |
+
|
168 |
+
# Tampilkan hasil prediksi emosi
|
169 |
+
st.write("Hasil Prediksi Emosi:")
|
170 |
+
st.write(df)
|
171 |
+
|
172 |
+
# Tambahkan tombol download CSV
|
173 |
+
st.markdown(get_download_link_emotion(df), unsafe_allow_html=True)
|
174 |
+
|
175 |
+
if __name__ == "__main__":
|
176 |
+
main()
|