Spaces:
Sleeping
Sleeping
Download app.py from abizaregi7/sentiment-analysis: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/abizaregi7/sentiment-analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/abizaregi7/sentiment-analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abizaregi7/sentiment-analysis/resolve/main/app.py
3.28 kB
| 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") |