GMCTech commited on
Commit
fd7c103
·
verified ·
1 Parent(s): 89b5007

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Load your model and tokenizer
6
+ model_name = "your-hf-username/LexCAT-LexiLiksik-Final" # ← REPLACE WITH YOUR MODEL REPO
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ # Define prediction function
11
+ def predict_sentiment(text):
12
+ if not text.strip():
13
+ return "Please enter text."
14
+
15
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
16
+ outputs = model(**inputs)
17
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
+ predicted_class = torch.argmax(predictions, dim=1).item()
19
+
20
+ sentiment_map = {0: "❌ Negative", 1: "➖ Neutral", 2: "✅ Positive"}
21
+ confidence = predictions[0][predicted_class].item()
22
+
23
+ result = f"**Predicted Sentiment**: {sentiment_map[predicted_class]}\n"
24
+ result += f"**Confidence**: {confidence:.3f}\n"
25
+ result += f"\n**Raw Probabilities**:\nNegative: {predictions[0][0]:.3f}\nNeutral: {predictions[0][1]:.3f}\nPositive: {predictions[0][2]:.3f}"
26
+
27
+ return result
28
+
29
+ # Define Gradio interface
30
+ demo = gr.Interface(
31
+ fn=predict_sentiment,
32
+ inputs=gr.Textbox(placeholder="Type a Taglish sentence, e.g., 'Maganda pero expensive tlga'", label="Input Tagalog–English (Taglish) Text"),
33
+ outputs=gr.Textbox(label="Sentiment Prediction"),
34
+ title="🔍 LexCAT: Taglish Sentiment Analysis",
35
+ description="""
36
+ **LexCAT** is a lexicon-enhanced transformer model for sentiment analysis of **Tagalog–English code-switched text (Taglish)**.
37
+ Developed by Glenn Marcus D. Cinco for his BS/MS thesis at Mapúa University.
38
+ Trained on FiReCS dataset, enhanced with **LexiLiksik** lexicon for intra-sentential shift detection (e.g., “Maganda pero expensive” → ❌ Negative).
39
+ """,
40
+ examples=[
41
+ ["sobrang lambot ng burger pero expensive tlga"],
42
+ ["Ang ganda ng service, one star!"],
43
+ ["Super duper late delivery umabot ng 2 weeks metro manila area lang naman"],
44
+ ["Salamat sa nyo nagana nmn po sya kaya super thank you ako"],
45
+ ["Ganda legit, kumpleto... problema lang nainit ng sobra..."]
46
+ ],
47
+ theme="soft",
48
+ allow_flagging="never"
49
+ )
50
+
51
+ # Launch app
52
+ if __name__ == "__main__":
53
+ demo.launch()