skylord commited on
Commit
82ab74d
1 Parent(s): 30113a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -1,11 +1,30 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from gradio.components import Textbox
4
 
5
- # Load the sentiment analysis pipeline with DistilBERT
6
- distilbert_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
7
- label_map = {"POSITIVE":"OTHER", "NEGATIVE":"SENSITIVE"}
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def predict_sentiment(text):
11
  """
@@ -13,10 +32,8 @@ def predict_sentiment(text):
13
  :param text: str, input text to analyze.
14
  :return: str, predicted sentiment and confidence score.
15
  """
16
- result = distilbert_pipeline(text)[0]
17
- label = label_map[result['label']]
18
- score = result['score']
19
- return f"TAG: {label}, Confidence: {score:.2f}"
20
 
21
  input1 = Textbox(lines=2, placeholder="Type your text here...")
22
 
@@ -24,8 +41,8 @@ input1 = Textbox(lines=2, placeholder="Type your text here...")
24
  iface = gr.Interface(fn=predict_sentiment,
25
  inputs=input1,
26
  outputs="text",
27
- title="Talk2Loop Sensitive statement tags",
28
- description="This model predicts the sensitivity of the input text. Enter a sentence to see if it's sensitive or not.")
29
 
30
  # Launch the interface
31
  iface.launch()
 
1
  import gradio as gr
 
2
  from gradio.components import Textbox
3
 
4
+ import torch
5
+ from transformers import pipeline
6
+ from transformers import AutoTokenizer
7
+ from transformers import AutoModelForSequenceClassification
8
+
9
+ # Load the DistilBERT tokenizer
10
+ tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
11
+
12
+ #Load the model
13
+ model = AutoModelForSequenceClassification.from_pretrained("skylord/pharma_classification")
14
+
15
 
16
+ def is_pharma(sentence, tokenize=tokenizer, model=model):
17
+ # tokenize the input
18
+ inputs = tokenizer(sentence, return_tensors='pt')
19
+ # ensure model and inputs are on the same device (GPU)
20
+ inputs = {name: tensor.cuda() for name, tensor in inputs.items()}
21
+ model = model.cuda()
22
+ # get prediction - 2 classes "probabilities" (not really true because they still need to be normalized)
23
+ with torch.no_grad():
24
+ predictions = model(**inputs)[0].cpu().numpy()
25
+ # get the top prediction class and convert it to its associated label
26
+ top_prediction = predictions.argmax().item()
27
+ return ds['train'].features['labels'].int2str(top_prediction)
28
 
29
  def predict_sentiment(text):
30
  """
 
32
  :param text: str, input text to analyze.
33
  :return: str, predicted sentiment and confidence score.
34
  """
35
+ result = is_pharma(text)
36
+ return f"TAG: {result}" #, Confidence: {score:.2f}
 
 
37
 
38
  input1 = Textbox(lines=2, placeholder="Type your text here...")
39
 
 
41
  iface = gr.Interface(fn=predict_sentiment,
42
  inputs=input1,
43
  outputs="text",
44
+ title="Identify if the news item is relevant to the pharma industry",
45
+ description="This model predicts the tag of the input text. Enter a sentence to see if it's pharma or not. Response is a Yes or a No")
46
 
47
  # Launch the interface
48
  iface.launch()