import streamlit as st from PIL import Image import json import os import tempfile from streamlit_app import ( MalayalamTranscriptionPipeline, analyze_text, save_analysis_to_csv, compare_analyses, print_analysis_summary ) # Function to load Lottie or fallback image def load_lottie(filepath): if os.path.exists(filepath): with open(filepath, "r") as f: return json.load(f) return None def display_lottie_or_image(lottie_data, fallback_image_path, height=200, key=None): try: from streamlit_lottie import st_lottie if lottie_data: st_lottie(lottie_data, height=height, key=key) return except ImportError: pass if os.path.exists(fallback_image_path): img = Image.open(fallback_image_path) st.image(img, width=height) else: st.warning("Animation and fallback image not found.") # Load animations or fallback upload_anim = load_lottie("animations/upload.json") analyze_anim = load_lottie("animations/analyze.json") results_anim = load_lottie("animations/results.json") # Page config st.set_page_config( page_title="Malayalam Audio Analyzer", page_icon="🎤", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS st.markdown(""" """, unsafe_allow_html=True) # Title st.title("đŸŽ™ī¸ Malayalam Audio Intelligence Analyzer") st.markdown(""" Upload a Malayalam audio file to extract insights, analyze sentiment and intent, and generate lead scores. """) # Sidebar info with st.sidebar: st.header("â„šī¸ About") st.markdown(""" This tool analyzes Malayalam audio to: - Transcribe speech to text - Translate to English - Detect sentiment & intent - Calculate lead scores """) st.header("📁 Supported Formats") st.markdown("MP3, WAV, AAC, M4A, FLAC, OGG, WMA") # File Upload with st.container(): col1, col2 = st.columns([3, 1]) with col1: uploaded_file = st.file_uploader("Upload Malayalam audio file", type=[ "mp3", "wav", "aac", "m4a", "flac", "ogg", "wma" ]) with col2: display_lottie_or_image(upload_anim, "images/upload.png", height=150, key="upload") if uploaded_file is not None: with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp: tmp.write(uploaded_file.read()) tmp_path = tmp.name transcriber = MalayalamTranscriptionPipeline() try: with st.container(): st.subheader("🔍 Processing Pipeline") col1, col2 = st.columns([3, 1]) with col1: progress_bar = st.progress(0) status_text = st.empty() status_text.markdown("### 🎧 Transcribing audio...") progress_bar.progress(20) results = transcriber.transcribe_audio(tmp_path) progress_bar.progress(40) if not results or not results.get("raw_transcription"): st.error("Transcription failed.") else: raw_text = results["raw_transcription"] status_text.markdown("### 🌐 Translating to English...") progress_bar.progress(60) translated = transcriber.translate_to_malayalam(results) ml_text = translated.get("malayalam_translation", "") progress_bar.progress(80) status_text.markdown("### 📊 Analyzing content...") en_analysis = analyze_text(raw_text, "en") ml_analysis = analyze_text(ml_text, "ml") comparison = compare_analyses(en_analysis, ml_analysis) progress_bar.progress(100) status_text.success("✅ Analysis completed!") with col2: display_lottie_or_image(analyze_anim, "images/analyze.png", height=200, key="analyze") st.subheader("📋 Results Overview") tab1, tab2 = st.tabs(["English Transcription", "Malayalam Translation"]) with tab1: st.markdown(f'
{raw_text}
', unsafe_allow_html=True) with tab2: st.markdown(f'
{ml_text}
', unsafe_allow_html=True) st.subheader("📊 Key Metrics") col1, col2, col3 = st.columns(3) with col1: st.markdown('

Sentiment Match

✅ 85%

', unsafe_allow_html=True) with col2: st.markdown('

Intent Match

đŸŽ¯ 78%

', unsafe_allow_html=True) with col3: en_avg = sum(x["sentiment_score"] for x in en_analysis) / len(en_analysis) if en_analysis else 0 ml_avg = sum(x["sentiment_score"] for x in ml_analysis) / len(ml_analysis) if ml_analysis else 0 lead_score = int(((en_avg + ml_avg) / 2) * 100) score_color = "#00ff88" if lead_score >= 70 else "#ffaa00" if lead_score >= 40 else "#ff5555" st.markdown(f'

Lead Score

đŸ”Ĩ {lead_score}/100

', unsafe_allow_html=True) with st.expander("🔍 Detailed Sentiment Analysis"): col1, col2 = st.columns(2) with col1: st.markdown("#### đŸ‡Ŧ🇧 English Analysis") print_analysis_summary(en_analysis, "English") with col2: st.markdown("#### đŸ‡ŽđŸ‡ŗ Malayalam Analysis") print_analysis_summary(ml_analysis, "Malayalam") st.subheader("đŸ“Ĩ Download Results") col1, col2, col3 = st.columns(3) en_csv = save_analysis_to_csv(en_analysis, "english") ml_csv = save_analysis_to_csv(ml_analysis, "malayalam") comparison_csv = save_analysis_to_csv(comparison, "comparison") with col1: if en_csv and os.path.exists(en_csv): with open(en_csv, "rb") as f: st.download_button("âŦ‡ī¸ English Analysis", f.read(), file_name=os.path.basename(en_csv), mime="text/csv") with col2: if ml_csv and os.path.exists(ml_csv): with open(ml_csv, "rb") as f: st.download_button("âŦ‡ī¸ Malayalam Analysis", f.read(), file_name=os.path.basename(ml_csv), mime="text/csv") with col3: if comparison_csv and os.path.exists(comparison_csv): with open(comparison_csv, "rb") as f: st.download_button("âŦ‡ī¸ Comparison Report", f.read(), file_name=os.path.basename(comparison_csv), mime="text/csv") display_lottie_or_image(results_anim, "images/results.png", height=300, key="results") except Exception as e: st.error(f"❌ Error: {str(e)}") st.exception(e) finally: transcriber.cleanup() os.remove(tmp_path)