Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
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
|
| 35 |
-
|
| 36 |
-
predictions
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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',
|