Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,35 @@ headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
|
| 8 |
def query(payload):
|
| 9 |
return requests.post(API_URL, headers=headers, json=payload).json()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def predict_sentiment(text):
|
| 12 |
if not text.strip():
|
| 13 |
return "⚠️ Input kosong"
|
|
|
|
| 14 |
output = query({"inputs": text})
|
| 15 |
try:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except:
|
| 18 |
return str(output)
|
| 19 |
|
|
@@ -22,7 +45,7 @@ with gr.Blocks(title="Twitter Sentiment Analysis") as ui:
|
|
| 22 |
gr.Markdown("Masukkan teks untuk dianalisis sentimennya:")
|
| 23 |
|
| 24 |
input_box = gr.Textbox(lines=3, label="Teks")
|
| 25 |
-
output_box = gr.
|
| 26 |
btn = gr.Button("Analisis Sentimen")
|
| 27 |
|
| 28 |
btn.click(predict_sentiment, inputs=input_box, outputs=output_box)
|
|
|
|
| 8 |
def query(payload):
|
| 9 |
return requests.post(API_URL, headers=headers, json=payload).json()
|
| 10 |
|
| 11 |
+
def colorize(label):
|
| 12 |
+
colors = {
|
| 13 |
+
"positive": "green",
|
| 14 |
+
"negative": "red",
|
| 15 |
+
"neutral": "orange"
|
| 16 |
+
}
|
| 17 |
+
return colors.get(label.lower(), "black")
|
| 18 |
+
|
| 19 |
def predict_sentiment(text):
|
| 20 |
if not text.strip():
|
| 21 |
return "⚠️ Input kosong"
|
| 22 |
+
|
| 23 |
output = query({"inputs": text})
|
| 24 |
try:
|
| 25 |
+
preds = output[0]
|
| 26 |
+
main = max(preds, key=lambda x: x["score"])
|
| 27 |
+
c = colorize(main["label"])
|
| 28 |
+
|
| 29 |
+
main_html = f"""
|
| 30 |
+
<h3>🎯 Prediksi Utama: <span style='color:{c}'>{main['label']}</span> ({main['score']:.4f})</h3>
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
details = ""
|
| 34 |
+
for p in preds:
|
| 35 |
+
col = colorize(p["label"])
|
| 36 |
+
details += f"<div><b style='color:{col}'>{p['label']}</b> → {p['score']:.4f}</div>"
|
| 37 |
+
|
| 38 |
+
return main_html + "<br>" + details
|
| 39 |
+
|
| 40 |
except:
|
| 41 |
return str(output)
|
| 42 |
|
|
|
|
| 45 |
gr.Markdown("Masukkan teks untuk dianalisis sentimennya:")
|
| 46 |
|
| 47 |
input_box = gr.Textbox(lines=3, label="Teks")
|
| 48 |
+
output_box = gr.HTML(label="Hasil Prediksi")
|
| 49 |
btn = gr.Button("Analisis Sentimen")
|
| 50 |
|
| 51 |
btn.click(predict_sentiment, inputs=input_box, outputs=output_box)
|