RohitCSharp commited on
Commit
97a62f2
·
verified ·
1 Parent(s): 34932a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -30
app.py CHANGED
@@ -1,33 +1,7 @@
1
  import gradio as gr
2
- from transformers import BertTokenizer, BertForSequenceClassification
3
  from transformers import pipeline
4
- import torch
5
-
6
- # Load model and tokenizer
7
- model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
8
- model = BertForSequenceClassification.from_pretrained(model_name)
9
- tokenizer = BertTokenizer.from_pretrained(model_name)
10
-
11
- # Define pipeline
12
- classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
13
-
14
- # Prediction function
15
- def predict_sentiment(text):
16
- if not text.strip():
17
- return "Please enter some text."
18
  result = classifier(text)[0]
19
- label = result['label']
20
- score = round(result['score'], 4)
21
- return f"Sentiment: {label} (Confidence: {score})"
22
-
23
- # Gradio UI
24
- interface = gr.Interface(
25
- fn=predict_sentiment,
26
- inputs=gr.Textbox(lines=3, placeholder="Enter movie review..."),
27
- outputs="text",
28
- title="IMDB Movie Review Sentiment Analysis with BERT",
29
- description="This demo uses BERT to predict sentiment on IMDB-like reviews. Model: nlptown/bert-base-multilingual-uncased-sentiment"
30
- )
31
-
32
- if __name__ == "__main__":
33
- interface.launch()
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
+ classifier = pipeline("sentiment-analysis", model="bert-base-uncased")
4
+ def classify_sentiment(text):
 
 
 
 
 
 
 
 
 
 
 
 
5
  result = classifier(text)[0]
6
+ return f"{result['label']} ({round(result['score'] * 100, 2)}%)"
7
+ gr.Interface(fn=classify_sentiment, inputs="text", outputs="text").launch()