tidalinn
commited on
Commit
·
24eb986
1
Parent(s):
f79013b
Updated project
Browse files- app.py +38 -2
- requirements.txt +6 -0
- src/__init__.py +0 -0
- src/config.py +15 -0
- src/constants.py +10 -0
- src/utils.py +46 -0
app.py
CHANGED
@@ -1,4 +1,40 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from src.constants import SETTINGS
|
3 |
+
from src.utils import load_models, print_results
|
4 |
|
5 |
+
|
6 |
+
def main():
|
7 |
+
st.title(SETTINGS.title)
|
8 |
+
col1, col2 = st.columns(2)
|
9 |
+
|
10 |
+
with col1:
|
11 |
+
st.header('Model Selection')
|
12 |
+
model_selected = st.radio('Choose model', SETTINGS.models)
|
13 |
+
|
14 |
+
with col2:
|
15 |
+
st.header('Input text')
|
16 |
+
input_text = col2.text_area(
|
17 |
+
'Write or paste any text',
|
18 |
+
value=SETTINGS.sample_text,
|
19 |
+
height=150
|
20 |
+
)
|
21 |
+
|
22 |
+
if st.button('Analyze', type='primary') and input_text != '':
|
23 |
+
pipes = load_models()
|
24 |
+
pipe = pipes[model_selected]
|
25 |
+
|
26 |
+
with st.spinner('Analyzing text...'):
|
27 |
+
output = pipe(input_text)
|
28 |
+
|
29 |
+
st.subheader(f"{SETTINGS.task.replace('-', ' ').capitalize()} results")
|
30 |
+
|
31 |
+
if isinstance(output, dict):
|
32 |
+
output = [output]
|
33 |
+
|
34 |
+
|
35 |
+
for i, result in enumerate(output, 1):
|
36 |
+
print_results(result)
|
37 |
+
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit>=1.40.2
|
2 |
+
spacy>=3.8.2
|
3 |
+
pydantic_settings>=2.6.1
|
4 |
+
transformers>=4.46.3
|
5 |
+
numpy<2
|
6 |
+
torch>=2.2.2
|
src/__init__.py
ADDED
File without changes
|
src/config.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic_settings import BaseSettings
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
|
5 |
+
class Settings(BaseSettings):
|
6 |
+
task: str = 'text-classification'
|
7 |
+
models: List[str] = [
|
8 |
+
'ProsusAI/finbert',
|
9 |
+
'tabularisai/multilingual-sentiment-analysis',
|
10 |
+
'fhamborg/roberta-targeted-sentiment-classification-newsarticles'
|
11 |
+
]
|
12 |
+
|
13 |
+
sample_text: str = 'The quality of exchanged money in cache defines random person as a self-personed financial mentor if he absolutely successed at being thoughtful during the process of calculating the bills'
|
14 |
+
|
15 |
+
title: str = 'Sentiment analyzer'
|
src/constants.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.config import Settings
|
2 |
+
|
3 |
+
COLORS = {
|
4 |
+
'POSITIVE': '#4CAF50', # green
|
5 |
+
'NEGATIVE': '#F44336', # red
|
6 |
+
'NEUTRAL': '#FFC107', # yellow
|
7 |
+
'DEFAULT': '#607D8B' # grey
|
8 |
+
}
|
9 |
+
|
10 |
+
SETTINGS = Settings()
|
src/utils.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from typing import Dict, Any
|
3 |
+
from transformers import pipeline
|
4 |
+
from src.constants import SETTINGS, COLORS
|
5 |
+
|
6 |
+
|
7 |
+
@st.cache_resource
|
8 |
+
def load_models() -> Dict[str, pipeline]:
|
9 |
+
return {
|
10 |
+
model_name: pipeline(task=SETTINGS.task, model=model_name)
|
11 |
+
for model_name in SETTINGS.models
|
12 |
+
}
|
13 |
+
|
14 |
+
|
15 |
+
def colorize_sentiment(label, score) -> str:
|
16 |
+
color = COLORS.get(label, COLORS['DEFAULT'])
|
17 |
+
|
18 |
+
return f"""
|
19 |
+
<div style="
|
20 |
+
display: inline-block;
|
21 |
+
background-color: {color};
|
22 |
+
color: white;
|
23 |
+
padding: 0.2em 0.5em;
|
24 |
+
border-radius: 0.3em;
|
25 |
+
margin: 0.1em;
|
26 |
+
font-size: 0.9em;
|
27 |
+
position: relative;
|
28 |
+
">{label}</div>
|
29 |
+
<div style="
|
30 |
+
font-size: 1em;
|
31 |
+
color: {color};
|
32 |
+
margin-left: 0.5em
|
33 |
+
">{score:.2f}</div>
|
34 |
+
"""
|
35 |
+
|
36 |
+
|
37 |
+
def print_results(result: Dict[str, Any]):
|
38 |
+
label = result['label']
|
39 |
+
score = result['score']
|
40 |
+
|
41 |
+
st.markdown(f"""
|
42 |
+
<div style="display: flex; align-items: center; margin-bottom: 1em;">
|
43 |
+
<span style="font-size: 1.1em; margin-right: 1em;">Overall sentiment:</span>
|
44 |
+
{colorize_sentiment(label, score)}
|
45 |
+
</div>
|
46 |
+
""", unsafe_allow_html=True)
|