shrish191 commited on
Commit
c93d183
·
verified ·
1 Parent(s): 504a9b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  from transformers import TFBertForSequenceClassification, BertTokenizer
3
  import tensorflow as tf
4
 
@@ -20,5 +20,38 @@ demo = gr.Interface(fn=classify_sentiment,
20
  description="Multilingual BERT-based Sentiment Analysis")
21
 
22
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
 
 
1
+ '''import gradio as gr
2
  from transformers import TFBertForSequenceClassification, BertTokenizer
3
  import tensorflow as tf
4
 
 
20
  description="Multilingual BERT-based Sentiment Analysis")
21
 
22
  demo.launch()
23
+ '''
24
+ import gradio as gr
25
+ from transformers import TFBertForSequenceClassification, BertTokenizer
26
+ import tensorflow as tf
27
+
28
+ # Load model and tokenizer from Hugging Face
29
+ model = TFBertForSequenceClassification.from_pretrained("shrish191/sentiment-bert")
30
+ tokenizer = BertTokenizer.from_pretrained("shrish191/sentiment-bert")
31
+
32
+ # Manually define the correct mapping
33
+ LABELS = {
34
+ 0: "Negative",
35
+ 1: "Neutral",
36
+ 2: "Positive"
37
+ }
38
+
39
+ def classify_sentiment(text):
40
+ inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True)
41
+ outputs = model(inputs)
42
+ probs = tf.nn.softmax(outputs.logits, axis=1)
43
+ pred_label = tf.argmax(probs, axis=1).numpy()[0]
44
+ confidence = float(tf.reduce_max(probs).numpy())
45
+ return f"Prediction: {LABELS[pred_label]} (Confidence: {confidence:.2f})"
46
+
47
+ demo = gr.Interface(
48
+ fn=classify_sentiment,
49
+ inputs=gr.Textbox(placeholder="Type your tweet here..."),
50
+ outputs="text",
51
+ title="Sentiment Analysis on Tweets",
52
+ description="Multilingual BERT model fine-tuned for sentiment classification. Labels: Positive, Neutral, Negative."
53
+ )
54
+
55
+ demo.launch()
56
 
57