Spaces:
Sleeping
Sleeping
mvasani3690
commited on
Create mile2.py
Browse files
mile2.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Set up the Streamlit app
|
7 |
+
st.title("Sentiment Analysis App")
|
8 |
+
st.write('Welcome to my Sentiment Analysis app!')
|
9 |
+
|
10 |
+
#subtitle
|
11 |
+
st.markdown("Sentiment Analysis App using 'streamlit' hosted on hugging spaces ")
|
12 |
+
st.markdown("")
|
13 |
+
|
14 |
+
user_input = st.text_area("Enter your text", value="")
|
15 |
+
form = st.form(key='sentiment-form')
|
16 |
+
submit = form.form_submit_button('Submit')
|
17 |
+
|
18 |
+
classifier = pipeline(model="distilbert-base-uncased-finetuned-sst-2-english")
|
19 |
+
classifier("I've been waiting for HuggingFAcecourse my whole life.")
|
20 |
+
|
21 |
+
classifier = pipeline(model="distilbert-base-uncased-finetuned-sst-2-english")
|
22 |
+
result = classifier(user_input)[0]
|
23 |
+
label = result['label']
|
24 |
+
score = result['score']
|
25 |
+
|
26 |
+
if submit:
|
27 |
+
classifier = pipeline(model="distilbert-base-uncased-finetuned-sst-2-english")
|
28 |
+
result = classifier(user_input)[0]
|
29 |
+
label = result['label']
|
30 |
+
score = result['score']
|
31 |
+
if label == 'POSITIVE':
|
32 |
+
st.success(f'{label} sentiment (score: {score})')
|
33 |
+
else:
|
34 |
+
st.error(f'{label} sentiment (score: {score})')
|
35 |
+
|
36 |
+
# Load the sentiment analysis model and tokenizer
|
37 |
+
model_name = "textattack/bert-base-uncased-SST-2"
|
38 |
+
model2 = AutoModelForSequenceClassification.from_pretrained(model_name)
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
40 |
+
|
41 |
+
|
42 |
+
# Model selection
|
43 |
+
model_options = {
|
44 |
+
"BERT-base-uncased-SST-2": "textattack/bert-base-uncased-SST-2",
|
45 |
+
"BERT-base-cased-finetuned-mrpc": "bert-base-cased-finetuned-mrpc"
|
46 |
+
}
|
47 |
+
model_name = st.selectbox("Select a pretrained model", list(model_options.keys()))
|
48 |
+
model_path = model_options[model_name]
|
49 |
+
|
50 |
+
# Sentiment analysis
|
51 |
+
if st.button("Analyze"):
|
52 |
+
if user_input.strip() == "":
|
53 |
+
st.warning("Please enter some text.")
|
54 |
+
else:
|
55 |
+
# Tokenize input text
|
56 |
+
inputs = tokenizer.encode_plus(user_input, return_tensors="pt", padding=True, truncation=True)
|
57 |
+
|
58 |
+
# Perform sentiment analysis
|
59 |
+
with torch.no_grad():
|
60 |
+
outputs = model2(**inputs)
|
61 |
+
logits = outputs.logits
|
62 |
+
predicted_label = torch.argmax(logits, dim=1).item()
|
63 |
+
|
64 |
+
sentiment = "Positive" if predicted_label == 1 else "Negative"
|
65 |
+
st.success(f"The sentiment of the text is: {sentiment}")
|
66 |
+
|