Pushing to HuggingFace after Testing
Browse files
main.py
CHANGED
@@ -39,12 +39,22 @@ def analyze_text(text):
|
|
39 |
# Tokenize input text for NER
|
40 |
ner_inputs = ner_tokenizer(text, return_tensors="pt")
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Perform Named Entity Recognition
|
43 |
with torch.no_grad():
|
44 |
ner_outputs = ner_model(**ner_inputs)
|
45 |
|
46 |
-
ner_predictions = torch.argmax(ner_outputs, dim=-1)
|
47 |
ner_labels = ner_predictions.tolist()
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Tokenize input text for sentiment analysis
|
50 |
sentiment_inputs = sentiment_tokenizer(text, return_tensors="pt")
|
@@ -52,10 +62,10 @@ def analyze_text(text):
|
|
52 |
# Perform sentiment analysis
|
53 |
with torch.no_grad():
|
54 |
sentiment_outputs = sentiment_model(**sentiment_inputs)
|
55 |
-
sentiment_probabilities = torch.
|
56 |
sentiment_scores = sentiment_probabilities.tolist()
|
57 |
-
|
58 |
-
sentiment = sentiment_config["id2label"][
|
59 |
|
60 |
return ner_labels, sentiment
|
61 |
|
@@ -71,12 +81,11 @@ def main():
|
|
71 |
|
72 |
# Display Named Entities
|
73 |
st.subheader("Named Entities")
|
74 |
-
|
75 |
-
st.write(f"- {label}")
|
76 |
|
77 |
# Display Sentiment Analysis
|
78 |
st.subheader("Sentiment Analysis")
|
79 |
st.write(f"Sentiment: {sentiment_scores}")
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
main()
|
|
|
39 |
# Tokenize input text for NER
|
40 |
ner_inputs = ner_tokenizer(text, return_tensors="pt")
|
41 |
|
42 |
+
input_ids = ner_inputs['input_ids']
|
43 |
+
|
44 |
+
# Converting token IDs back to tokens
|
45 |
+
tokens = [ner_tokenizer.convert_ids_to_tokens(id) for id in input_ids.squeeze().tolist()]
|
46 |
+
|
47 |
+
|
48 |
# Perform Named Entity Recognition
|
49 |
with torch.no_grad():
|
50 |
ner_outputs = ner_model(**ner_inputs)
|
51 |
|
52 |
+
ner_predictions = torch.argmax(ner_outputs, dim=-1)[0]
|
53 |
ner_labels = ner_predictions.tolist()
|
54 |
+
ner_labels = [ner_config["id2labels"][str(label)] for label in ner_labels]
|
55 |
+
|
56 |
+
#matching the tokens with the labels
|
57 |
+
ner_labels = [f"{token}: {label}" for token, label in zip(tokens, ner_labels)]
|
58 |
|
59 |
# Tokenize input text for sentiment analysis
|
60 |
sentiment_inputs = sentiment_tokenizer(text, return_tensors="pt")
|
|
|
62 |
# Perform sentiment analysis
|
63 |
with torch.no_grad():
|
64 |
sentiment_outputs = sentiment_model(**sentiment_inputs)
|
65 |
+
sentiment_probabilities = torch.argmax(sentiment_outputs, dim=1)
|
66 |
sentiment_scores = sentiment_probabilities.tolist()
|
67 |
+
sentiment_id = sentiment_scores[0]
|
68 |
+
sentiment = sentiment_config["id2label"][str(sentiment_id)]
|
69 |
|
70 |
return ner_labels, sentiment
|
71 |
|
|
|
81 |
|
82 |
# Display Named Entities
|
83 |
st.subheader("Named Entities")
|
84 |
+
st.write(ner_labels)
|
|
|
85 |
|
86 |
# Display Sentiment Analysis
|
87 |
st.subheader("Sentiment Analysis")
|
88 |
st.write(f"Sentiment: {sentiment_scores}")
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
+
main()
|