Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,52 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""
|
| 3 |
-
Created on Mon Aug 25 14:10:33 2025
|
| 4 |
-
|
| 5 |
-
@author: shuangshuang
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from transformers import pipeline
|
| 9 |
-
import gradio as gr
|
| 10 |
-
|
| 11 |
-
# Load model
|
| 12 |
-
classifier = pipeline(
|
| 13 |
-
"sentiment-analysis",
|
| 14 |
-
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
| 15 |
-
device=-1,
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Map labels
|
| 19 |
-
label_map = {
|
| 20 |
-
"1 star": "very bad",
|
| 21 |
-
"2 stars": "bad",
|
| 22 |
-
"3 stars": "neutral",
|
| 23 |
-
"4 stars": "good",
|
| 24 |
-
"5 stars": "very good",
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Prediction function
|
| 29 |
-
def predict(text):
|
| 30 |
-
text = text.strip()
|
| 31 |
-
if not text:
|
| 32 |
-
return "Please enter some text."
|
| 33 |
-
result = classifier(text)[0]
|
| 34 |
-
label = result["label"]
|
| 35 |
-
score = result["score"]
|
| 36 |
-
mapped = label_map[label]
|
| 37 |
-
return f"Prediction: {mapped}\nConfidence: {score:.2f}"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Gradio interface
|
| 41 |
-
interface = gr.Interface(
|
| 42 |
-
fn=predict,
|
| 43 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
| 44 |
-
outputs="text",
|
| 45 |
-
title="Sentiment Analysis",
|
| 46 |
-
description="Enter a sentence to analyze its sentiment.",
|
| 47 |
-
examples=["I love this product!", "This is terrible."],
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# Launch (Hugging Face Spaces doesn't need special params)
|
| 51 |
-
if __name__ == "__main__":
|
| 52 |
-
interface.launch()
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Mon Aug 25 14:10:33 2025
|
| 4 |
+
|
| 5 |
+
@author: shuangshuang
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
classifier = pipeline(
|
| 13 |
+
"sentiment-analysis",
|
| 14 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
| 15 |
+
device=-1,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Map labels
|
| 19 |
+
label_map = {
|
| 20 |
+
"1 star": "very bad",
|
| 21 |
+
"2 stars": "bad",
|
| 22 |
+
"3 stars": "neutral",
|
| 23 |
+
"4 stars": "good",
|
| 24 |
+
"5 stars": "very good",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Prediction function
|
| 29 |
+
def predict(text):
|
| 30 |
+
text = text.strip()
|
| 31 |
+
if not text:
|
| 32 |
+
return "Please enter some text."
|
| 33 |
+
result = classifier(text)[0]
|
| 34 |
+
label = result["label"]
|
| 35 |
+
score = result["score"]
|
| 36 |
+
mapped = label_map[label]
|
| 37 |
+
return f"Prediction: {mapped}\nConfidence: {score:.2f}"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Gradio interface
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
+
fn=predict,
|
| 43 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
| 44 |
+
outputs="text",
|
| 45 |
+
title="Sentiment Analysis",
|
| 46 |
+
description="Enter a sentence to analyze its sentiment.",
|
| 47 |
+
examples=["I love this product!", "This is terrible."],
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch (Hugging Face Spaces doesn't need special params)
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
interface.launch(ssr_mode=False)
|