abizaregi7 commited on
Commit
c178c9a
Β·
verified Β·
1 Parent(s): e71b29b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -29
app.py CHANGED
@@ -2,49 +2,54 @@ import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
  from transformers import pipeline
 
5
 
6
  # Konfigurasi Halaman
7
  st.set_page_config(page_title='Sentiment Analysis Dashboard', layout='wide')
8
  st.title('πŸ’¬ Sentiment Analysis Dashboard')
9
 
10
- # Inisialisasi Model Sentimen untuk Bahasa Indonesia
11
  @st.cache_resource
12
  def load_sentiment_model():
13
- return pipeline("sentiment-analysis", model="w11wo/indonesian-roberta-base-sentiment-classifier")
14
 
15
  sentiment_model = load_sentiment_model()
 
16
 
17
  # Input Kalimat
18
  st.subheader("πŸ“ Masukkan Teks untuk Analisis Sentimen")
19
  user_input = st.text_area("Masukkan satu atau lebih kalimat (pisahkan dengan enter)", height=200)
20
 
21
- if user_input:
22
- sentences = user_input.strip().split("\n") # Pisahkan per baris
23
- results = sentiment_model(sentences)
24
-
25
- # Konversi hasil ke DataFrame
26
- df_results = pd.DataFrame({
27
- "Kalimat": sentences,
28
- "Sentimen": [res['label'] for res in results],
29
- "Skor": [res['score'] for res in results]
30
- })
31
-
32
- # Tampilkan hasil
33
- st.subheader("πŸ“Š Hasil Analisis Sentimen")
34
- st.dataframe(df_results)
35
-
36
- # Visualisasi Sentimen
37
- st.subheader("πŸ“ˆ Visualisasi Sentimen")
38
- fig = px.bar(df_results, x="Kalimat", y="Skor", color="Sentimen", title="Distribusi Sentimen",
39
- labels={'Kalimat': 'Teks', 'Skor': 'Confidence Score'}, text_auto=True)
40
- st.plotly_chart(fig, use_container_width=True)
41
-
42
- # Unduh hasil
43
- st.subheader("πŸ“₯ Unduh Hasil")
44
- st.download_button("Download CSV", df_results.to_csv(index=False).encode("utf-8"), "sentiment_results.csv", "text/csv")
45
- else:
46
- st.info("Masukkan teks untuk memulai analisis sentimen.")
 
 
 
47
 
48
  # Tambahkan file requirements.txt untuk deploy di Hugging Face
49
  with open("requirements.txt", "w") as f:
50
- f.write("streamlit\npandas\nplotly\ntransformers\ntorch")
 
2
  import pandas as pd
3
  import plotly.express as px
4
  from transformers import pipeline
5
+ from deep_translator import GoogleTranslator
6
 
7
  # Konfigurasi Halaman
8
  st.set_page_config(page_title='Sentiment Analysis Dashboard', layout='wide')
9
  st.title('πŸ’¬ Sentiment Analysis Dashboard')
10
 
11
+ # Inisialisasi Model Sentimen
12
  @st.cache_resource
13
  def load_sentiment_model():
14
+ return pipeline("sentiment-analysis")
15
 
16
  sentiment_model = load_sentiment_model()
17
+ translator = GoogleTranslator(source='auto', target='en')
18
 
19
  # Input Kalimat
20
  st.subheader("πŸ“ Masukkan Teks untuk Analisis Sentimen")
21
  user_input = st.text_area("Masukkan satu atau lebih kalimat (pisahkan dengan enter)", height=200)
22
 
23
+ if st.button("πŸ” Analisis Sentimen"):
24
+ if user_input:
25
+ sentences = user_input.strip().split("\n") # Pisahkan per baris
26
+ translated_sentences = [translator.translate(sentence) for sentence in sentences]
27
+ results = sentiment_model(translated_sentences)
28
+
29
+ # Konversi hasil ke DataFrame
30
+ df_results = pd.DataFrame({
31
+ "Kalimat Asli": sentences,
32
+ "Terjemahan": translated_sentences,
33
+ "Sentimen": [res['label'] for res in results],
34
+ "Skor": [res['score'] for res in results]
35
+ })
36
+
37
+ # Tampilkan hasil
38
+ st.subheader("πŸ“Š Hasil Analisis Sentimen")
39
+ st.dataframe(df_results)
40
+
41
+ # Visualisasi Sentimen
42
+ st.subheader("πŸ“ˆ Visualisasi Sentimen")
43
+ fig = px.bar(df_results, x="Kalimat Asli", y="Skor", color="Sentimen", title="Distribusi Sentimen",
44
+ labels={'Kalimat Asli': 'Teks', 'Skor': 'Confidence Score'}, text_auto=True)
45
+ st.plotly_chart(fig, use_container_width=True)
46
+
47
+ # Unduh hasil
48
+ st.subheader("πŸ“₯ Unduh Hasil")
49
+ st.download_button("Download CSV", df_results.to_csv(index=False).encode("utf-8"), "sentiment_results.csv", "text/csv")
50
+ else:
51
+ st.warning("Silakan masukkan teks terlebih dahulu.")
52
 
53
  # Tambahkan file requirements.txt untuk deploy di Hugging Face
54
  with open("requirements.txt", "w") as f:
55
+ f.write("streamlit\npandas\nplotly\ntransformers\ntorch\ndeep-translator")