Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- mile2.py +66 -0
- requirements.txt +59 -0
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 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==5.0.1
|
2 |
+
attrs==23.1.0
|
3 |
+
blinker==1.6.2
|
4 |
+
cachetools==5.3.1
|
5 |
+
certifi==2023.5.7
|
6 |
+
charset-normalizer==3.1.0
|
7 |
+
click==8.1.3
|
8 |
+
decorator==5.1.1
|
9 |
+
filelock==3.12.2
|
10 |
+
fsspec==2023.6.0
|
11 |
+
gitdb==4.0.10
|
12 |
+
GitPython==3.1.31
|
13 |
+
huggingface-hub==0.15.1
|
14 |
+
idna==3.4
|
15 |
+
importlib-metadata==6.7.0
|
16 |
+
Jinja2==3.1.2
|
17 |
+
jsonschema==4.17.3
|
18 |
+
markdown-it-py==3.0.0
|
19 |
+
MarkupSafe==2.1.3
|
20 |
+
mdurl==0.1.2
|
21 |
+
mpmath==1.3.0
|
22 |
+
networkx==3.1
|
23 |
+
numpy==1.25.0
|
24 |
+
packaging==23.1
|
25 |
+
pandas==2.0.3
|
26 |
+
Pillow==9.5.0
|
27 |
+
protobuf==4.23.3
|
28 |
+
pyarrow==12.0.1
|
29 |
+
pydeck==0.8.1b0
|
30 |
+
Pygments==2.15.1
|
31 |
+
Pympler==1.0.1
|
32 |
+
pyrsistent==0.19.3
|
33 |
+
python-dateutil==2.8.2
|
34 |
+
pytz==2023.3
|
35 |
+
pytz-deprecation-shim==0.1.0.post0
|
36 |
+
PyYAML==6.0
|
37 |
+
regex==2023.6.3
|
38 |
+
requests==2.31.0
|
39 |
+
rich==13.4.2
|
40 |
+
safetensors==0.3.1
|
41 |
+
six==1.16.0
|
42 |
+
smmap==5.0.0
|
43 |
+
streamlit==1.24.0
|
44 |
+
sympy==1.12
|
45 |
+
tenacity==8.2.2
|
46 |
+
tokenizers==0.13.3
|
47 |
+
toml==0.10.2
|
48 |
+
toolz==0.12.0
|
49 |
+
torch==2.0.1
|
50 |
+
tornado==6.3.2
|
51 |
+
tqdm==4.65.0
|
52 |
+
transformers==4.30.2
|
53 |
+
typing_extensions==4.7.1
|
54 |
+
tzdata==2023.3
|
55 |
+
tzlocal==4.3.1
|
56 |
+
urllib3==2.0.3
|
57 |
+
validators==0.20.0
|
58 |
+
watchdog==3.0.0
|
59 |
+
zipp==3.15.0
|