File size: 7,760 Bytes
d09b2a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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)