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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -2,7 +2,8 @@ import streamlit as st
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')
@@ -16,14 +17,36 @@ def load_sentiment_model():
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
@@ -52,4 +75,4 @@ if st.button("πŸ” Analisis Sentimen"):
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")
 
2
  import pandas as pd
3
  import plotly.express as px
4
  from transformers import pipeline
5
+ from deep_translator import GoogleTranslator, exceptions
6
+ import time
7
 
8
  # Konfigurasi Halaman
9
  st.set_page_config(page_title='Sentiment Analysis Dashboard', layout='wide')
 
17
  sentiment_model = load_sentiment_model()
18
  translator = GoogleTranslator(source='auto', target='en')
19
 
20
+ # Fungsi untuk menerjemahkan dengan retry jika terjadi TooManyRequests
21
+
22
+ def safe_translate(text):
23
+ try:
24
+ return translator.translate(text)
25
+ except exceptions.TooManyRequests:
26
+ time.sleep(2) # Tunggu sebelum mencoba lagi
27
+ try:
28
+ return translator.translate(text)
29
+ except exceptions.TooManyRequests:
30
+ return "Terjemahan gagal"
31
+
32
  # Input Kalimat
33
  st.subheader("πŸ“ Masukkan Teks untuk Analisis Sentimen")
34
+ user_input = st.text_area("Masukkan satu atau lebih kalimat (pisahkan dengan enter)", height=200, value="""
35
+ Saya sangat senang dengan layanan yang diberikan!
36
+ Makanannya enak dan pelayanannya luar biasa!
37
+ Produk ini sangat membantu dalam pekerjaan saya.
38
+ Saya kecewa dengan kualitas barang yang saya terima.
39
+ Pelayanannya lambat dan tidak ramah.
40
+ Aplikasi ini sering mengalami crash, sangat mengecewakan!
41
+ Hari ini saya pergi ke kantor seperti biasa.
42
+ Cuaca hari ini cukup cerah.
43
+ Saya membeli buku baru di toko kemarin.
44
+ """)
45
 
46
  if st.button("πŸ” Analisis Sentimen"):
47
  if user_input:
48
  sentences = user_input.strip().split("\n") # Pisahkan per baris
49
+ translated_sentences = [safe_translate(sentence) for sentence in sentences]
50
  results = sentiment_model(translated_sentences)
51
 
52
  # Konversi hasil ke DataFrame
 
75
 
76
  # Tambahkan file requirements.txt untuk deploy di Hugging Face
77
  with open("requirements.txt", "w") as f:
78
+ f.write("streamlit\npandas\nplotly\ntransformers\ntorch\ndeep-translator")