Spaces:
Sleeping
Sleeping
Create streamlit-app.py
Browse files- streamlit-app.py +52 -0
streamlit-app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 6 |
+
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 7 |
+
|
| 8 |
+
def query(payload):
|
| 9 |
+
return requests.post(API_URL, headers=headers, json=payload).json()
|
| 10 |
+
|
| 11 |
+
def color(label):
|
| 12 |
+
return {
|
| 13 |
+
"positive": "green",
|
| 14 |
+
"negative": "red",
|
| 15 |
+
"neutral": "orange"
|
| 16 |
+
}.get(label.lower(), "black")
|
| 17 |
+
|
| 18 |
+
st.set_page_config(page_title="Twitter Sentiment Analysis", page_icon="🧠")
|
| 19 |
+
|
| 20 |
+
st.title("🧠 Twitter Sentiment Analysis (RoBERTa)")
|
| 21 |
+
st.write("Masukkan teks untuk dianalisis sentimennya:")
|
| 22 |
+
|
| 23 |
+
text = st.text_area("Teks Input")
|
| 24 |
+
|
| 25 |
+
if st.button("Analisis Sentimen"):
|
| 26 |
+
if not text.strip():
|
| 27 |
+
st.warning("⚠️ Input kosong")
|
| 28 |
+
else:
|
| 29 |
+
output = query({"inputs": text})
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
preds = output[0]
|
| 33 |
+
main = max(preds, key=lambda x: x["score"])
|
| 34 |
+
c = color(main["label"])
|
| 35 |
+
|
| 36 |
+
st.markdown(
|
| 37 |
+
f"<h3>🎯 Prediksi Utama: "
|
| 38 |
+
f"<span style='color:{c}'>{main['label']}</span> ({main['score']:.4f})</h3>",
|
| 39 |
+
unsafe_allow_html=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
st.write("---")
|
| 43 |
+
st.subheader("Detail Skor:")
|
| 44 |
+
|
| 45 |
+
for p in preds:
|
| 46 |
+
st.markdown(
|
| 47 |
+
f"<div><b style='color:{color(p['label'])}'>{p['label']}</b> → {p['score']:.4f}</div>",
|
| 48 |
+
unsafe_allow_html=True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except:
|
| 52 |
+
st.error(output)
|