Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load model AnggaPuspa/aiboss
|
| 5 |
+
MODEL_NAME = "AnggaPuspa/aiboss"
|
| 6 |
+
|
| 7 |
+
print(f"Loading model: {MODEL_NAME}")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 10 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 11 |
+
print("Model loaded!")
|
| 12 |
+
|
| 13 |
+
# Label mapping sesuai web_config.json
|
| 14 |
+
LABEL_MAP = {
|
| 15 |
+
"LABEL_0": "negative",
|
| 16 |
+
"LABEL_1": "neutral",
|
| 17 |
+
"LABEL_2": "positive"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def analyze_sentiment(text: str):
|
| 21 |
+
"""Analyze sentiment of input text"""
|
| 22 |
+
if not text or not text.strip():
|
| 23 |
+
return {"error": "Please provide text to analyze"}
|
| 24 |
+
|
| 25 |
+
results = classifier(text)[0]
|
| 26 |
+
|
| 27 |
+
# Normalize labels
|
| 28 |
+
normalized = []
|
| 29 |
+
for item in results:
|
| 30 |
+
label = LABEL_MAP.get(item["label"], item["label"])
|
| 31 |
+
normalized.append({"label": label, "score": round(item["score"], 4)})
|
| 32 |
+
|
| 33 |
+
# Sort by score
|
| 34 |
+
normalized.sort(key=lambda x: x["score"], reverse=True)
|
| 35 |
+
top = normalized[0]
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"sentiment": top["label"],
|
| 39 |
+
"confidence": top["score"],
|
| 40 |
+
"details": normalized
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Create Gradio interface with API endpoint
|
| 44 |
+
demo = gr.Interface(
|
| 45 |
+
fn=analyze_sentiment,
|
| 46 |
+
inputs=gr.Textbox(label="Input Text", placeholder="Masukkan teks untuk dianalisis..."),
|
| 47 |
+
outputs=gr.JSON(label="Result"),
|
| 48 |
+
title="Sentiment Analysis API - AnggaPuspa/aiboss",
|
| 49 |
+
description="IndoBERT Sentiment Analysis (Negative/Neutral/Positive)",
|
| 50 |
+
api_name="predict" # This creates /api/predict endpoint
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
demo.launch()
|