GMCTech commited on
Commit
9399b88
·
verified ·
1 Parent(s): 3d8b7aa

Update app.py | Separate results

Browse files
Files changed (1) hide show
  1. app.py +83 -37
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
- result = f"Predicted Sentiment: \n{sentiment_map[predicted_class]}\n"
19
- result += f"Confidence: \n{confidence:.3f}\n"
20
- result += f"\nRaw Probabilities:\nNegative: {predictions[0][0]:.3f}\nNeutral: {predictions[0][1]:.3f}\nPositive: {predictions[0][2]:.3f}"
21
- return result
22
-
23
- demo = gr.Interface(
24
- fn=predict_sentiment,
25
- inputs=gr.Textbox(
26
- placeholder="Type a Taglish sentence, e.g., 'Maganda pero expensive tlga'",
27
- label="Input Tagalog–English (Taglish) Text",
28
- lines=10,
29
- max_lines=20
30
- ),
31
- outputs=gr.Textbox(
32
- label="Sentiment Prediction",
33
- lines=15,
34
- max_lines=30
35
- ),
36
- title="🔍 LexCAT: Taglish Sentiment Analysis",
37
- description="""
38
- LexCAT is a lexicon-enhanced transformer model for sentiment analysis of Tagalog–English code-switched text (Taglish).
39
-
40
- • Developed by Glenn Marcus D. Cinco for his BS/MS thesis at Mapúa University.
41
- Trained on the FiReCS dataset.
42
- • Enhanced with LexiLiksik to detect intra-sentential shifts (e.g., “Maganda pero expensive” → ❌ Negative).
43
- """,
44
- examples=[
45
- ["sobrang lambot ng burger pero expensive tlga"],
46
- ["Ang ganda ng service, one star!"],
47
- ["Super duper late delivery umabot ng 2 weeks metro manila area lang naman"],
48
- ["Salamat sa nyo nagana nmn po sya kaya super thank you ako"],
49
- ["Ganda legit, kumpleto... problema lang nainit ng sobra..."]
50
- ],
51
- theme="soft",
52
- allow_flagging="never"
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()