Spaces:
Sleeping
Sleeping
| 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(""" | |
| <style> | |
| .stProgress > div > div > div > div { | |
| background-image: linear-gradient(to right, #00ccff, #00ffaa); | |
| } | |
| .stButton>button { | |
| border-radius: 20px; | |
| font-weight: bold; | |
| } | |
| .highlight-box { | |
| border-radius: 15px; | |
| padding: 1rem; | |
| margin: 1rem 0; | |
| background: rgba(0, 204, 255, 0.1); | |
| border-left: 5px solid #00ccff; | |
| } | |
| .metric-card { | |
| border-radius: 10px; | |
| padding: 1.5rem; | |
| margin: 0.5rem; | |
| background: rgba(255, 255, 255, 0.1); | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| transition: transform 0.3s; | |
| } | |
| .metric-card:hover { | |
| transform: translateY(-5px); | |
| } | |
| </style> | |
| """, 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'<div class="highlight-box">{raw_text}</div>', unsafe_allow_html=True) | |
| with tab2: | |
| st.markdown(f'<div class="highlight-box">{ml_text}</div>', unsafe_allow_html=True) | |
| st.subheader("๐ Key Metrics") | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.markdown('<div class="metric-card"><h3>Sentiment Match</h3><p style="font-size: 2rem;">โ 85%</p></div>', unsafe_allow_html=True) | |
| with col2: | |
| st.markdown('<div class="metric-card"><h3>Intent Match</h3><p style="font-size: 2rem;">๐ฏ 78%</p></div>', 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'<div class="metric-card"><h3>Lead Score</h3><p style="font-size: 2rem; color: {score_color}">๐ฅ {lead_score}/100</p></div>', 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) | |