Spaces:
Sleeping
Sleeping
File size: 3,276 Bytes
5627533 b7e77f6 5627533 b7e77f6 eff9a36 b7e77f6 eff9a36 b7e77f6 bec3498 5627533 b7e77f6 eff9a36 b7e77f6 bec3498 eff9a36 b7e77f6 bec3498 eff9a36 b7e77f6 eff9a36 b7e77f6 b21d1a6 b7e77f6 bec3498 b7e77f6 c178c9a bec3498 eff9a36 b7e77f6 eff9a36 b7e77f6 b21d1a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import streamlit as st
import pandas as pd
import plotly.express as px
from textblob import TextBlob
# Konfigurasi Halaman
st.set_page_config(page_title='Sentiment Analysis Dashboard', layout='wide')
st.title('π¬ Sentiment Analysis Dashboard')
# Inisialisasi session_state untuk menyimpan riwayat analisis
if "results" not in st.session_state:
st.session_state["results"] = []
# Input Kalimat
st.subheader("π Masukkan Teks untuk Analisis Sentimen")
user_input = st.text_area("Masukkan satu atau lebih kalimat dalam bahasa Inggris (pisahkan dengan enter)", height=200)
if st.button("π Analisis Sentimen"):
text = user_input.strip()
if text:
sentences = text.split("\n") # Pisahkan per baris
results = []
for sentence in sentences:
blob = TextBlob(sentence)
sentiment_score = blob.sentiment.polarity
sentiment = "Positif" if sentiment_score > 0 else "Negatif" if sentiment_score < 0 else "Netral"
results.append({
"Kalimat": sentence,
"Sentimen": sentiment,
"Skor": sentiment_score
})
# Simpan hasil dalam session_state
st.session_state["results"].extend(results)
df_results = pd.DataFrame(results)
# Tampilkan hasil
st.subheader("π Hasil Analisis Sentimen")
st.dataframe(df_results)
# Menampilkan kesimpulan
sentiment_counts = df_results["Sentimen"].value_counts().to_dict()
total_positif = sentiment_counts.get("Positif", 0)
total_negatif = sentiment_counts.get("Negatif", 0)
total_netral = sentiment_counts.get("Netral", 0)
st.markdown(f"""
<div style="border:1px solid #ccc; padding: 16px; border-radius: 8px; background-color: #f9f9f9;">
<h3>π Kesimpulan</h3>
<ul>
<li><span style='color:green; font-weight:bold;'>Positif:</span> {total_positif}</li>
<li><span style='color:red; font-weight:bold;'>Negatif:</span> {total_negatif}</li>
<li><span style='color:blue; font-weight:bold;'>Netral:</span> {total_netral}</li>
</ul>
</div>
""", unsafe_allow_html=True)
# Visualisasi Sentimen
st.subheader("π Visualisasi Sentimen")
fig = px.bar(df_results, x="Kalimat", y="Skor", color="Sentimen", title="Distribusi Sentimen",
labels={'Kalimat': 'Teks', 'Skor': 'Confidence Score'}, text_auto=True)
st.plotly_chart(fig, use_container_width=True)
# Unduh hasil
st.subheader("π₯ Unduh Hasil")
st.download_button("Download CSV", df_results.to_csv(index=False).encode("utf-8"), "sentiment_results.csv", "text/csv")
else:
st.warning("Silakan masukkan teks terlebih dahulu dalam bahasa Inggris.")
# Tombol Reset hanya ditampilkan jika ada riwayat
if st.session_state["results"]:
if st.button("π Reset Riwayat"):
st.session_state["results"] = []
st.success("β
Riwayat telah direset.")
# Tambahkan file requirements.txt untuk deploy di Hugging Face
with open("requirements.txt", "w") as f:
f.write("streamlit\npandas\nplotly\ntextblob") |