import streamlit as st
import tempfile
import os
from model_pipeline import run_model_on_video # Assuming this function exists and is correctly implemented
# Page setup
st.set_page_config(
page_title="AI Cricket Commentary",
layout="wide",
initial_sidebar_state="collapsed",
menu_items={
'Get Help': 'https://www.example.com/help',
'Report a bug': "https://www.example.com/bug",
'About': "# AI Cricket Commentary Generator"
}
)
# Custom CSS for a professional, dark theme look
st.markdown("""
""", unsafe_allow_html=True)
# --- HEADER ---
st.markdown('
🏏 AI Cricket Commentary Generator
', unsafe_allow_html=True)
st.markdown('Revolutionizing Cricket Commentary with AI-driven analysis
', unsafe_allow_html=True)
# --- TRY IT YOURSELF SECTION ---
st.markdown('', unsafe_allow_html=True)
# Use st.columns to create a clean two-column layout for upload and commentary
col_upload, col_display = st.columns(2)
with col_upload:
st.markdown("Upload Cricket Match Video
", unsafe_allow_html=True)
video_file = st.file_uploader(
"Drag and drop file here\nLimit 200MB per file",
type=["mp4", "mov", "avi", "mpeg4"],
label_visibility="collapsed"
)
with col_display:
if video_file:
st.markdown("Uploaded Video Preview
", unsafe_allow_html=True)
st.video(video_file)
if st.button("🚀 Generate Commentary", use_container_width=True):
with st.spinner("Generating... please wait ⏳"):
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
tmp.write(video_file.read())
temp_video_path = tmp.name
try:
commentary_text, audio_path, video_output_path = run_model_on_video(temp_video_path)
st.success("✅ Commentary Generated Successfully!")
st.markdown(f"**📝 AI Commentary Summary:**\n\n{commentary_text}")
st.audio(audio_path, format="audio/mp3", autoplay=False)
col_audio, col_video = st.columns(2)
with col_audio:
with open(audio_path, 'rb') as a:
st.download_button("⬇️ Download Audio", a, file_name="commentary.mp3", use_container_width=True)
with col_video:
with open(video_output_path, 'rb') as v:
st.download_button("⬇️ Download Final Video", v, file_name="final_video.mp4", use_container_width=True)
st.video(video_output_path)
except Exception as e:
st.error(f"❌ An error occurred: {e}")
st.exception(e)
finally:
try:
if 'temp_video_path' in locals() and os.path.exists(temp_video_path):
os.remove(temp_video_path)
if 'audio_path' in locals() and os.path.exists(audio_path):
os.remove(audio_path)
if 'video_output_path' in locals() and os.path.exists(video_output_path):
os.remove(video_output_path)
except PermissionError:
st.warning("Could not delete temporary files. Please delete them manually if necessary.")
else:
st.markdown('', unsafe_allow_html=True)
st.markdown("
AI Generated Commentary
", unsafe_allow_html=True)
st.markdown('
Upload a cricket video to see the preview and generate commentary.
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
# --- HOW IT WORKS SECTION ---
st.markdown('', unsafe_allow_html=True)
how_cols = st.columns(3)
steps = [
("📤 Upload Video", "Upload your cricket video file. Our system processes it frame by frame."),
("🧠 AI Analysis", "The AI analyzes key events, recognizes shots, and predicts outcomes."),
("🗣️ Generate Commentary", "High-quality, professional commentary is generated and ready for playback or download.")
]
for col, (title, desc) in zip(how_cols, steps):
with col:
st.markdown(f'', unsafe_allow_html=True)
# --- KEY FEATURES SECTION ---
st.markdown('', unsafe_allow_html=True)
feature_cols = st.columns(3)
features = [
("🎯 Shot Recognition", "Accurately detects shots like cover drives, sweeps, pulls, and hooks."),
("📊 Outcome Prediction", "Intelligently predicts boundaries, wickets, and other key outcomes."),
("🎙️ Broadcast-Quality Commentary", "Generates dynamic commentary using proper cricket terminology and flow."),
]
for col, (title, desc) in zip(feature_cols, features):
with col:
st.markdown(f'', unsafe_allow_html=True)
# --- FOOTER ---
st.markdown('', unsafe_allow_html=True)