shrish191 commited on
Commit
13916fc
·
verified ·
1 Parent(s): 3d2120b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -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,3 +20,28 @@ demo = gr.Interface(fn=classify_sentiment,
20
  description="Multilingual BERT-based Sentiment Analysis")
21
 
22
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 your HF model repo
29
+ model = TFBertForSequenceClassification.from_pretrained("shrish191/sentiment-bert")
30
+ tokenizer = BertTokenizer.from_pretrained("shrish191/sentiment-bert")
31
+
32
+ def classify_sentiment(text):
33
+ text = text.lower().strip() # Normalize input
34
+ inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True)
35
+ predictions = model(inputs).logits
36
+ label = tf.argmax(predictions, axis=1).numpy()[0]
37
+ labels = model.config.id2label # Use mapping from config.json
38
+ print(f"Text: {text} | Prediction: {label} | Logits: {predictions.numpy()}") # Debug
39
+ return labels[str(label)] # Convert to string key
40
+
41
+ demo = gr.Interface(fn=classify_sentiment,
42
+ inputs=gr.Textbox(placeholder="Enter a tweet..."),
43
+ outputs="text",
44
+ title="Tweet Sentiment Classifier",
45
+ description="Multilingual BERT-based Sentiment Analysis")
46
+
47
+ demo.launch()