Spaces:
Sleeping
Sleeping
Update frontend.py
Browse files- frontend.py +21 -12
frontend.py
CHANGED
@@ -72,6 +72,7 @@ def speak(text, lang='en'):
|
|
72 |
|
73 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
74 |
|
|
|
75 |
# === SINGLE REVIEW ANALYSIS ===
|
76 |
with tab1:
|
77 |
st.title("π ChurnSight AI β Product Feedback Assistant")
|
@@ -80,8 +81,9 @@ with tab1:
|
|
80 |
review = st.text_area("π Enter Customer Feedback", value=st.session_state.review, height=180)
|
81 |
st.session_state.review = review
|
82 |
|
83 |
-
analyze = False # β
|
84 |
col1, col2, col3 = st.columns(3)
|
|
|
85 |
with col1:
|
86 |
analyze = st.button("π Analyze")
|
87 |
with col2:
|
@@ -101,10 +103,7 @@ with tab1:
|
|
101 |
if st.session_state.review and (analyze or st.session_state.trigger_example_analysis):
|
102 |
with st.spinner("Analyzing feedback..."):
|
103 |
try:
|
104 |
-
model_used =
|
105 |
-
None if sentiment_model == "Auto-detect"
|
106 |
-
else sentiment_model
|
107 |
-
)
|
108 |
payload = {
|
109 |
"text": st.session_state.review,
|
110 |
"model": model_used or "distilbert-base-uncased-finetuned-sst-2-english",
|
@@ -164,23 +163,33 @@ with tab1:
|
|
164 |
suggestions = ["What made the user upset?", "Is this user likely to churn?"]
|
165 |
else:
|
166 |
suggestions = ["What are the key takeaways?", "Is there any concern raised?"]
|
|
|
167 |
selected_q = st.selectbox("π‘ Suggested Questions", ["Type your own..."] + suggestions)
|
168 |
q_input = st.text_input("π Your Question") if selected_q == "Type your own..." else selected_q
|
|
|
169 |
if q_input:
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
if st.checkbox("π Show Churn Risk Trends"):
|
178 |
try:
|
179 |
df = pd.DataFrame(st.session_state.churn_log)
|
180 |
df["date"] = pd.to_datetime(df["timestamp"]).dt.date
|
181 |
trend = df.groupby(["date", "churn_risk"]).size().unstack(fill_value=0).reset_index()
|
|
|
182 |
st.markdown("#### π
Daily Churn Trend")
|
183 |
-
y_columns = [col for col in trend.columns if col not in ["date"]]
|
184 |
fig = px.bar(trend, x="date", y=y_columns, barmode="group")
|
185 |
st.plotly_chart(fig, use_container_width=True)
|
186 |
st.download_button("β¬οΈ Export Trend CSV", trend.to_csv(index=False), "churn_trend.csv")
|
|
|
72 |
|
73 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
74 |
|
75 |
+
# === SINGLE REVIEW ANALYSIS ===
|
76 |
# === SINGLE REVIEW ANALYSIS ===
|
77 |
with tab1:
|
78 |
st.title("π ChurnSight AI β Product Feedback Assistant")
|
|
|
81 |
review = st.text_area("π Enter Customer Feedback", value=st.session_state.review, height=180)
|
82 |
st.session_state.review = review
|
83 |
|
84 |
+
analyze = False # β
Flag to track if analyze was clicked
|
85 |
col1, col2, col3 = st.columns(3)
|
86 |
+
|
87 |
with col1:
|
88 |
analyze = st.button("π Analyze")
|
89 |
with col2:
|
|
|
103 |
if st.session_state.review and (analyze or st.session_state.trigger_example_analysis):
|
104 |
with st.spinner("Analyzing feedback..."):
|
105 |
try:
|
106 |
+
model_used = None if sentiment_model == "Auto-detect" else sentiment_model
|
|
|
|
|
|
|
107 |
payload = {
|
108 |
"text": st.session_state.review,
|
109 |
"model": model_used or "distilbert-base-uncased-finetuned-sst-2-english",
|
|
|
163 |
suggestions = ["What made the user upset?", "Is this user likely to churn?"]
|
164 |
else:
|
165 |
suggestions = ["What are the key takeaways?", "Is there any concern raised?"]
|
166 |
+
|
167 |
selected_q = st.selectbox("π‘ Suggested Questions", ["Type your own..."] + suggestions)
|
168 |
q_input = st.text_input("π Your Question") if selected_q == "Type your own..." else selected_q
|
169 |
+
|
170 |
if q_input:
|
171 |
+
try:
|
172 |
+
follow_payload = {
|
173 |
+
"text": st.session_state.review,
|
174 |
+
"question": q_input,
|
175 |
+
"verbosity": verbosity
|
176 |
+
}
|
177 |
+
headers = {"x-api-key": st.session_state.get("api_token", "my-secret-key")}
|
178 |
+
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
179 |
+
if res.ok:
|
180 |
+
st.success(res.json().get("answer"))
|
181 |
+
else:
|
182 |
+
st.error("Failed to get follow-up answer.")
|
183 |
+
except Exception as e:
|
184 |
+
st.error(f"β οΈ Follow-up error: {e}")
|
185 |
|
186 |
if st.checkbox("π Show Churn Risk Trends"):
|
187 |
try:
|
188 |
df = pd.DataFrame(st.session_state.churn_log)
|
189 |
df["date"] = pd.to_datetime(df["timestamp"]).dt.date
|
190 |
trend = df.groupby(["date", "churn_risk"]).size().unstack(fill_value=0).reset_index()
|
191 |
+
y_columns = [col for col in trend.columns if col != "date"]
|
192 |
st.markdown("#### π
Daily Churn Trend")
|
|
|
193 |
fig = px.bar(trend, x="date", y=y_columns, barmode="group")
|
194 |
st.plotly_chart(fig, use_container_width=True)
|
195 |
st.download_button("β¬οΈ Export Trend CSV", trend.to_csv(index=False), "churn_trend.csv")
|