syedkhalid076 commited on
Commit
78aa4a9
1 Parent(s): 02f979f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import torch
4
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
5
+
6
+ # Load model
7
+ model_name = "syedkhalid076/RoBERTa-Sentimental-Analysis-Model"
8
+ 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().numpy()
17
+ sentiment = "positive" if logits[0][1] > logits[0][0] else "negative"
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
+ df['Sentiment'] = df['Text'].apply(predict_sentiment)
24
+ return df
25
+
26
+ # Streamlit app
27
+ def main():
28
+ st.title("Sentiment Analysis App")
29
+ st.write("Write a sentence or upload a CSV file to analyze sentiment.")
30
+
31
+ option = st.radio("Choose input type:", ("Write a sentence", "Upload a CSV file"))
32
+
33
+ if option == "Write a sentence":
34
+ sentence = st.text_input("Enter a sentence:")
35
+ if st.button("Analyze"):
36
+ sentiment = predict_sentiment(sentence)
37
+ st.write("Sentiment:", sentiment)
38
+
39
+ elif option == "Upload a CSV file":
40
+ file = st.file_uploader("Upload CSV file", type=['csv'])
41
+ if file is not None:
42
+ df = process_csv(file)
43
+ st.write(df)
44
+
45
+ if __name__ == '__main__':
46
+ main()