Spaces:
Sleeping
Sleeping
Update app.py | Separate results
Browse files
app.py
CHANGED
|
@@ -8,49 +8,95 @@ model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
| 8 |
|
| 9 |
def predict_sentiment(text):
|
| 10 |
if not text.strip():
|
| 11 |
-
return "Please enter text."
|
|
|
|
| 12 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 13 |
outputs = model(**inputs)
|
| 14 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 15 |
predicted_class = torch.argmax(predictions, dim=1).item()
|
|
|
|
| 16 |
sentiment_map = {0: "NEGATIVE", 1: "NEUTRAL", 2: "POSITIVE"}
|
| 17 |
confidence = predictions[0][predicted_class].item()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
def predict_sentiment(text):
|
| 10 |
if not text.strip():
|
| 11 |
+
return "Please enter text.", "—", "—"
|
| 12 |
+
|
| 13 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 14 |
outputs = model(**inputs)
|
| 15 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 16 |
predicted_class = torch.argmax(predictions, dim=1).item()
|
| 17 |
+
|
| 18 |
sentiment_map = {0: "NEGATIVE", 1: "NEUTRAL", 2: "POSITIVE"}
|
| 19 |
confidence = predictions[0][predicted_class].item()
|
| 20 |
+
|
| 21 |
+
# Format each output separately
|
| 22 |
+
sentiment_output = f"{sentiment_map[predicted_class]}"
|
| 23 |
+
confidence_output = f"{confidence:.3f}"
|
| 24 |
+
probabilities_output = (
|
| 25 |
+
f"Negative: {predictions[0][0]:.3f}\n"
|
| 26 |
+
f"Neutral: {predictions[0][1]:.3f}\n"
|
| 27 |
+
f"Positive: {predictions[0][2]:.3f}"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return sentiment_output, confidence_output, probabilities_output
|
| 31 |
+
|
| 32 |
+
# Define Gradio Interface with 3 separate outputs
|
| 33 |
+
with gr.Blocks(theme="soft") as demo:
|
| 34 |
+
gr.Markdown("# 🔍 LexCAT: Taglish Sentiment Analysis")
|
| 35 |
+
gr.Markdown("""
|
| 36 |
+
LexCAT is a lexicon-enhanced transformer model for sentiment analysis of Tagalog–English code-switched text (Taglish). \n\n
|
| 37 |
+
|
| 38 |
+
• Developed by Glenn Marcus D. Cinco for his BS/MS thesis at Mapúa University. \n
|
| 39 |
+
• Trained on the FiReCS dataset. \n
|
| 40 |
+
• Enhanced with LexiLiksik to detect intra-sentential shifts (e.g., “Maganda pero expensive” → Negative).
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=1):
|
| 45 |
+
input_box = gr.Textbox(
|
| 46 |
+
placeholder="Type a Taglish sentence, e.g., 'Maganda pero expensive tlga'",
|
| 47 |
+
label="Input Tagalog–English (Taglish) Text",
|
| 48 |
+
lines=10,
|
| 49 |
+
max_lines=20
|
| 50 |
+
)
|
| 51 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 52 |
+
clear_btn = gr.Button("Clear")
|
| 53 |
+
|
| 54 |
+
with gr.Column(scale=1):
|
| 55 |
+
sentiment_box = gr.Textbox(
|
| 56 |
+
label="Predicted Sentiment",
|
| 57 |
+
lines=3,
|
| 58 |
+
max_lines=5,
|
| 59 |
+
interactive=False
|
| 60 |
+
)
|
| 61 |
+
confidence_box = gr.Textbox(
|
| 62 |
+
label="Confidence",
|
| 63 |
+
lines=3,
|
| 64 |
+
max_lines=5,
|
| 65 |
+
interactive=False
|
| 66 |
+
)
|
| 67 |
+
probabilities_box = gr.Textbox(
|
| 68 |
+
label="Raw Probabilities",
|
| 69 |
+
lines=6,
|
| 70 |
+
max_lines=10,
|
| 71 |
+
interactive=False
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Set up event listeners
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
fn=predict_sentiment,
|
| 77 |
+
inputs=input_box,
|
| 78 |
+
outputs=[sentiment_box, confidence_box, probabilities_box]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
clear_btn.click(
|
| 82 |
+
fn=lambda: ("", "", ""),
|
| 83 |
+
inputs=None,
|
| 84 |
+
outputs=[input_box, sentiment_box, confidence_box, probabilities_box]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Add examples below
|
| 88 |
+
gr.Examples(
|
| 89 |
+
examples=[
|
| 90 |
+
["sobrang lambot ng burger pero expensive tlga"],
|
| 91 |
+
["Ang ganda ng service, one star!"],
|
| 92 |
+
["Super duper late delivery umabot ng 2 weeks metro manila area lang naman"],
|
| 93 |
+
["Salamat sa nyo nagana nmn po sya kaya super thank you ako"],
|
| 94 |
+
["Ganda legit, kumpleto... problema lang nainit ng sobra..."]
|
| 95 |
+
],
|
| 96 |
+
inputs=input_box,
|
| 97 |
+
outputs=[sentiment_box, confidence_box, probabilities_box],
|
| 98 |
+
label="Example Sentences"
|
| 99 |
+
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo.launch()
|