app improvement again
Browse files
app.py
CHANGED
|
@@ -14,11 +14,6 @@ st.set_page_config(
|
|
| 14 |
layout="wide"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# Initialize session state
|
| 18 |
-
if 'history' not in st.session_state:
|
| 19 |
-
st.session_state.history = []
|
| 20 |
-
if 'api_health' not in st.session_state:
|
| 21 |
-
st.session_state.api_health = None
|
| 22 |
|
| 23 |
# Custom CSS
|
| 24 |
st.markdown("""
|
|
@@ -43,13 +38,6 @@ st.markdown("""
|
|
| 43 |
</style>
|
| 44 |
""", unsafe_allow_html=True)
|
| 45 |
|
| 46 |
-
def check_api_health():
|
| 47 |
-
"""Check if the API is responsive"""
|
| 48 |
-
try:
|
| 49 |
-
response = requests.get("https://llepogam-hate-speech-detection-api.hf.space/health")
|
| 50 |
-
return response.status_code == 200
|
| 51 |
-
except:
|
| 52 |
-
return False
|
| 53 |
|
| 54 |
def hate_speech_detection(text):
|
| 55 |
"""Make API call with error handling"""
|
|
@@ -86,8 +74,10 @@ def get_severity_class(probability):
|
|
| 86 |
# Header Section
|
| 87 |
st.title("π« Offensive Speech Detection")
|
| 88 |
st.markdown("""
|
| 89 |
-
This application helps identify potentially offensive
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
**How it works:**
|
| 93 |
1. Enter your text in the input box below
|
|
@@ -95,22 +85,13 @@ It uses a machine learning model to analyze text and determine if it contains of
|
|
| 95 |
3. Results show both the classification and confidence level
|
| 96 |
""")
|
| 97 |
|
| 98 |
-
# API Status
|
| 99 |
-
if st.button("Check API Status"):
|
| 100 |
-
with st.spinner("Checking API health..."):
|
| 101 |
-
st.session_state.api_health = check_api_health()
|
| 102 |
-
|
| 103 |
-
if st.session_state.api_health is not None:
|
| 104 |
-
status_color = "green" if st.session_state.api_health else "red"
|
| 105 |
-
status_text = "Online" if st.session_state.api_health else "Offline"
|
| 106 |
-
st.markdown(f"API Status: :{status_color}[{status_text}]")
|
| 107 |
|
| 108 |
# Example inputs
|
| 109 |
with st.expander("π Example Inputs"):
|
| 110 |
st.markdown("""
|
| 111 |
Try these example texts to test the model:
|
| 112 |
1. "Have a great day!"
|
| 113 |
-
2. "
|
| 114 |
3. "You're amazing!"
|
| 115 |
|
| 116 |
Click on any example to copy it to the input box.
|
|
@@ -126,22 +107,23 @@ with st.expander("π Example Inputs"):
|
|
| 126 |
with st.expander("β Frequently Asked Questions"):
|
| 127 |
st.markdown("""
|
| 128 |
**Q: What is considered offensive speech?**
|
| 129 |
-
- A: The model
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
**Q: How accurate is the detection?**
|
| 132 |
-
- A: The model
|
| 133 |
|
| 134 |
-
**Q: What happens to my input data?**
|
| 135 |
-
- A: Your text is only used for prediction and temporarily stored in your session history.
|
| 136 |
""")
|
| 137 |
|
| 138 |
# Text Input Section
|
| 139 |
-
max_chars =
|
| 140 |
user_input = st.text_area(
|
| 141 |
"Enter text to analyze:",
|
| 142 |
height=100,
|
| 143 |
key="user_input",
|
| 144 |
-
help="Enter the text you want to analyze for offensive content. Maximum
|
| 145 |
max_chars=max_chars
|
| 146 |
)
|
| 147 |
|
|
@@ -166,7 +148,7 @@ if user_input:
|
|
| 166 |
st.error(f"Error: {error}")
|
| 167 |
else:
|
| 168 |
# Format probability as percentage
|
| 169 |
-
probability_pct = result['probability']
|
| 170 |
|
| 171 |
# Create prediction box with appropriate severity class
|
| 172 |
severity_class = get_severity_class(result['probability'])
|
|
@@ -188,9 +170,9 @@ if user_input:
|
|
| 188 |
'axis': {'range': [0, 100]},
|
| 189 |
'bar': {'color': "darkblue"},
|
| 190 |
'steps': [
|
| 191 |
-
{'range': [0,
|
| 192 |
-
{'range': [
|
| 193 |
-
{'range': [
|
| 194 |
]
|
| 195 |
}
|
| 196 |
))
|
|
@@ -231,7 +213,7 @@ if st.session_state.history:
|
|
| 231 |
st.markdown("---")
|
| 232 |
st.markdown("""
|
| 233 |
<div style='text-align: center'>
|
| 234 |
-
<p>Developed with β€οΈ
|
| 235 |
</div>
|
| 236 |
""", unsafe_allow_html=True)
|
| 237 |
|
|
|
|
| 14 |
layout="wide"
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Custom CSS
|
| 19 |
st.markdown("""
|
|
|
|
| 38 |
</style>
|
| 39 |
""", unsafe_allow_html=True)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def hate_speech_detection(text):
|
| 43 |
"""Make API call with error handling"""
|
|
|
|
| 74 |
# Header Section
|
| 75 |
st.title("π« Offensive Speech Detection")
|
| 76 |
st.markdown("""
|
| 77 |
+
This application helps identify potentially offensive oontent in text provided by an user.
|
| 78 |
+
|
| 79 |
+
It uses a machine learning model to analyze text and determine if it contains offensive speech.
|
| 80 |
+
|
| 81 |
|
| 82 |
**How it works:**
|
| 83 |
1. Enter your text in the input box below
|
|
|
|
| 85 |
3. Results show both the classification and confidence level
|
| 86 |
""")
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Example inputs
|
| 90 |
with st.expander("π Example Inputs"):
|
| 91 |
st.markdown("""
|
| 92 |
Try these example texts to test the model:
|
| 93 |
1. "Have a great day!"
|
| 94 |
+
2. "You are the worst person in the world."
|
| 95 |
3. "You're amazing!"
|
| 96 |
|
| 97 |
Click on any example to copy it to the input box.
|
|
|
|
| 107 |
with st.expander("β Frequently Asked Questions"):
|
| 108 |
st.markdown("""
|
| 109 |
**Q: What is considered offensive speech?**
|
| 110 |
+
- A: The model is using a dataset of tweets, which were tagged as offensive or not. More information on the dataset can be found here : https://huggingface.co/datasets/christophsonntag/OLID
|
| 111 |
+
|
| 112 |
+
**Q: How is the prediction done?**
|
| 113 |
+
- A: The model predicts a value between 1 and 0. The closer it is to 1, the more offensive is the prediction. When the prediction is higher than 0.5, the text is considered as offensive
|
| 114 |
|
| 115 |
**Q: How accurate is the detection?**
|
| 116 |
+
- A: The model created has an accuracy of 73.1%, which means than prediction are correct almost 3 times out of four. When the targeted values is below 0.3 or higher than 0.7, it means than there is a high level of confidence in the prediction
|
| 117 |
|
|
|
|
|
|
|
| 118 |
""")
|
| 119 |
|
| 120 |
# Text Input Section
|
| 121 |
+
max_chars = 140
|
| 122 |
user_input = st.text_area(
|
| 123 |
"Enter text to analyze:",
|
| 124 |
height=100,
|
| 125 |
key="user_input",
|
| 126 |
+
help="Enter the text you want to analyze for offensive content. Maximum 140 characters.",
|
| 127 |
max_chars=max_chars
|
| 128 |
)
|
| 129 |
|
|
|
|
| 148 |
st.error(f"Error: {error}")
|
| 149 |
else:
|
| 150 |
# Format probability as percentage
|
| 151 |
+
probability_pct = result['probability']
|
| 152 |
|
| 153 |
# Create prediction box with appropriate severity class
|
| 154 |
severity_class = get_severity_class(result['probability'])
|
|
|
|
| 170 |
'axis': {'range': [0, 100]},
|
| 171 |
'bar': {'color': "darkblue"},
|
| 172 |
'steps': [
|
| 173 |
+
{'range': [0, 0.3], 'color': "lightgreen"},
|
| 174 |
+
{'range': [0.3, 0.7], 'color': "orange"},
|
| 175 |
+
{'range': [0.7, 1], 'color': "red"}
|
| 176 |
]
|
| 177 |
}
|
| 178 |
))
|
|
|
|
| 213 |
st.markdown("---")
|
| 214 |
st.markdown("""
|
| 215 |
<div style='text-align: center'>
|
| 216 |
+
<p>Developed with β€οΈ by Louis Le Pogam</p>
|
| 217 |
</div>
|
| 218 |
""", unsafe_allow_html=True)
|
| 219 |
|