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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -63
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,67 +20,5 @@ demo = gr.Interface(fn=classify_sentiment,
20
  description="Multilingual BERT-based Sentiment Analysis")
21
 
22
  demo.launch()
23
- '''
24
- '''
25
- import gradio as gr
26
- from transformers import TFBertForSequenceClassification, BertTokenizer
27
- import tensorflow as tf
28
-
29
- # Load model and tokenizer from your HF model repo
30
- model = TFBertForSequenceClassification.from_pretrained("shrish191/sentiment-bert")
31
- tokenizer = BertTokenizer.from_pretrained("shrish191/sentiment-bert")
32
-
33
- def classify_sentiment(text):
34
- text = text.lower().strip() # Normalize input
35
- inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True)
36
- predictions = model(inputs).logits
37
- label = tf.argmax(predictions, axis=1).numpy()[0]
38
- labels = model.config.id2label # Use mapping from config.json
39
- print(f"Text: {text} | Prediction: {label} | Logits: {predictions.numpy()}") # Debug
40
- return labels[str(label)] # Convert to string key
41
-
42
- demo = gr.Interface(fn=classify_sentiment,
43
- inputs=gr.Textbox(placeholder="Enter a tweet..."),
44
- outputs="text",
45
- title="Tweet Sentiment Classifier",
46
- description="Multilingual BERT-based Sentiment Analysis")
47
-
48
- demo.launch()
49
- '''
50
- import gradio as gr
51
- from transformers import TFBertForSequenceClassification, BertTokenizer
52
- import tensorflow as tf
53
 
54
- # Load model and tokenizer from Hugging Face Hub
55
- model = TFBertForSequenceClassification.from_pretrained("shrish191/sentiment-bert")
56
- tokenizer = BertTokenizer.from_pretrained("shrish191/sentiment-bert")
57
-
58
- def classify_sentiment(text):
59
- text = text.lower().strip()
60
- inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True)
61
- outputs = model(inputs, training=False)
62
- logits = outputs.logits
63
- label_id = int(tf.argmax(logits, axis=1).numpy()[0])
64
-
65
- # Handle label mapping correctly
66
- raw_labels = model.config.id2label
67
- if isinstance(list(raw_labels.keys())[0], str):
68
- label = raw_labels.get(str(label_id), "Unknown")
69
- else:
70
- label = raw_labels.get(label_id, "Unknown")
71
-
72
- print(f"Text: {text} | Label ID: {label_id} | Label: {label} | Logits: {logits.numpy()}")
73
- return label
74
-
75
- # Define the Gradio interface
76
- demo = gr.Interface(
77
- fn=classify_sentiment,
78
- inputs=gr.Textbox(placeholder="Enter a tweet..."),
79
- outputs="text",
80
- title="Tweet Sentiment Classifier",
81
- description="Multilingual BERT-based Sentiment Analysis"
82
- )
83
-
84
- # Launch the app
85
- demo.launch()
86
 
 
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