Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,31 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
def main():
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.title("Sentiment Analysis with HuggingFace Spaces")
|
| 8 |
st.write("Enter a sentence to analyze its sentiment:")
|
| 9 |
|
| 10 |
user_input = st.text_input("")
|
| 11 |
if user_input:
|
| 12 |
-
result = sentiment_pipeline(user_input)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if __name__ == "__main__":
|
| 20 |
-
main()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
def main():
|
| 5 |
+
# 1. 建立情感分析 pipeline
|
| 6 |
+
sentiment_pipeline = pipeline(
|
| 7 |
+
task="sentiment-analysis",
|
| 8 |
+
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
|
| 9 |
+
)
|
| 10 |
|
| 11 |
st.title("Sentiment Analysis with HuggingFace Spaces")
|
| 12 |
st.write("Enter a sentence to analyze its sentiment:")
|
| 13 |
|
| 14 |
user_input = st.text_input("")
|
| 15 |
if user_input:
|
| 16 |
+
result = sentiment_pipeline(user_input)[0]
|
| 17 |
+
label = result["label"] # e.g. "LABEL_0" or "LABEL_1"
|
| 18 |
+
score = result["score"]
|
| 19 |
|
| 20 |
+
# 2. 映射成更友好的文本
|
| 21 |
+
label_map = {
|
| 22 |
+
"LABEL_0": "Negative",
|
| 23 |
+
"LABEL_1": "Positive"
|
| 24 |
+
}
|
| 25 |
+
friendly_label = label_map.get(label, label)
|
| 26 |
+
|
| 27 |
+
# 3. 输出
|
| 28 |
+
st.write(f"Sentiment: **{friendly_label}**")
|
| 29 |
+
st.write(f"Confidence: **{score:.2f}**")
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
+
main()
|