Spaces:
Sleeping
Sleeping
Update frontend.py
Browse files- frontend.py +16 -65
frontend.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
-
import azure.cognitiveservices.speech as speechsdk
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
import plotly.express as px
|
|
@@ -57,48 +56,6 @@ with st.sidebar:
|
|
| 57 |
use_aspects = st.checkbox("π Detect Pain Points")
|
| 58 |
use_explain_bulk = st.checkbox("π§ Generate PM Insight (Bulk)")
|
| 59 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
| 60 |
-
voice_lang = st.selectbox("π Voice Language", ["en", "fr", "es", "de", "hi", "zh"])
|
| 61 |
-
|
| 62 |
-
# Text-to-Speech
|
| 63 |
-
# Setup usage tracking
|
| 64 |
-
if "tts_usage_count" not in st.session_state:
|
| 65 |
-
st.session_state.tts_usage_count = 0
|
| 66 |
-
if "enable_audio" not in st.session_state:
|
| 67 |
-
st.session_state.enable_audio = False
|
| 68 |
-
|
| 69 |
-
# Azure TTS function
|
| 70 |
-
def azure_speak(text, lang='en-US'):
|
| 71 |
-
import os
|
| 72 |
-
if "HF_SPACE_ID" in os.environ:
|
| 73 |
-
st.warning("Azure TTS is not supported on Hugging Face Spaces. Using fallback TTS.")
|
| 74 |
-
return None
|
| 75 |
-
|
| 76 |
-
if st.session_state.tts_usage_count > 20:
|
| 77 |
-
st.warning("π TTS usage limit reached.")
|
| 78 |
-
return None
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
import azure.cognitiveservices.speech as speechsdk
|
| 82 |
-
speech_config = speechsdk.SpeechConfig(
|
| 83 |
-
subscription=st.secrets["AZURE_SPEECH_KEY"],
|
| 84 |
-
region=st.secrets["AZURE_REGION"]
|
| 85 |
-
)
|
| 86 |
-
speech_config.speech_synthesis_language = lang
|
| 87 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmpfile:
|
| 88 |
-
audio_config = speechsdk.audio.AudioOutputConfig(filename=tmpfile.name)
|
| 89 |
-
synthesizer = speechsdk.SpeechSynthesizer(speech_config, audio_config)
|
| 90 |
-
result = synthesizer.speak_text_async(text).get()
|
| 91 |
-
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
|
| 92 |
-
st.session_state.tts_usage_count += 1
|
| 93 |
-
return tmpfile.name
|
| 94 |
-
else:
|
| 95 |
-
st.error("β Azure TTS failed.")
|
| 96 |
-
return None
|
| 97 |
-
except Exception as e:
|
| 98 |
-
st.error(f"Azure TTS error: {e}")
|
| 99 |
-
return None
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
| 104 |
|
|
@@ -146,7 +103,11 @@ with tab1:
|
|
| 146 |
if res.ok:
|
| 147 |
st.session_state.last_response = res.json()
|
| 148 |
else:
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
except Exception as e:
|
| 151 |
st.error(f"π« Exception: {e}")
|
| 152 |
|
|
@@ -165,7 +126,6 @@ with tab1:
|
|
| 165 |
if data.get("pain_points"):
|
| 166 |
st.error("π Pain Points: " + ", ".join(data["pain_points"]))
|
| 167 |
|
| 168 |
-
# Add to churn log
|
| 169 |
try:
|
| 170 |
st.session_state.churn_log.append({
|
| 171 |
"timestamp": datetime.now(),
|
|
@@ -178,21 +138,6 @@ with tab1:
|
|
| 178 |
except Exception as e:
|
| 179 |
st.warning(f"π§ͺ Logging failed: {e}")
|
| 180 |
|
| 181 |
-
# Only show toggle and button for audio
|
| 182 |
-
st.subheader("π Audio Summary")
|
| 183 |
-
st.session_state.enable_audio = st.toggle("π§ Generate Audio Summary")
|
| 184 |
-
|
| 185 |
-
if st.session_state.enable_audio:
|
| 186 |
-
if st.session_state.tts_usage_count > 20:
|
| 187 |
-
st.warning("π Azure TTS usage limit reached for this session.")
|
| 188 |
-
else:
|
| 189 |
-
if st.button("βΆοΈ Generate & Play Audio"):
|
| 190 |
-
audio_path = azure_speak(data["summary"], lang=f"{voice_lang}-US")
|
| 191 |
-
if audio_path:
|
| 192 |
-
audio_bytes = open(audio_path, "rb").read()
|
| 193 |
-
st.audio(audio_bytes, format="audio/mp3")
|
| 194 |
-
st.download_button("β¬οΈ Download Audio", audio_bytes, "summary.mp3")
|
| 195 |
-
|
| 196 |
st.markdown("### π Ask a Follow-Up")
|
| 197 |
sentiment = data["sentiment"]["label"].lower()
|
| 198 |
churn = data.get("churn_risk", "")
|
|
@@ -215,12 +160,15 @@ with tab1:
|
|
| 215 |
"verbosity": verbosity
|
| 216 |
}
|
| 217 |
headers = {"x-api-key": api_token}
|
| 218 |
-
|
| 219 |
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
| 220 |
if res.ok:
|
| 221 |
st.success(res.json().get("answer"))
|
| 222 |
else:
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
st.error(f"β οΈ Follow-up error: {e}")
|
| 226 |
|
|
@@ -237,7 +185,6 @@ with tab1:
|
|
| 237 |
except Exception as e:
|
| 238 |
st.error(f"Trend error: {e}")
|
| 239 |
|
| 240 |
-
|
| 241 |
# === BULK REVIEW ANALYSIS ===
|
| 242 |
with tab2:
|
| 243 |
st.title("π Bulk Feedback Analysis")
|
|
@@ -261,6 +208,10 @@ with tab2:
|
|
| 261 |
st.dataframe(df)
|
| 262 |
st.download_button("β¬οΈ Export Results CSV", df.to_csv(index=False), "bulk_results.csv")
|
| 263 |
else:
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
-
st.error(f"Bulk analysis failed: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import plotly.express as px
|
|
|
|
| 56 |
use_aspects = st.checkbox("π Detect Pain Points")
|
| 57 |
use_explain_bulk = st.checkbox("π§ Generate PM Insight (Bulk)")
|
| 58 |
verbosity = st.radio("π£οΈ Response Style", ["Brief", "Detailed"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
tab1, tab2 = st.tabs(["π§ Analyze Review", "π Bulk Reviews"])
|
| 61 |
|
|
|
|
| 103 |
if res.ok:
|
| 104 |
st.session_state.last_response = res.json()
|
| 105 |
else:
|
| 106 |
+
try:
|
| 107 |
+
err_detail = res.json().get("detail", "No detail provided.")
|
| 108 |
+
except Exception:
|
| 109 |
+
err_detail = res.text
|
| 110 |
+
st.error(f"β Backend Error ({res.status_code}): {err_detail}")
|
| 111 |
except Exception as e:
|
| 112 |
st.error(f"π« Exception: {e}")
|
| 113 |
|
|
|
|
| 126 |
if data.get("pain_points"):
|
| 127 |
st.error("π Pain Points: " + ", ".join(data["pain_points"]))
|
| 128 |
|
|
|
|
| 129 |
try:
|
| 130 |
st.session_state.churn_log.append({
|
| 131 |
"timestamp": datetime.now(),
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
st.warning(f"π§ͺ Logging failed: {e}")
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
st.markdown("### π Ask a Follow-Up")
|
| 142 |
sentiment = data["sentiment"]["label"].lower()
|
| 143 |
churn = data.get("churn_risk", "")
|
|
|
|
| 160 |
"verbosity": verbosity
|
| 161 |
}
|
| 162 |
headers = {"x-api-key": api_token}
|
|
|
|
| 163 |
res = requests.post(f"{backend_url}/followup/", json=follow_payload, headers=headers)
|
| 164 |
if res.ok:
|
| 165 |
st.success(res.json().get("answer"))
|
| 166 |
else:
|
| 167 |
+
try:
|
| 168 |
+
err_detail = res.json().get("detail", "No detail provided.")
|
| 169 |
+
except Exception:
|
| 170 |
+
err_detail = res.text
|
| 171 |
+
st.error(f"β Follow-up API Error ({res.status_code}): {err_detail}")
|
| 172 |
except Exception as e:
|
| 173 |
st.error(f"β οΈ Follow-up error: {e}")
|
| 174 |
|
|
|
|
| 185 |
except Exception as e:
|
| 186 |
st.error(f"Trend error: {e}")
|
| 187 |
|
|
|
|
| 188 |
# === BULK REVIEW ANALYSIS ===
|
| 189 |
with tab2:
|
| 190 |
st.title("π Bulk Feedback Analysis")
|
|
|
|
| 208 |
st.dataframe(df)
|
| 209 |
st.download_button("β¬οΈ Export Results CSV", df.to_csv(index=False), "bulk_results.csv")
|
| 210 |
else:
|
| 211 |
+
try:
|
| 212 |
+
err_detail = res.json().get("detail", "No detail provided.")
|
| 213 |
+
except Exception:
|
| 214 |
+
err_detail = res.text
|
| 215 |
+
st.error(f"β Bulk API Error ({res.status_code}): {err_detail}")
|
| 216 |
except Exception as e:
|
| 217 |
+
st.error(f"Bulk analysis failed: {e}")
|