Update app.py
Browse files
app.py
CHANGED
|
@@ -9,39 +9,50 @@ tokenizer = RobertaTokenizer.from_pretrained(model_name)
|
|
| 9 |
model = RobertaForSequenceClassification.from_pretrained(model_name)
|
| 10 |
model.eval()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to predict sentiment for a single sentence
|
| 13 |
def predict_sentiment(sentence):
|
| 14 |
inputs = tokenizer(sentence, return_tensors="pt", max_length=512, truncation=True)
|
| 15 |
outputs = model(**inputs)
|
| 16 |
-
logits = outputs.logits.detach().cpu()
|
| 17 |
-
|
|
|
|
| 18 |
return sentiment
|
| 19 |
|
| 20 |
# Function to process CSV file and predict sentiment for each row
|
| 21 |
def process_csv(file):
|
| 22 |
df = pd.read_csv(file)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
return df
|
| 25 |
|
| 26 |
# Streamlit app
|
| 27 |
def main():
|
| 28 |
st.title("Sentiment Analysis App")
|
| 29 |
-
st.write("
|
| 30 |
-
st.write("NOTE: If uploading a CSV file,
|
| 31 |
|
| 32 |
option = st.radio("Choose input type:", ("Write a sentence", "Upload a CSV file"))
|
| 33 |
|
| 34 |
if option == "Write a sentence":
|
| 35 |
sentence = st.text_input("Enter a sentence:")
|
| 36 |
if st.button("Analyze"):
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
elif option == "Upload a CSV file":
|
| 41 |
file = st.file_uploader("Upload CSV file", type=['csv'])
|
| 42 |
if file is not None:
|
| 43 |
df = process_csv(file)
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
main()
|
|
|
|
| 9 |
model = RobertaForSequenceClassification.from_pretrained(model_name)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
# Define sentiment labels
|
| 13 |
+
sentiment_labels = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
| 14 |
+
|
| 15 |
# Function to predict sentiment for a single sentence
|
| 16 |
def predict_sentiment(sentence):
|
| 17 |
inputs = tokenizer(sentence, return_tensors="pt", max_length=512, truncation=True)
|
| 18 |
outputs = model(**inputs)
|
| 19 |
+
logits = outputs.logits.detach().cpu()
|
| 20 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 21 |
+
sentiment = sentiment_labels[predicted_class]
|
| 22 |
return sentiment
|
| 23 |
|
| 24 |
# Function to process CSV file and predict sentiment for each row
|
| 25 |
def process_csv(file):
|
| 26 |
df = pd.read_csv(file)
|
| 27 |
+
if 'Text' not in df.columns:
|
| 28 |
+
st.error("CSV file must have a 'Text' column with sentences for analysis.")
|
| 29 |
+
return None
|
| 30 |
+
df['sentiment'] = df['text'].apply(predict_sentiment)
|
| 31 |
return df
|
| 32 |
|
| 33 |
# Streamlit app
|
| 34 |
def main():
|
| 35 |
st.title("Sentiment Analysis App")
|
| 36 |
+
st.write("Analyze text sentiment as Negative, Neutral, or Positive.")
|
| 37 |
+
st.write("NOTE: If uploading a CSV file, ensure the column containing text is named 'text' (case-sensitive).")
|
| 38 |
|
| 39 |
option = st.radio("Choose input type:", ("Write a sentence", "Upload a CSV file"))
|
| 40 |
|
| 41 |
if option == "Write a sentence":
|
| 42 |
sentence = st.text_input("Enter a sentence:")
|
| 43 |
if st.button("Analyze"):
|
| 44 |
+
if sentence.strip():
|
| 45 |
+
sentiment = predict_sentiment(sentence)
|
| 46 |
+
st.write("Sentiment:", sentiment)
|
| 47 |
+
else:
|
| 48 |
+
st.warning("Please enter a valid sentence.")
|
| 49 |
|
| 50 |
elif option == "Upload a CSV file":
|
| 51 |
file = st.file_uploader("Upload CSV file", type=['csv'])
|
| 52 |
if file is not None:
|
| 53 |
df = process_csv(file)
|
| 54 |
+
if df is not None:
|
| 55 |
+
st.write(df)
|
| 56 |
|
| 57 |
if __name__ == '__main__':
|
| 58 |
main()
|