Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
sentimentAnalysisTokenizer = AutoTokenizer.from_pretrained(sentimentAnalysisModelName)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
for
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
sentimentAnalysisBatch = sentimentAnalysisTokenizer(sentimentAnalysisXTrainingVariable, padding = True, truncation = True, max_length = 512, return_tensors = "pt")
|
| 27 |
-
print(sentimentAnalysisBatch)
|
| 28 |
-
|
| 29 |
-
with torch.no_grad():
|
| 30 |
-
sentimentAnalysisOutputs = sentimentAnalysisModel(**sentimentAnalysisBatch, labels = torch.tensor([1, 0]))
|
| 31 |
-
print(sentimentAnalysisOutputs)
|
| 32 |
-
sentimentAnalysisPredictions = F.softmax(sentimentAnalysisOutputs.logits, dim = 1)
|
| 33 |
-
print(sentimentAnalysisPredictions)
|
| 34 |
-
sentimentAnalysisLabels = torch.argmax(sentimentAnalysisPredictions, dim = 1)
|
| 35 |
-
print(sentimentAnalysisLabels)
|
| 36 |
-
sentimentAnalysisLabels = [sentimentAnalysisModel.config.id2label[sentimentAnalysisLabelID] for sentimentAnalysisLabelID in sentimentAnalysisLabels.tolist()]
|
| 37 |
-
print(sentimentAnalysisLabels)
|
| 38 |
-
|
| 39 |
-
sentimentAnalysis_saveDirectory = "modelSaved"
|
| 40 |
-
sentimentAnalysisTokenizer.save_pretrained(sentimentAnalysis_saveDirectory)
|
| 41 |
-
sentimentAnalysisModel.save_pretrained(sentimentAnalysis_saveDirectory)
|
| 42 |
-
|
| 43 |
-
sentimentAnalysisTokenizer = AutoTokenizer.from_pretrained(sentimentAnalysis_saveDirectory)
|
| 44 |
-
sentimentAnalysisModel = AutoModelForSequenceClassification.from_pretrained(sentimentAnalysis_saveDirectory)
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Define a list of pretrained models
|
| 5 |
+
sentimentAnalysisModels = {
|
| 6 |
+
"Roberta": "deepset/roberta-base-squad2",
|
| 7 |
+
"RESPIN": "RESPIN/Telugu_LanguageModels_for_ASR",
|
| 8 |
+
"GPT2-n": "DunnBC22/gpt2-Causal_Language_Model-AG_News",
|
| 9 |
+
"Roberta-Base (ADDITIONAL MODEL!)": "achimoraites/TextClassification-roberta-base_ag_news",
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# Display a selection box for the user to choose a model
|
| 13 |
+
sentimentAnalysisLanguageSelections = st.selectbox("Please select one of these finetuned models from the dropdown", list(sentimentAnalysisModels.keys()))
|
| 14 |
+
|
| 15 |
+
# roBERTa specific label map
|
| 16 |
+
languageMapRoberta = {"LABEL_0": "NEGATIVE",
|
| 17 |
+
"LABEL_1": "POSITIVE", "LABEL_2": "NEUTRAL"}
|
| 18 |
+
|
| 19 |
+
# Load the selected model and tokenizer
|
| 20 |
+
sentimentAnalysisModelName = sentimentAnalysisModels[sentimentAnalysisSelectedModels]
|
| 21 |
sentimentAnalysisTokenizer = AutoTokenizer.from_pretrained(sentimentAnalysisModelName)
|
| 22 |
+
sentiment_pipeline = pipeline(
|
| 23 |
+
"sentiment-analysis", model=sentimentAnalysisModelName, tokenizer=sentimentAnalysisTokenizer)
|
| 24 |
+
|
| 25 |
+
# Get user input and perform sentiment analysis
|
| 26 |
+
sentimentAnalysisTextInput = st.text_input("Please enter sample text for finetuned language model below:",
|
| 27 |
+
"I am grateful for Data Science Program at NJIT and to have amazing faculty to learn under!")
|
| 28 |
+
sentimentAnalysisSubmitButton = st.button("Press the submit button for final grading")
|
| 29 |
+
|
| 30 |
+
if sentimentAnalysisSubmitButton and sentimentAnalysisTextInput:
|
| 31 |
+
sentimentAnalysisFinalOutput = sentiment_pipeline(sentimentAnalysisTextInput)
|
| 32 |
+
if sentimentAnalysisSelectedModels == "roBERTa":
|
| 33 |
+
st.write("Roberta Sentiment Analysis Resultant Value:", roberta_label_map[result[0]["Roberta Label"]])
|
| 34 |
+
st.write("Roberta Sentiment Analysis Resultant Score:", sentimentAnalysisFinalOutput[0]["Probability Assigned with Sentiment Analysis Label"])
|
| 35 |
+
else:
|
| 36 |
+
st.write("Sentiment Analysis Resultant Value:", sentimentAnalysisFinalOutput[0]["Sentiment Analysis Label"])
|
| 37 |
+
st.write("Sentiment Analysis Resultant Score:", sentimentAnalysisFinalOutput[0]["Probability Assigned with Sentiment Analysis Label"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|