-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (35 loc) · 1.36 KB
/
app.py
File metadata and controls
43 lines (35 loc) · 1.36 KB
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
from transformers import pipeline
import gradio as gr
# 1. Model translate (multi-bahasa → Inggris)
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
# 2. Model sentiment (khusus bahasa Inggris)
sentiment_model = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
# Fungsi analisis dengan translasi
def analyze_translated_sentiment(text):
# Translate dulu ke Inggris
translation = translator(text, max_length=512)[0]["translation_text"]
# Analisis sentimen pakai hasil translate
result = sentiment_model(translation)[0]
label = result['label']
score = round(result['score'], 3)
# Interpretasi label
interpretation = {
"NEGATIVE": "😡 Negatif",
"POSITIVE": "🥰 Positif"
}
return f"Teks asli: {text}\n" + \
f"Terjemahan: {translation}\n" + \
f"Hasil: {label} ({score})\nInterpretasi: {interpretation.get(label, 'Tidak diketahui')}"
# UI Gradio
interface = gr.Interface(
fn=analyze_translated_sentiment,
inputs=gr.Textbox(lines=3, placeholder="Tulis teks Bahasa Indonesia atau lainnya..."),
outputs="text",
title="🌐 Sentiment Analysis (Translated)",
description="Menganalisis sentimen teks multibahasa dengan menerjemahkannya terlebih dahulu ke Bahasa Inggris."
)
if __name__ == "__main__":
interface.launch()