maximuspowers commited on
Commit
7a0674a
·
verified ·
1 Parent(s): b75c854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -2,11 +2,11 @@ import torch
2
  from transformers import BertTokenizerFast, BertForTokenClassification
3
  import gradio as gr
4
 
5
- # Load tokenizer and model
6
  tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
7
  model = BertForTokenClassification.from_pretrained('maximuspowers/bias-detection-ner')
8
- model.eval() # Set the model to evaluation mode
9
- model.to('cuda' if torch.cuda.is_available() else 'cpu') # Move model to appropriate device
10
 
11
  # Define label mappings
12
  id2label = {
@@ -20,12 +20,10 @@ id2label = {
20
  }
21
 
22
  def predict_ner_tags(sentence):
23
- # Tokenize the sentence
24
  inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True, max_length=128)
25
  input_ids = inputs['input_ids'].to(model.device)
26
  attention_mask = inputs['attention_mask'].to(model.device)
27
 
28
- # Predict using the model
29
  with torch.no_grad():
30
  outputs = model(input_ids=input_ids, attention_mask=attention_mask)
31
  logits = outputs.logits
@@ -43,7 +41,6 @@ def predict_ner_tags(sentence):
43
  return result
44
 
45
  def format_output(result):
46
- # Create HTML content with formatted output
47
  formatted_output = "<div style='font-family: Arial;'>"
48
  for token, labels in result:
49
  styles = []
@@ -62,12 +59,11 @@ def format_output(result):
62
  iface = gr.Interface(
63
  fn=predict_ner_tags,
64
  inputs="text",
65
- outputs=gr.outputs.HTML(label="Output"),
66
  title="Named Entity Recognition with BERT",
67
  description="Enter a sentence to predict NER tags using a BERT model trained for multi-label classification. Different styles represent different entity types.",
68
  examples=["Tall men are so clumsy."],
69
- allow_flagging="never",
70
- theme="default"
71
  )
72
 
73
  if __name__ == "__main__":
 
2
  from transformers import BertTokenizerFast, BertForTokenClassification
3
  import gradio as gr
4
 
5
+ # Initialize tokenizer and model
6
  tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
7
  model = BertForTokenClassification.from_pretrained('maximuspowers/bias-detection-ner')
8
+ model.eval()
9
+ model.to('cuda' if torch.cuda.is_available() else 'cpu')
10
 
11
  # Define label mappings
12
  id2label = {
 
20
  }
21
 
22
  def predict_ner_tags(sentence):
 
23
  inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True, max_length=128)
24
  input_ids = inputs['input_ids'].to(model.device)
25
  attention_mask = inputs['attention_mask'].to(model.device)
26
 
 
27
  with torch.no_grad():
28
  outputs = model(input_ids=input_ids, attention_mask=attention_mask)
29
  logits = outputs.logits
 
41
  return result
42
 
43
  def format_output(result):
 
44
  formatted_output = "<div style='font-family: Arial;'>"
45
  for token, labels in result:
46
  styles = []
 
59
  iface = gr.Interface(
60
  fn=predict_ner_tags,
61
  inputs="text",
62
+ outputs="html", # Directly use "html" here
63
  title="Named Entity Recognition with BERT",
64
  description="Enter a sentence to predict NER tags using a BERT model trained for multi-label classification. Different styles represent different entity types.",
65
  examples=["Tall men are so clumsy."],
66
+ allow_flagging="never"
 
67
  )
68
 
69
  if __name__ == "__main__":