straightline777 commited on
Commit
c2634bf
·
verified ·
1 Parent(s): 4173b83

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import torch
3
- import nltk
4
- from nltk.corpus import stopwords
5
- from nltk.stem import WordNetLemmatizer
6
- from nltk.tokenize import word_tokenize
7
 
8
  from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoConfig
9
 
@@ -31,13 +31,36 @@ def preprocess(text):
31
  processed = [lemmatizer.lemmatize(word.lower()) for word in tokens if word.isalpha() and word not in stop_words]
32
  return ' '.join(processed)
33
 
34
- def analyze_sentiment(text):
35
- text = preprocess(text)
36
- predictions = clf_sentiment(text)
37
- label_mapping = {'LABEL_0': 'Negative', 'LABEL_1': 'Neutral', 'LABEL_2': 'Positive'}
38
- predicted_label = label_mapping[predictions[0]['label']]
39
- confidence = f"{predictions[0]['score'] * 100:.2f}%"
40
- return f"Predicted Label: {predicted_label}\nConfidence: {confidence}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  LABEL = ['O',
43
  'B-storage',
 
1
  import gradio as gr
2
  import torch
3
+ # import nltk
4
+ # from nltk.corpus import stopwords
5
+ # from nltk.stem import WordNetLemmatizer
6
+ # from nltk.tokenize import word_tokenize
7
 
8
  from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoConfig
9
 
 
31
  processed = [lemmatizer.lemmatize(word.lower()) for word in tokens if word.isalpha() and word not in stop_words]
32
  return ' '.join(processed)
33
 
34
+ def analyze_ner(text):
35
+ predictions = clf_ner(text)
36
+ print(predictions) # Debugging statement to see the structure of the output
37
+
38
+ keys = [f'LABEL_{i}' for i in range(len(LABEL))]
39
+ values = LABEL
40
+
41
+ label_mapping = {k: v for k, v in zip(keys, values)}
42
+
43
+ results = []
44
+ current_entity = ""
45
+ current_tag = None
46
+ for pred in predictions:
47
+ word = pred['word']
48
+ tag = label_mapping[pred['entity']]
49
+ confidence = pred['score']
50
+
51
+ if word.startswith("##"):
52
+ current_entity += word[2:]
53
+ else:
54
+ if current_entity:
55
+ results.append(f"Entity: {current_entity}\nPredicted Tag: {current_tag}\nConfidence Probability: {confidence:.2f}\n")
56
+ current_entity = word
57
+ current_tag = tag
58
+
59
+ if current_entity:
60
+ results.append(f"Entity: {current_entity}\nPredicted Tag: {current_tag}\nConfidence Probability: {confidence:.2f}\n")
61
+
62
+ return "\n".join(results)
63
+
64
 
65
  LABEL = ['O',
66
  'B-storage',