mrgmd01 commited on
Commit
b445cfc
·
verified ·
1 Parent(s): 61cba3f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load models
5
+ english_model = pipeline(
6
+ "sentiment-analysis",
7
+ model="cardiffnlp/twitter-roberta-base-sentiment-latest"
8
+ )
9
+
10
+ urdu_model = pipeline(
11
+ "sentiment-analysis",
12
+ model="mrgmd01/sentiment_model_FineTune_cardiffnlp"
13
+ )
14
+
15
+ # Simple language detection (very lightweight)
16
+ def detect_language(text):
17
+ urdu_chars = set("ابتثجحخدذرزسشصضطظعغفقکلمنوہیءآؤئۀ")
18
+ for ch in text:
19
+ if ch in urdu_chars:
20
+ return "urdu"
21
+ return "english"
22
+
23
+ # Normalize labels into Positive, Negative, Neutral
24
+ def normalize_label(label):
25
+ label = label.lower()
26
+ if "positive" in label:
27
+ return "positive"
28
+ elif "negative" in label:
29
+ return "negative"
30
+ else:
31
+ return "neutral"
32
+
33
+ # Main prediction function
34
+ def predict_sentiment(text):
35
+ if not text.strip():
36
+ return "Please enter a sentence."
37
+
38
+ lang = detect_language(text)
39
+
40
+ if lang == "english":
41
+ result = english_model(text)[0]
42
+ else:
43
+ result = urdu_model(text)[0]
44
+
45
+ sentiment = normalize_label(result["label"])
46
+ score = round(result["score"], 3)
47
+
48
+ return f"Sentiment: {sentiment} (confidence: {score})"
49
+
50
+ # Gradio interface
51
+ iface = gr.Interface(
52
+ fn=predict_sentiment,
53
+ inputs=gr.Textbox(lines=3, placeholder="Enter a sentence in English, Urdu, or Roman Urdu..."),
54
+ outputs="text",
55
+ title="Multilingual Sentiment Analysis",
56
+ description="This app predicts sentiment (Positive, Negative, Neutral) for English using CardiffNLP and for Urdu/Roman Urdu using a fine-tuned model."
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ iface.launch()