AnggaPuspa commited on
Commit
989db16
·
verified ·
1 Parent(s): d918a60
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -7,10 +7,21 @@ MODEL_NAME = "AnggaPuspa/aiboss"
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",
@@ -63,10 +74,10 @@ def analyze_batch(texts_json: str):
63
  if len(texts) > 100:
64
  return {"error": f"Batch size too large ({len(texts)}). Max 100 texts per batch."}
65
 
66
- # Truncate texts to 512 chars to prevent tokenizer issues
67
- clean_texts = [str(t)[:512] if t else "" for t in texts]
68
 
69
- # Run batch inference (MUCH faster than one-by-one!)
70
  try:
71
  all_preds = classifier(clean_texts)
72
 
@@ -90,16 +101,29 @@ def analyze_batch(texts_json: str):
90
  except Exception as e:
91
  return {"error": f"Batch processing failed: {str(e)}"}
92
 
93
- # Create Gradio interface with BOTH single and batch endpoints
94
- with gr.Blocks() as demo:
95
- gr.Markdown("# Sentiment Analysis API - AnggaPuspa/aiboss")
96
- gr.Markdown("IndoBERT Sentiment Analysis (Negative/Neutral/Positive)")
97
 
98
  with gr.Tab("Single Text"):
99
- single_input = gr.Textbox(label="Input Text", placeholder="Masukkan teks untuk dianalisis...")
 
 
 
 
100
  single_output = gr.JSON(label="Result")
101
- single_btn = gr.Button("Analyze")
102
  single_btn.click(fn=analyze_sentiment, inputs=single_input, outputs=single_output, api_name="predict")
 
 
 
 
 
 
 
 
 
103
 
104
  with gr.Tab("Batch Processing"):
105
  batch_input = gr.Textbox(
@@ -108,7 +132,7 @@ with gr.Blocks() as demo:
108
  lines=5
109
  )
110
  batch_output = gr.JSON(label="Results")
111
- batch_btn = gr.Button("Analyze Batch")
112
  batch_btn.click(fn=analyze_batch, inputs=batch_input, outputs=batch_output, api_name="predict_batch")
113
 
114
  demo.launch()
 
7
  print(f"Loading model: {MODEL_NAME}")
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
 
 
10
 
11
+ # Use max_length dari web_config (128)
12
+ MAX_LENGTH = 128
13
+ classifier = pipeline(
14
+ "text-classification",
15
+ model=model,
16
+ tokenizer=tokenizer,
17
+ return_all_scores=True,
18
+ truncation=True,
19
+ max_length=MAX_LENGTH
20
+ )
21
+ print(f"Model loaded! Max length: {MAX_LENGTH}")
22
+
23
+ # Label mapping sesuai web_config.json - IndoBERT Sentiment Sawit
24
+ # 0 = negative, 1 = neutral, 2 = positive
25
  LABEL_MAP = {
26
  "LABEL_0": "negative",
27
  "LABEL_1": "neutral",
 
74
  if len(texts) > 100:
75
  return {"error": f"Batch size too large ({len(texts)}). Max 100 texts per batch."}
76
 
77
+ # Clean and truncate texts
78
+ clean_texts = [str(t).strip()[:MAX_LENGTH * 4] if t else "" for t in texts] # Approx char limit
79
 
80
+ # Run batch inference
81
  try:
82
  all_preds = classifier(clean_texts)
83
 
 
101
  except Exception as e:
102
  return {"error": f"Batch processing failed: {str(e)}"}
103
 
104
+ # Create Gradio interface
105
+ with gr.Blocks(title="IndoBERT Sentiment Sawit") as demo:
106
+ gr.Markdown("# 🌴 Sentiment Analysis API - IndoBERT Sentiment Sawit")
107
+ gr.Markdown("Model: `AnggaPuspa/aiboss` | Labels: Negative / Neutral / Positive")
108
 
109
  with gr.Tab("Single Text"):
110
+ single_input = gr.Textbox(
111
+ label="Input Text",
112
+ placeholder="Masukkan teks untuk dianalisis...",
113
+ lines=3
114
+ )
115
  single_output = gr.JSON(label="Result")
116
+ single_btn = gr.Button("🔍 Analyze", variant="primary")
117
  single_btn.click(fn=analyze_sentiment, inputs=single_input, outputs=single_output, api_name="predict")
118
+
119
+ gr.Examples(
120
+ examples=[
121
+ "Sawit sangat bagus untuk ekonomi Indonesia",
122
+ "Harga sawit terus menurun, petani rugi besar",
123
+ "Sawit adalah komoditas yang biasa saja"
124
+ ],
125
+ inputs=single_input
126
+ )
127
 
128
  with gr.Tab("Batch Processing"):
129
  batch_input = gr.Textbox(
 
132
  lines=5
133
  )
134
  batch_output = gr.JSON(label="Results")
135
+ batch_btn = gr.Button("Analyze Batch", variant="primary")
136
  batch_btn.click(fn=analyze_batch, inputs=batch_input, outputs=batch_output, api_name="predict_batch")
137
 
138
  demo.launch()