Spaces:
Running
Running
Upload webApp.py
#1
by FaithCodes - opened
- src/webApp.py +17 -11
src/webApp.py
CHANGED
|
@@ -131,10 +131,12 @@ def engineer_features(review, rating, verified, review_frequency):
|
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
-
def gauge_chart(value, title):
|
| 135 |
"""
|
| 136 |
-
Renders a gauge chart showing model
|
|
|
|
| 137 |
"""
|
|
|
|
| 138 |
fig = go.Figure(go.Indicator(
|
| 139 |
mode="gauge+number",
|
| 140 |
value=round(value * 100, 1),
|
|
@@ -142,12 +144,13 @@ def gauge_chart(value, title):
|
|
| 142 |
title={"text": title},
|
| 143 |
gauge={
|
| 144 |
"axis": {"range": [0, 100]},
|
| 145 |
-
"bar": {"color": "#e63946" if value >
|
| 146 |
"steps": [
|
| 147 |
-
{"range": [0,
|
| 148 |
-
{"range": [
|
| 149 |
{"range": [70, 100], "color": "#f8d7da"},
|
| 150 |
],
|
|
|
|
| 151 |
}
|
| 152 |
))
|
| 153 |
fig.update_layout(height=250, margin=dict(t=40, b=0, l=20, r=20))
|
|
@@ -582,7 +585,7 @@ if analyse_clicked:
|
|
| 582 |
pred = 1 if prob > threshold else 0
|
| 583 |
label = "Suspicious" if pred == 1 else "Not Suspicious"
|
| 584 |
prob_pct = round(prob * 100, 1)
|
| 585 |
-
likely = "likely" if
|
| 586 |
|
| 587 |
# Show reviewer frequency note
|
| 588 |
if not freq_found:
|
|
@@ -601,19 +604,21 @@ if analyse_clicked:
|
|
| 601 |
with sum_col2:
|
| 602 |
if pred == 1:
|
| 603 |
st.error(
|
| 604 |
-
f"Review: **Suspicious** \
|
| 605 |
else:
|
| 606 |
st.success(
|
| 607 |
-
f"Review: **Not Suspicious** \
|
| 608 |
|
| 609 |
# side by side gauge charts
|
| 610 |
gauge_col1, gauge_col2 = st.columns(2)
|
| 611 |
with gauge_col1:
|
| 612 |
st.plotly_chart(gauge_chart(
|
| 613 |
-
s_score, "Sentiment Confidence"
|
|
|
|
|
|
|
| 614 |
with gauge_col2:
|
| 615 |
st.plotly_chart(gauge_chart(
|
| 616 |
-
prob, "Suspicion
|
| 617 |
|
| 618 |
# sentiment detailed breakdown
|
| 619 |
with st.expander("Sentiment Analysis: Detailed Breakdown", expanded=True):
|
|
@@ -702,7 +707,8 @@ if analyse_clicked:
|
|
| 702 |
|
| 703 |
# Interpretation sentence
|
| 704 |
st.markdown(
|
| 705 |
-
f"The model
|
|
|
|
| 706 |
f"**{likely} suspicious** as a result of the following features:"
|
| 707 |
)
|
| 708 |
|
|
|
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
+
def gauge_chart(value, title, threshold=0.5, bar_color=None):
|
| 135 |
"""
|
| 136 |
+
Renders a gauge chart showing a model score.
|
| 137 |
+
The bar turns red above the decision threshold, which is also marked on the dial.
|
| 138 |
"""
|
| 139 |
+
t = threshold * 100
|
| 140 |
fig = go.Figure(go.Indicator(
|
| 141 |
mode="gauge+number",
|
| 142 |
value=round(value * 100, 1),
|
|
|
|
| 144 |
title={"text": title},
|
| 145 |
gauge={
|
| 146 |
"axis": {"range": [0, 100]},
|
| 147 |
+
"bar": {"color": bar_color or ("#e63946" if value > threshold else "#2a9d8f")},
|
| 148 |
"steps": [
|
| 149 |
+
{"range": [0, t], "color": "#d4edda"},
|
| 150 |
+
{"range": [t, 70], "color": "#fff3cd"},
|
| 151 |
{"range": [70, 100], "color": "#f8d7da"},
|
| 152 |
],
|
| 153 |
+
"threshold": {"line": {"color": "#333", "width": 3}, "thickness": 0.8, "value": t},
|
| 154 |
}
|
| 155 |
))
|
| 156 |
fig.update_layout(height=250, margin=dict(t=40, b=0, l=20, r=20))
|
|
|
|
| 585 |
pred = 1 if prob > threshold else 0
|
| 586 |
label = "Suspicious" if pred == 1 else "Not Suspicious"
|
| 587 |
prob_pct = round(prob * 100, 1)
|
| 588 |
+
likely = "likely" if pred == 1 else "likely not"
|
| 589 |
|
| 590 |
# Show reviewer frequency note
|
| 591 |
if not freq_found:
|
|
|
|
| 604 |
with sum_col2:
|
| 605 |
if pred == 1:
|
| 606 |
st.error(
|
| 607 |
+
f"Review: **Suspicious** \nSuspicion score: **{prob_pct}%**")
|
| 608 |
else:
|
| 609 |
st.success(
|
| 610 |
+
f"Review: **Not Suspicious** \nSuspicion score: **{prob_pct}%**")
|
| 611 |
|
| 612 |
# side by side gauge charts
|
| 613 |
gauge_col1, gauge_col2 = st.columns(2)
|
| 614 |
with gauge_col1:
|
| 615 |
st.plotly_chart(gauge_chart(
|
| 616 |
+
s_score, "Sentiment Confidence",
|
| 617 |
+
bar_color="#2a9d8f" if s_label == "LABEL_1" else "#e63946"),
|
| 618 |
+
use_container_width=True)
|
| 619 |
with gauge_col2:
|
| 620 |
st.plotly_chart(gauge_chart(
|
| 621 |
+
prob, "Suspicion Score", threshold=threshold), use_container_width=True)
|
| 622 |
|
| 623 |
# sentiment detailed breakdown
|
| 624 |
with st.expander("Sentiment Analysis: Detailed Breakdown", expanded=True):
|
|
|
|
| 707 |
|
| 708 |
# Interpretation sentence
|
| 709 |
st.markdown(
|
| 710 |
+
f"The model gives this review a suspicion score of **{prob_pct}%** "
|
| 711 |
+
f"(reviews above {int(threshold * 100)}% are flagged), so it is "
|
| 712 |
f"**{likely} suspicious** as a result of the following features:"
|
| 713 |
)
|
| 714 |
|