straightline777 commited on
Commit
c9e47ec
·
verified ·
1 Parent(s): 10277d6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -10
app.py CHANGED
@@ -1,32 +1,94 @@
1
-
2
  import gradio as gr
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
 
4
 
5
  # Load your model and tokenizer from local paths
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  model_path = '.'
7
  tokenizer_path = '.'
8
 
9
  model_sentiment = AutoModelForSequenceClassification.from_pretrained(model_path)
10
  tokenizer_sentiment = AutoTokenizer.from_pretrained(tokenizer_path)
11
 
12
- # Initialize the pipeline
13
- clf = pipeline("text-classification", model=model_sentiment, tokenizer=tokenizer_sentiment)
 
 
 
 
 
14
 
15
  # Define the sentiment analysis function
16
  def analyze_sentiment(text):
17
- predictions = clf(text)
18
  label_mapping = {'LABEL_0': 'Negative', 'LABEL_1': 'Neutral', 'LABEL_2': 'Positive'}
19
  predicted_label = label_mapping[predictions[0]['label']]
20
  confidence = f"{predictions[0]['score'] * 100:.2f}%"
21
  return predicted_label, confidence
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Create the Gradio interface
24
  interface = gr.Interface(
25
- fn=analyze_sentiment,
26
- inputs=gr.Textbox(lines=2, placeholder="Enter a tweet here..."),
27
- outputs=[gr.Textbox(label="Predicted Label"), gr.Textbox(label="Confidence")],
28
- title="Sentiment Analysis for Product Reviews",
29
- description="This DistilBERT model was trained further to classify product reviews into negative, neutral, or positive sentiments."
 
 
 
30
  )
31
 
32
  if __name__ == "__main__":
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoConfig
4
 
5
  # Load your model and tokenizer from local paths
6
+ model_NER = AutoModelForTokenClassification.from_pretrained(
7
+ pretrained_model_name_or_path=None,
8
+ config="config_2.json",
9
+ state_dict=torch.load("model_2.safetensors", map_location=torch.device('cpu'))
10
+ )
11
+
12
+ tokenizer_NER = AutoTokenizer.from_pretrained(
13
+ pretrained_model_name_or_path=None,
14
+ tokenizer_file="tokenizer_2.json",
15
+ special_tokens_map_file="special_tokens_map_2.json",
16
+ tokenizer_config_file="tokenizer_config_2.json",
17
+ vocab_file="vocab_2.txt"
18
+ )
19
+
20
+ # Load sentiment analysis model and tokenizer
21
  model_path = '.'
22
  tokenizer_path = '.'
23
 
24
  model_sentiment = AutoModelForSequenceClassification.from_pretrained(model_path)
25
  tokenizer_sentiment = AutoTokenizer.from_pretrained(tokenizer_path)
26
 
27
+ # Initialize the pipelines
28
+ clf_sentiment = pipeline("text-classification", model=model_sentiment, tokenizer=tokenizer_sentiment)
29
+ clf_ner = pipeline("ner", model=model_NER, tokenizer=tokenizer_NER)
30
+
31
+ # Load the model configuration to get the labels for NER
32
+ config = AutoConfig.from_pretrained("config_2.json")
33
+ label_list = config.id2label
34
 
35
  # Define the sentiment analysis function
36
  def analyze_sentiment(text):
37
+ predictions = clf_sentiment(text)
38
  label_mapping = {'LABEL_0': 'Negative', 'LABEL_1': 'Neutral', 'LABEL_2': 'Positive'}
39
  predicted_label = label_mapping[predictions[0]['label']]
40
  confidence = f"{predictions[0]['score'] * 100:.2f}%"
41
  return predicted_label, confidence
42
 
43
+
44
+ def analyze_ner(input_text):
45
+ predictions = clf_ner(input_text)
46
+
47
+ merged_tokens = []
48
+ current_word = ""
49
+ current_tag = None
50
+ current_confidence = 0
51
+
52
+ for pred in predictions:
53
+ token = pred['word']
54
+ tag = label_list[int(pred['entity'].split('_')[-1])]
55
+ confidence = pred['score']
56
+
57
+ if token.startswith("##"):
58
+ current_word += token[2:]
59
+ current_confidence = min(current_confidence, confidence)
60
+ else:
61
+ if current_word:
62
+ merged_tokens.append((current_word, current_tag, current_confidence))
63
+ current_word = token
64
+ current_tag = tag
65
+ current_confidence = confidence
66
+
67
+ if current_word:
68
+ merged_tokens.append((current_word, current_tag, current_confidence))
69
+
70
+ results = []
71
+ for word, tag, confidence in merged_tokens:
72
+ results.append(f"Entity: {word}, Tag: {tag}, Confidence: {confidence:.2f}")
73
+
74
+ return "\n".join(results)
75
+
76
+
77
+ def analyze_text(text):
78
+ sentiment_result = analyze_sentiment(text)
79
+ ner_result = analyze_ner(text)
80
+ return sentiment_result, ner_result
81
+
82
  # Create the Gradio interface
83
  interface = gr.Interface(
84
+ fn=analyze_text,
85
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
86
+ outputs=[
87
+ gr.Textbox(label="Sentiment Analysis Result"),
88
+ gr.Textbox(label="Named Entity Recognition Result")
89
+ ],
90
+ title="Text Analysis",
91
+ description="This interface allows you to perform Sentiment Analysis and Named Entity Recognition (NER) on text."
92
  )
93
 
94
  if __name__ == "__main__":