Spaces:
Sleeping
Sleeping
Rename streamlit_main.py to app.py
Browse files- streamlit_main.py โ app.py +206 -206
streamlit_main.py โ app.py
RENAMED
|
@@ -1,206 +1,206 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
import tempfile
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
MalayalamTranscriptionPipeline,
|
| 9 |
-
analyze_text,
|
| 10 |
-
save_analysis_to_csv,
|
| 11 |
-
compare_analyses,
|
| 12 |
-
print_analysis_summary
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# Function to load Lottie or fallback image
|
| 16 |
-
def load_lottie(filepath):
|
| 17 |
-
if os.path.exists(filepath):
|
| 18 |
-
with open(filepath, "r") as f:
|
| 19 |
-
return json.load(f)
|
| 20 |
-
return None
|
| 21 |
-
|
| 22 |
-
def display_lottie_or_image(lottie_data, fallback_image_path, height=200, key=None):
|
| 23 |
-
try:
|
| 24 |
-
from streamlit_lottie import st_lottie
|
| 25 |
-
if lottie_data:
|
| 26 |
-
st_lottie(lottie_data, height=height, key=key)
|
| 27 |
-
return
|
| 28 |
-
except ImportError:
|
| 29 |
-
pass
|
| 30 |
-
|
| 31 |
-
if os.path.exists(fallback_image_path):
|
| 32 |
-
img = Image.open(fallback_image_path)
|
| 33 |
-
st.image(img, width=height)
|
| 34 |
-
else:
|
| 35 |
-
st.warning("Animation and fallback image not found.")
|
| 36 |
-
|
| 37 |
-
# Load animations or fallback
|
| 38 |
-
upload_anim = load_lottie("animations/upload.json")
|
| 39 |
-
analyze_anim = load_lottie("animations/analyze.json")
|
| 40 |
-
results_anim = load_lottie("animations/results.json")
|
| 41 |
-
|
| 42 |
-
# Page config
|
| 43 |
-
st.set_page_config(
|
| 44 |
-
page_title="Malayalam Audio Analyzer",
|
| 45 |
-
page_icon="๐ค",
|
| 46 |
-
layout="wide",
|
| 47 |
-
initial_sidebar_state="expanded"
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# Custom CSS
|
| 51 |
-
st.markdown("""
|
| 52 |
-
<style>
|
| 53 |
-
.stProgress > div > div > div > div {
|
| 54 |
-
background-image: linear-gradient(to right, #00ccff, #00ffaa);
|
| 55 |
-
}
|
| 56 |
-
.stButton>button {
|
| 57 |
-
border-radius: 20px;
|
| 58 |
-
font-weight: bold;
|
| 59 |
-
}
|
| 60 |
-
.highlight-box {
|
| 61 |
-
border-radius: 15px;
|
| 62 |
-
padding: 1rem;
|
| 63 |
-
margin: 1rem 0;
|
| 64 |
-
background: rgba(0, 204, 255, 0.1);
|
| 65 |
-
border-left: 5px solid #00ccff;
|
| 66 |
-
}
|
| 67 |
-
.metric-card {
|
| 68 |
-
border-radius: 10px;
|
| 69 |
-
padding: 1.5rem;
|
| 70 |
-
margin: 0.5rem;
|
| 71 |
-
background: rgba(255, 255, 255, 0.1);
|
| 72 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 73 |
-
transition: transform 0.3s;
|
| 74 |
-
}
|
| 75 |
-
.metric-card:hover {
|
| 76 |
-
transform: translateY(-5px);
|
| 77 |
-
}
|
| 78 |
-
</style>
|
| 79 |
-
""", unsafe_allow_html=True)
|
| 80 |
-
|
| 81 |
-
# Title
|
| 82 |
-
st.title("๐๏ธ Malayalam Audio Intelligence Analyzer")
|
| 83 |
-
st.markdown("""
|
| 84 |
-
Upload a Malayalam audio file to extract insights, analyze sentiment and intent, and generate lead scores.
|
| 85 |
-
""")
|
| 86 |
-
|
| 87 |
-
# Sidebar info
|
| 88 |
-
with st.sidebar:
|
| 89 |
-
st.header("โน๏ธ About")
|
| 90 |
-
st.markdown("""
|
| 91 |
-
This tool analyzes Malayalam audio to:
|
| 92 |
-
- Transcribe speech to text
|
| 93 |
-
- Translate to English
|
| 94 |
-
- Detect sentiment & intent
|
| 95 |
-
- Calculate lead scores
|
| 96 |
-
""")
|
| 97 |
-
st.header("๐ Supported Formats")
|
| 98 |
-
st.markdown("MP3, WAV, AAC, M4A, FLAC, OGG, WMA")
|
| 99 |
-
|
| 100 |
-
# File Upload
|
| 101 |
-
with st.container():
|
| 102 |
-
col1, col2 = st.columns([3, 1])
|
| 103 |
-
with col1:
|
| 104 |
-
uploaded_file = st.file_uploader("Upload Malayalam audio file", type=[
|
| 105 |
-
"mp3", "wav", "aac", "m4a", "flac", "ogg", "wma"
|
| 106 |
-
])
|
| 107 |
-
with col2:
|
| 108 |
-
display_lottie_or_image(upload_anim, "images/upload.png", height=150, key="upload")
|
| 109 |
-
|
| 110 |
-
if uploaded_file is not None:
|
| 111 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp:
|
| 112 |
-
tmp.write(uploaded_file.read())
|
| 113 |
-
tmp_path = tmp.name
|
| 114 |
-
|
| 115 |
-
transcriber = MalayalamTranscriptionPipeline()
|
| 116 |
-
|
| 117 |
-
try:
|
| 118 |
-
with st.container():
|
| 119 |
-
st.subheader("๐ Processing Pipeline")
|
| 120 |
-
col1, col2 = st.columns([3, 1])
|
| 121 |
-
with col1:
|
| 122 |
-
progress_bar = st.progress(0)
|
| 123 |
-
status_text = st.empty()
|
| 124 |
-
|
| 125 |
-
status_text.markdown("### ๐ง Transcribing audio...")
|
| 126 |
-
progress_bar.progress(20)
|
| 127 |
-
results = transcriber.transcribe_audio(tmp_path)
|
| 128 |
-
progress_bar.progress(40)
|
| 129 |
-
|
| 130 |
-
if not results or not results.get("raw_transcription"):
|
| 131 |
-
st.error("Transcription failed.")
|
| 132 |
-
else:
|
| 133 |
-
raw_text = results["raw_transcription"]
|
| 134 |
-
|
| 135 |
-
status_text.markdown("### ๐ Translating to English...")
|
| 136 |
-
progress_bar.progress(60)
|
| 137 |
-
translated = transcriber.translate_to_malayalam(results)
|
| 138 |
-
ml_text = translated.get("malayalam_translation", "")
|
| 139 |
-
progress_bar.progress(80)
|
| 140 |
-
|
| 141 |
-
status_text.markdown("### ๐ Analyzing content...")
|
| 142 |
-
en_analysis = analyze_text(raw_text, "en")
|
| 143 |
-
ml_analysis = analyze_text(ml_text, "ml")
|
| 144 |
-
comparison = compare_analyses(en_analysis, ml_analysis)
|
| 145 |
-
progress_bar.progress(100)
|
| 146 |
-
|
| 147 |
-
status_text.success("โ
Analysis completed!")
|
| 148 |
-
with col2:
|
| 149 |
-
display_lottie_or_image(analyze_anim, "images/analyze.png", height=200, key="analyze")
|
| 150 |
-
|
| 151 |
-
st.subheader("๐ Results Overview")
|
| 152 |
-
tab1, tab2 = st.tabs(["English Transcription", "Malayalam Translation"])
|
| 153 |
-
with tab1:
|
| 154 |
-
st.markdown(f'<div class="highlight-box">{raw_text}</div>', unsafe_allow_html=True)
|
| 155 |
-
with tab2:
|
| 156 |
-
st.markdown(f'<div class="highlight-box">{ml_text}</div>', unsafe_allow_html=True)
|
| 157 |
-
|
| 158 |
-
st.subheader("๐ Key Metrics")
|
| 159 |
-
col1, col2, col3 = st.columns(3)
|
| 160 |
-
with col1:
|
| 161 |
-
st.markdown('<div class="metric-card"><h3>Sentiment Match</h3><p style="font-size: 2rem;">โ
85%</p></div>', unsafe_allow_html=True)
|
| 162 |
-
with col2:
|
| 163 |
-
st.markdown('<div class="metric-card"><h3>Intent Match</h3><p style="font-size: 2rem;">๐ฏ 78%</p></div>', unsafe_allow_html=True)
|
| 164 |
-
with col3:
|
| 165 |
-
en_avg = sum(x["sentiment_score"] for x in en_analysis) / len(en_analysis) if en_analysis else 0
|
| 166 |
-
ml_avg = sum(x["sentiment_score"] for x in ml_analysis) / len(ml_analysis) if ml_analysis else 0
|
| 167 |
-
lead_score = int(((en_avg + ml_avg) / 2) * 100)
|
| 168 |
-
score_color = "#00ff88" if lead_score >= 70 else "#ffaa00" if lead_score >= 40 else "#ff5555"
|
| 169 |
-
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)
|
| 170 |
-
|
| 171 |
-
with st.expander("๐ Detailed Sentiment Analysis"):
|
| 172 |
-
col1, col2 = st.columns(2)
|
| 173 |
-
with col1:
|
| 174 |
-
st.markdown("#### ๐ฌ๐ง English Analysis")
|
| 175 |
-
print_analysis_summary(en_analysis, "English")
|
| 176 |
-
with col2:
|
| 177 |
-
st.markdown("#### ๐ฎ๐ณ Malayalam Analysis")
|
| 178 |
-
print_analysis_summary(ml_analysis, "Malayalam")
|
| 179 |
-
|
| 180 |
-
st.subheader("๐ฅ Download Results")
|
| 181 |
-
col1, col2, col3 = st.columns(3)
|
| 182 |
-
en_csv = save_analysis_to_csv(en_analysis, "english")
|
| 183 |
-
ml_csv = save_analysis_to_csv(ml_analysis, "malayalam")
|
| 184 |
-
comparison_csv = save_analysis_to_csv(comparison, "comparison")
|
| 185 |
-
|
| 186 |
-
with col1:
|
| 187 |
-
if en_csv and os.path.exists(en_csv):
|
| 188 |
-
with open(en_csv, "rb") as f:
|
| 189 |
-
st.download_button("โฌ๏ธ English Analysis", f.read(), file_name=os.path.basename(en_csv), mime="text/csv")
|
| 190 |
-
with col2:
|
| 191 |
-
if ml_csv and os.path.exists(ml_csv):
|
| 192 |
-
with open(ml_csv, "rb") as f:
|
| 193 |
-
st.download_button("โฌ๏ธ Malayalam Analysis", f.read(), file_name=os.path.basename(ml_csv), mime="text/csv")
|
| 194 |
-
with col3:
|
| 195 |
-
if comparison_csv and os.path.exists(comparison_csv):
|
| 196 |
-
with open(comparison_csv, "rb") as f:
|
| 197 |
-
st.download_button("โฌ๏ธ Comparison Report", f.read(), file_name=os.path.basename(comparison_csv), mime="text/csv")
|
| 198 |
-
|
| 199 |
-
display_lottie_or_image(results_anim, "images/results.png", height=300, key="results")
|
| 200 |
-
|
| 201 |
-
except Exception as e:
|
| 202 |
-
st.error(f"โ Error: {str(e)}")
|
| 203 |
-
st.exception(e)
|
| 204 |
-
finally:
|
| 205 |
-
transcriber.cleanup()
|
| 206 |
-
os.remove(tmp_path)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
from streamlit_app import (
|
| 8 |
+
MalayalamTranscriptionPipeline,
|
| 9 |
+
analyze_text,
|
| 10 |
+
save_analysis_to_csv,
|
| 11 |
+
compare_analyses,
|
| 12 |
+
print_analysis_summary
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Function to load Lottie or fallback image
|
| 16 |
+
def load_lottie(filepath):
|
| 17 |
+
if os.path.exists(filepath):
|
| 18 |
+
with open(filepath, "r") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
def display_lottie_or_image(lottie_data, fallback_image_path, height=200, key=None):
|
| 23 |
+
try:
|
| 24 |
+
from streamlit_lottie import st_lottie
|
| 25 |
+
if lottie_data:
|
| 26 |
+
st_lottie(lottie_data, height=height, key=key)
|
| 27 |
+
return
|
| 28 |
+
except ImportError:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
if os.path.exists(fallback_image_path):
|
| 32 |
+
img = Image.open(fallback_image_path)
|
| 33 |
+
st.image(img, width=height)
|
| 34 |
+
else:
|
| 35 |
+
st.warning("Animation and fallback image not found.")
|
| 36 |
+
|
| 37 |
+
# Load animations or fallback
|
| 38 |
+
upload_anim = load_lottie("animations/upload.json")
|
| 39 |
+
analyze_anim = load_lottie("animations/analyze.json")
|
| 40 |
+
results_anim = load_lottie("animations/results.json")
|
| 41 |
+
|
| 42 |
+
# Page config
|
| 43 |
+
st.set_page_config(
|
| 44 |
+
page_title="Malayalam Audio Analyzer",
|
| 45 |
+
page_icon="๐ค",
|
| 46 |
+
layout="wide",
|
| 47 |
+
initial_sidebar_state="expanded"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Custom CSS
|
| 51 |
+
st.markdown("""
|
| 52 |
+
<style>
|
| 53 |
+
.stProgress > div > div > div > div {
|
| 54 |
+
background-image: linear-gradient(to right, #00ccff, #00ffaa);
|
| 55 |
+
}
|
| 56 |
+
.stButton>button {
|
| 57 |
+
border-radius: 20px;
|
| 58 |
+
font-weight: bold;
|
| 59 |
+
}
|
| 60 |
+
.highlight-box {
|
| 61 |
+
border-radius: 15px;
|
| 62 |
+
padding: 1rem;
|
| 63 |
+
margin: 1rem 0;
|
| 64 |
+
background: rgba(0, 204, 255, 0.1);
|
| 65 |
+
border-left: 5px solid #00ccff;
|
| 66 |
+
}
|
| 67 |
+
.metric-card {
|
| 68 |
+
border-radius: 10px;
|
| 69 |
+
padding: 1.5rem;
|
| 70 |
+
margin: 0.5rem;
|
| 71 |
+
background: rgba(255, 255, 255, 0.1);
|
| 72 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 73 |
+
transition: transform 0.3s;
|
| 74 |
+
}
|
| 75 |
+
.metric-card:hover {
|
| 76 |
+
transform: translateY(-5px);
|
| 77 |
+
}
|
| 78 |
+
</style>
|
| 79 |
+
""", unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
# Title
|
| 82 |
+
st.title("๐๏ธ Malayalam Audio Intelligence Analyzer")
|
| 83 |
+
st.markdown("""
|
| 84 |
+
Upload a Malayalam audio file to extract insights, analyze sentiment and intent, and generate lead scores.
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
# Sidebar info
|
| 88 |
+
with st.sidebar:
|
| 89 |
+
st.header("โน๏ธ About")
|
| 90 |
+
st.markdown("""
|
| 91 |
+
This tool analyzes Malayalam audio to:
|
| 92 |
+
- Transcribe speech to text
|
| 93 |
+
- Translate to English
|
| 94 |
+
- Detect sentiment & intent
|
| 95 |
+
- Calculate lead scores
|
| 96 |
+
""")
|
| 97 |
+
st.header("๐ Supported Formats")
|
| 98 |
+
st.markdown("MP3, WAV, AAC, M4A, FLAC, OGG, WMA")
|
| 99 |
+
|
| 100 |
+
# File Upload
|
| 101 |
+
with st.container():
|
| 102 |
+
col1, col2 = st.columns([3, 1])
|
| 103 |
+
with col1:
|
| 104 |
+
uploaded_file = st.file_uploader("Upload Malayalam audio file", type=[
|
| 105 |
+
"mp3", "wav", "aac", "m4a", "flac", "ogg", "wma"
|
| 106 |
+
])
|
| 107 |
+
with col2:
|
| 108 |
+
display_lottie_or_image(upload_anim, "images/upload.png", height=150, key="upload")
|
| 109 |
+
|
| 110 |
+
if uploaded_file is not None:
|
| 111 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp:
|
| 112 |
+
tmp.write(uploaded_file.read())
|
| 113 |
+
tmp_path = tmp.name
|
| 114 |
+
|
| 115 |
+
transcriber = MalayalamTranscriptionPipeline()
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
with st.container():
|
| 119 |
+
st.subheader("๐ Processing Pipeline")
|
| 120 |
+
col1, col2 = st.columns([3, 1])
|
| 121 |
+
with col1:
|
| 122 |
+
progress_bar = st.progress(0)
|
| 123 |
+
status_text = st.empty()
|
| 124 |
+
|
| 125 |
+
status_text.markdown("### ๐ง Transcribing audio...")
|
| 126 |
+
progress_bar.progress(20)
|
| 127 |
+
results = transcriber.transcribe_audio(tmp_path)
|
| 128 |
+
progress_bar.progress(40)
|
| 129 |
+
|
| 130 |
+
if not results or not results.get("raw_transcription"):
|
| 131 |
+
st.error("Transcription failed.")
|
| 132 |
+
else:
|
| 133 |
+
raw_text = results["raw_transcription"]
|
| 134 |
+
|
| 135 |
+
status_text.markdown("### ๐ Translating to English...")
|
| 136 |
+
progress_bar.progress(60)
|
| 137 |
+
translated = transcriber.translate_to_malayalam(results)
|
| 138 |
+
ml_text = translated.get("malayalam_translation", "")
|
| 139 |
+
progress_bar.progress(80)
|
| 140 |
+
|
| 141 |
+
status_text.markdown("### ๐ Analyzing content...")
|
| 142 |
+
en_analysis = analyze_text(raw_text, "en")
|
| 143 |
+
ml_analysis = analyze_text(ml_text, "ml")
|
| 144 |
+
comparison = compare_analyses(en_analysis, ml_analysis)
|
| 145 |
+
progress_bar.progress(100)
|
| 146 |
+
|
| 147 |
+
status_text.success("โ
Analysis completed!")
|
| 148 |
+
with col2:
|
| 149 |
+
display_lottie_or_image(analyze_anim, "images/analyze.png", height=200, key="analyze")
|
| 150 |
+
|
| 151 |
+
st.subheader("๐ Results Overview")
|
| 152 |
+
tab1, tab2 = st.tabs(["English Transcription", "Malayalam Translation"])
|
| 153 |
+
with tab1:
|
| 154 |
+
st.markdown(f'<div class="highlight-box">{raw_text}</div>', unsafe_allow_html=True)
|
| 155 |
+
with tab2:
|
| 156 |
+
st.markdown(f'<div class="highlight-box">{ml_text}</div>', unsafe_allow_html=True)
|
| 157 |
+
|
| 158 |
+
st.subheader("๐ Key Metrics")
|
| 159 |
+
col1, col2, col3 = st.columns(3)
|
| 160 |
+
with col1:
|
| 161 |
+
st.markdown('<div class="metric-card"><h3>Sentiment Match</h3><p style="font-size: 2rem;">โ
85%</p></div>', unsafe_allow_html=True)
|
| 162 |
+
with col2:
|
| 163 |
+
st.markdown('<div class="metric-card"><h3>Intent Match</h3><p style="font-size: 2rem;">๐ฏ 78%</p></div>', unsafe_allow_html=True)
|
| 164 |
+
with col3:
|
| 165 |
+
en_avg = sum(x["sentiment_score"] for x in en_analysis) / len(en_analysis) if en_analysis else 0
|
| 166 |
+
ml_avg = sum(x["sentiment_score"] for x in ml_analysis) / len(ml_analysis) if ml_analysis else 0
|
| 167 |
+
lead_score = int(((en_avg + ml_avg) / 2) * 100)
|
| 168 |
+
score_color = "#00ff88" if lead_score >= 70 else "#ffaa00" if lead_score >= 40 else "#ff5555"
|
| 169 |
+
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)
|
| 170 |
+
|
| 171 |
+
with st.expander("๐ Detailed Sentiment Analysis"):
|
| 172 |
+
col1, col2 = st.columns(2)
|
| 173 |
+
with col1:
|
| 174 |
+
st.markdown("#### ๐ฌ๐ง English Analysis")
|
| 175 |
+
print_analysis_summary(en_analysis, "English")
|
| 176 |
+
with col2:
|
| 177 |
+
st.markdown("#### ๐ฎ๐ณ Malayalam Analysis")
|
| 178 |
+
print_analysis_summary(ml_analysis, "Malayalam")
|
| 179 |
+
|
| 180 |
+
st.subheader("๐ฅ Download Results")
|
| 181 |
+
col1, col2, col3 = st.columns(3)
|
| 182 |
+
en_csv = save_analysis_to_csv(en_analysis, "english")
|
| 183 |
+
ml_csv = save_analysis_to_csv(ml_analysis, "malayalam")
|
| 184 |
+
comparison_csv = save_analysis_to_csv(comparison, "comparison")
|
| 185 |
+
|
| 186 |
+
with col1:
|
| 187 |
+
if en_csv and os.path.exists(en_csv):
|
| 188 |
+
with open(en_csv, "rb") as f:
|
| 189 |
+
st.download_button("โฌ๏ธ English Analysis", f.read(), file_name=os.path.basename(en_csv), mime="text/csv")
|
| 190 |
+
with col2:
|
| 191 |
+
if ml_csv and os.path.exists(ml_csv):
|
| 192 |
+
with open(ml_csv, "rb") as f:
|
| 193 |
+
st.download_button("โฌ๏ธ Malayalam Analysis", f.read(), file_name=os.path.basename(ml_csv), mime="text/csv")
|
| 194 |
+
with col3:
|
| 195 |
+
if comparison_csv and os.path.exists(comparison_csv):
|
| 196 |
+
with open(comparison_csv, "rb") as f:
|
| 197 |
+
st.download_button("โฌ๏ธ Comparison Report", f.read(), file_name=os.path.basename(comparison_csv), mime="text/csv")
|
| 198 |
+
|
| 199 |
+
display_lottie_or_image(results_anim, "images/results.png", height=300, key="results")
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
st.error(f"โ Error: {str(e)}")
|
| 203 |
+
st.exception(e)
|
| 204 |
+
finally:
|
| 205 |
+
transcriber.cleanup()
|
| 206 |
+
os.remove(tmp_path)
|