Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ import time
|
|
| 13 |
import base64
|
| 14 |
import io
|
| 15 |
import streamlit.components.v1 as components
|
|
|
|
| 16 |
|
| 17 |
# Suppress warnings for a clean console
|
| 18 |
logging.getLogger("torch").setLevel(logging.CRITICAL)
|
|
@@ -20,6 +21,14 @@ logging.getLogger("transformers").setLevel(logging.CRITICAL)
|
|
| 20 |
warnings.filterwarnings("ignore")
|
| 21 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Check if CUDA is available, otherwise use CPU
|
| 24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
print(f"Using device: {device}")
|
|
@@ -173,7 +182,7 @@ def process_uploaded_audio(audio_file):
|
|
| 173 |
st.error(f"Error processing uploaded audio: {str(e)}")
|
| 174 |
return None
|
| 175 |
finally:
|
| 176 |
-
if temp_file_path and os.path.exists(temp_file_path)
|
| 177 |
os.remove(temp_file_path)
|
| 178 |
|
| 179 |
# Show model information
|
|
@@ -221,6 +230,19 @@ def custom_audio_recorder():
|
|
| 221 |
}
|
| 222 |
</script>
|
| 223 |
<button id="record-btn" onclick="toggleRecording()">Start Recording</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
"""
|
| 225 |
return components.html(audio_recorder_html, height=100)
|
| 226 |
|
|
@@ -228,18 +250,25 @@ def custom_audio_recorder():
|
|
| 228 |
def display_analysis_results(transcribed_text):
|
| 229 |
emotions_dict, top_emotion, emotion_map, sentiment = perform_emotion_detection(transcribed_text)
|
| 230 |
is_sarcastic, sarcasm_score = perform_sarcasm_detection(transcribed_text)
|
| 231 |
-
st.header("Results")
|
| 232 |
st.text_area("Transcribed Text", transcribed_text, height=100, disabled=True)
|
| 233 |
-
col1, col2 = st.columns(2)
|
| 234 |
with col1:
|
| 235 |
st.subheader("Sentiment")
|
| 236 |
-
|
| 237 |
-
|
| 238 |
st.subheader("Sarcasm")
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# Process base64 audio
|
| 245 |
def process_base64_audio(base64_data):
|
|
@@ -261,27 +290,40 @@ def process_base64_audio(base64_data):
|
|
| 261 |
|
| 262 |
# Main App Logic
|
| 263 |
def main():
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
with tab1:
|
| 266 |
-
|
|
|
|
| 267 |
if audio_file:
|
| 268 |
-
st.audio(audio_file)
|
| 269 |
-
if st.button("Analyze
|
| 270 |
-
with st.spinner("Analyzing..."):
|
| 271 |
-
|
| 272 |
-
if
|
| 273 |
-
|
| 274 |
-
if
|
| 275 |
-
display_analysis_results(
|
|
|
|
|
|
|
| 276 |
with tab2:
|
|
|
|
|
|
|
| 277 |
audio_data = custom_audio_recorder()
|
| 278 |
-
if audio_data and st.button("Analyze
|
| 279 |
-
with st.spinner("
|
| 280 |
-
|
| 281 |
-
if
|
| 282 |
-
|
| 283 |
-
if
|
| 284 |
-
display_analysis_results(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
show_model_info()
|
| 286 |
|
| 287 |
if __name__ == "__main__":
|
|
|
|
| 13 |
import base64
|
| 14 |
import io
|
| 15 |
import streamlit.components.v1 as components
|
| 16 |
+
import numpy as np
|
| 17 |
|
| 18 |
# Suppress warnings for a clean console
|
| 19 |
logging.getLogger("torch").setLevel(logging.CRITICAL)
|
|
|
|
| 21 |
warnings.filterwarnings("ignore")
|
| 22 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 23 |
|
| 24 |
+
# Check if NumPy is available
|
| 25 |
+
try:
|
| 26 |
+
test_array = np.array([1, 2, 3])
|
| 27 |
+
torch.from_numpy(test_array)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"NumPy is not available or incompatible with PyTorch: {str(e)}. Ensure 'numpy' is in requirements.txt and reinstall dependencies.")
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
# Check if CUDA is available, otherwise use CPU
|
| 33 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 34 |
print(f"Using device: {device}")
|
|
|
|
| 182 |
st.error(f"Error processing uploaded audio: {str(e)}")
|
| 183 |
return None
|
| 184 |
finally:
|
| 185 |
+
if temp_file_path and os.path.exists(temp_file_path):
|
| 186 |
os.remove(temp_file_path)
|
| 187 |
|
| 188 |
# Show model information
|
|
|
|
| 230 |
}
|
| 231 |
</script>
|
| 232 |
<button id="record-btn" onclick="toggleRecording()">Start Recording</button>
|
| 233 |
+
<style>
|
| 234 |
+
#record-btn {
|
| 235 |
+
background-color: #f63366;
|
| 236 |
+
color: white;
|
| 237 |
+
border: none;
|
| 238 |
+
padding: 10px 20px;
|
| 239 |
+
border-radius: 5px;
|
| 240 |
+
cursor: pointer;
|
| 241 |
+
}
|
| 242 |
+
#record-btn:hover {
|
| 243 |
+
background-color: #ff0000;
|
| 244 |
+
}
|
| 245 |
+
</style>
|
| 246 |
"""
|
| 247 |
return components.html(audio_recorder_html, height=100)
|
| 248 |
|
|
|
|
| 250 |
def display_analysis_results(transcribed_text):
|
| 251 |
emotions_dict, top_emotion, emotion_map, sentiment = perform_emotion_detection(transcribed_text)
|
| 252 |
is_sarcastic, sarcasm_score = perform_sarcasm_detection(transcribed_text)
|
| 253 |
+
st.header("Analysis Results")
|
| 254 |
st.text_area("Transcribed Text", transcribed_text, height=100, disabled=True)
|
| 255 |
+
col1, col2 = st.columns([1, 2])
|
| 256 |
with col1:
|
| 257 |
st.subheader("Sentiment")
|
| 258 |
+
sentiment_icon = "π" if sentiment == "POSITIVE" else "π" if sentiment == "NEGATIVE" else "π"
|
| 259 |
+
st.markdown(f"{sentiment_icon} {sentiment} (Based on {top_emotion})")
|
| 260 |
st.subheader("Sarcasm")
|
| 261 |
+
sarcasm_icon = "π" if is_sarcastic else "π"
|
| 262 |
+
st.markdown(f"{sarcasm_icon} {'Detected' if is_sarcastic else 'Not Detected'} (Score: {sarcasm_score:.2f})")
|
| 263 |
+
with col2:
|
| 264 |
+
st.subheader("Emotions")
|
| 265 |
+
if emotions_dict:
|
| 266 |
+
st.markdown(f"*Dominant:* {emotion_map.get(top_emotion, 'β')} {top_emotion.capitalize()} (Score: {emotions_dict[top_emotion]:.3f})")
|
| 267 |
+
fig = px.bar(x=list(emotions_dict.keys()), y=list(emotions_dict.values()),
|
| 268 |
+
labels={'x': 'Emotion', 'y': 'Score'}, title="Emotion Distribution")
|
| 269 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 270 |
+
else:
|
| 271 |
+
st.write("No emotions detected.")
|
| 272 |
|
| 273 |
# Process base64 audio
|
| 274 |
def process_base64_audio(base64_data):
|
|
|
|
| 290 |
|
| 291 |
# Main App Logic
|
| 292 |
def main():
|
| 293 |
+
if 'debug_info' not in st.session_state:
|
| 294 |
+
st.session_state.debug_info = []
|
| 295 |
+
tab1, tab2 = st.tabs(["π Upload Audio", "π Record Audio"])
|
| 296 |
with tab1:
|
| 297 |
+
st.header("Upload an Audio File")
|
| 298 |
+
audio_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "ogg"])
|
| 299 |
if audio_file:
|
| 300 |
+
st.audio(audio_file.getvalue())
|
| 301 |
+
if st.button("Analyze Upload", key="analyze_upload"):
|
| 302 |
+
with st.spinner("Analyzing audio..."):
|
| 303 |
+
temp_audio_path = process_uploaded_audio(audio_file)
|
| 304 |
+
if temp_audio_path:
|
| 305 |
+
transcribed_text = transcribe_audio(temp_audio_path)
|
| 306 |
+
if transcribed_text:
|
| 307 |
+
display_analysis_results(transcribed_text)
|
| 308 |
+
else:
|
| 309 |
+
st.error("Could not transcribe audio. Try clearer audio.")
|
| 310 |
with tab2:
|
| 311 |
+
st.header("Record Your Voice")
|
| 312 |
+
st.subheader("Browser-Based Recorder")
|
| 313 |
audio_data = custom_audio_recorder()
|
| 314 |
+
if audio_data and st.button("Analyze Recording", key="analyze_rec"):
|
| 315 |
+
with st.spinner("Processing recording..."):
|
| 316 |
+
temp_audio_path = process_base64_audio(audio_data)
|
| 317 |
+
if temp_audio_path:
|
| 318 |
+
transcribed_text = transcribe_audio(temp_audio_path)
|
| 319 |
+
if transcribed_text:
|
| 320 |
+
display_analysis_results(transcribed_text)
|
| 321 |
+
else:
|
| 322 |
+
st.error("Could not transcribe audio. Speak clearly.")
|
| 323 |
+
st.subheader("Manual Text Input")
|
| 324 |
+
manual_text = st.text_area("Enter text to analyze:", placeholder="Type your text...")
|
| 325 |
+
if st.button("Analyze Text", key="analyze_manual") and manual_text:
|
| 326 |
+
display_analysis_results(manual_text)
|
| 327 |
show_model_info()
|
| 328 |
|
| 329 |
if __name__ == "__main__":
|