Mayankuttam commited on
Commit
4a7fda3
Β·
verified Β·
1 Parent(s): 32ca2ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +287 -0
app.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tempfile
3
+ import os
4
+ from model_pipeline import run_model_on_video # Assuming this function exists and is correctly implemented
5
+
6
+ # Page setup
7
+ st.set_page_config(
8
+ page_title="AI Cricket Commentary",
9
+ layout="wide",
10
+ initial_sidebar_state="collapsed",
11
+ menu_items={
12
+ 'Get Help': 'https://www.example.com/help',
13
+ 'Report a bug': "https://www.example.com/bug",
14
+ 'About': "# AI Cricket Commentary Generator"
15
+ }
16
+ )
17
+
18
+ # Custom CSS for a professional, dark theme look
19
+ st.markdown("""
20
+ <style>
21
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
22
+
23
+ html, body, .stApp {
24
+ background-color: #0d0e12;
25
+ font-family: 'Inter', sans-serif;
26
+ color: #e0e0e0;
27
+ }
28
+
29
+ .main-title {
30
+ font-size: 2.5rem;
31
+ font-weight: 700;
32
+ color: #e0e0e0;
33
+ text-align: center;
34
+ margin-top: 1rem;
35
+ margin-bottom: 0.2rem;
36
+ display: flex;
37
+ justify-content: center;
38
+ align-items: center;
39
+ gap: 0.75rem;
40
+ }
41
+
42
+ .subtitle {
43
+ font-size: 1.1rem;
44
+ color: #a0a0a0;
45
+ font-weight: 400;
46
+ text-align: center;
47
+ margin-bottom: 2rem;
48
+ }
49
+
50
+ .section-header {
51
+ font-size: 1.5rem;
52
+ font-weight: 600;
53
+ color: #e0e0e0;
54
+ margin-top: 3rem;
55
+ margin-bottom: 1.5rem;
56
+ display: flex;
57
+ align-items: center;
58
+ gap: 0.5rem;
59
+ }
60
+
61
+ /* Main container for the "Try It Yourself" section to fix alignment */
62
+ .main-content-container {
63
+ display: flex;
64
+ justify-content: space-between;
65
+ gap: 2rem;
66
+ margin-bottom: 2rem;
67
+ }
68
+
69
+ /* Custom file uploader styling to match the image */
70
+ .stFileUploader {
71
+ background: #1f2229;
72
+ border: 2px dashed #444a56;
73
+ padding: 1.5rem;
74
+ border-radius: 12px;
75
+ text-align: center;
76
+ cursor: pointer;
77
+ transition: border-color 0.3s ease, background 0.3s ease;
78
+ display: flex;
79
+ flex-direction: column;
80
+ align-items: center;
81
+ justify-content: center;
82
+ }
83
+
84
+ .stFileUploader:hover {
85
+ border-color: #6a6f7b;
86
+ background: #282b33;
87
+ }
88
+
89
+ .stFileUploader [data-testid="stFileUploaderDropzone"] {
90
+ background: transparent !important;
91
+ border: none !important;
92
+ padding: 0 !important;
93
+ }
94
+
95
+ .stFileUploader [data-testid="stFileUploaderFile"] {
96
+ background-color: #2c2e36;
97
+ color: #e0e0e0;
98
+ border-radius: 8px;
99
+ padding: 0.5rem;
100
+ margin-top: 1rem;
101
+ }
102
+
103
+ .stFileUploader [data-testid="stFileUploaderDropzone"] [data-testid="stMarkdownContainer"] {
104
+ display: flex;
105
+ flex-direction: column;
106
+ align-items: center;
107
+ justify-content: center;
108
+ }
109
+
110
+ .stFileUploader [data-testid="stMarkdownContainer"] p:first-child {
111
+ font-size: 1rem;
112
+ font-weight: 600;
113
+ color: #e0e0e0;
114
+ margin-bottom: 0.25rem;
115
+ }
116
+
117
+ .stFileUploader [data-testid="stMarkdownContainer"] p:nth-child(2) {
118
+ font-size: 0.8rem;
119
+ color: #7b7d85;
120
+ }
121
+
122
+ /* Uploaded video display */
123
+ .stVideo {
124
+ border-radius: 12px;
125
+ overflow: hidden;
126
+ box-shadow: 0 4px 12px rgba(0,0,0,0.3);
127
+ }
128
+
129
+ .video-container, .results-box {
130
+ background: #1f2229;
131
+ padding: 1.5rem;
132
+ border-radius: 12px;
133
+ box-shadow: 0 4px 12px rgba(0,0,0,0.3);
134
+ height: 100%;
135
+ border: 1px solid #2c2f37;
136
+ }
137
+
138
+ .results-box h3 {
139
+ margin-top: 0;
140
+ color: #e0e0e0;
141
+ }
142
+
143
+ .results-box .placeholder-text {
144
+ color: #7b7d85;
145
+ text-align: center;
146
+ padding-top: 2rem;
147
+ padding-bottom: 2rem;
148
+ }
149
+
150
+ /* Button styling */
151
+ .stButton>button {
152
+ background: #007bff;
153
+ color: white;
154
+ font-weight: 600;
155
+ border-radius: 8px;
156
+ padding: 0.75rem 1.5rem;
157
+ border: none;
158
+ transition: all 0.3s ease;
159
+ box-shadow: 0 4px 6px rgba(0, 123, 255, 0.2);
160
+ }
161
+
162
+ .stButton>button:hover {
163
+ background: #0056b3;
164
+ transform: translateY(-2px);
165
+ box-shadow: 0 6px 10px rgba(0, 123, 255, 0.3);
166
+ }
167
+
168
+ .stDownloadButton>button {
169
+ background: #444a56;
170
+ color: #e0e0e0;
171
+ font-weight: 600;
172
+ border: none;
173
+ border-radius: 8px;
174
+ padding: 0.75rem 1.5rem;
175
+ transition: all 0.3s ease;
176
+ box-shadow: 0 4px 6px rgba(0,0,0,0.2);
177
+ }
178
+
179
+ .stDownloadButton>button:hover {
180
+ background: #5d616d;
181
+ transform: translateY(-2px);
182
+ box-shadow: 0 6px 10px rgba(0,0,0,0.3);
183
+ }
184
+
185
+ /* Footer styling */
186
+ .footer {
187
+ text-align: center;
188
+ font-size: 0.9rem;
189
+ color: #7b7d85;
190
+ margin-top: 5rem;
191
+ padding-top: 2rem;
192
+ border-top: 1px solid #2c2f37;
193
+ }
194
+ </style>
195
+ """, unsafe_allow_html=True)
196
+
197
+ # --- HEADER ---
198
+ st.markdown('<h1 class="main-title">🏏 AI Cricket Commentary Generator</h1>', unsafe_allow_html=True)
199
+ st.markdown('<p class="subtitle">Revolutionizing Cricket Commentary with AI-driven analysis</p>', unsafe_allow_html=True)
200
+
201
+ # --- TRY IT YOURSELF SECTION ---
202
+ st.markdown('<h2 class="section-header">🎬 Try It Yourself</h2>', unsafe_allow_html=True)
203
+
204
+ # Use st.columns to create a clean two-column layout for upload and commentary
205
+ col_upload, col_display = st.columns(2)
206
+
207
+ with col_upload:
208
+ st.markdown("<h4>Upload Cricket Match Video</h4>", unsafe_allow_html=True)
209
+ video_file = st.file_uploader(
210
+ "Drag and drop file here\nLimit 200MB per file",
211
+ type=["mp4", "mov", "avi", "mpeg4"],
212
+ label_visibility="collapsed"
213
+ )
214
+
215
+ with col_display:
216
+ if video_file:
217
+ st.markdown("<h4>Uploaded Video Preview</h4>", unsafe_allow_html=True)
218
+ st.video(video_file)
219
+ if st.button("πŸš€ Generate Commentary", use_container_width=True):
220
+ with st.spinner("Generating... please wait ⏳"):
221
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
222
+ tmp.write(video_file.read())
223
+ temp_video_path = tmp.name
224
+
225
+ try:
226
+ commentary_text, audio_path, video_output_path = run_model_on_video(temp_video_path)
227
+
228
+ st.success("βœ… Commentary Generated Successfully!")
229
+ st.markdown(f"**πŸ“ AI Commentary Summary:**\n\n{commentary_text}")
230
+ st.audio(audio_path, format="audio/mp3", autoplay=False)
231
+
232
+ col_audio, col_video = st.columns(2)
233
+ with col_audio:
234
+ with open(audio_path, 'rb') as a:
235
+ st.download_button("⬇️ Download Audio", a, file_name="commentary.mp3", use_container_width=True)
236
+ with col_video:
237
+ with open(video_output_path, 'rb') as v:
238
+ st.download_button("⬇️ Download Final Video", v, file_name="final_video.mp4", use_container_width=True)
239
+
240
+ st.video(video_output_path)
241
+
242
+ except Exception as e:
243
+ st.error(f"❌ An error occurred: {e}")
244
+ st.exception(e)
245
+ finally:
246
+ try:
247
+ if 'temp_video_path' in locals() and os.path.exists(temp_video_path):
248
+ os.remove(temp_video_path)
249
+ if 'audio_path' in locals() and os.path.exists(audio_path):
250
+ os.remove(audio_path)
251
+ if 'video_output_path' in locals() and os.path.exists(video_output_path):
252
+ os.remove(video_output_path)
253
+ except PermissionError:
254
+ st.warning("Could not delete temporary files. Please delete them manually if necessary.")
255
+ else:
256
+ st.markdown('<div class="results-box">', unsafe_allow_html=True)
257
+ st.markdown("<h3>AI Generated Commentary</h3>", unsafe_allow_html=True)
258
+ st.markdown('<p class="placeholder-text">Upload a cricket video to see the preview and generate commentary.</p>', unsafe_allow_html=True)
259
+ st.markdown('</div>', unsafe_allow_html=True)
260
+
261
+
262
+ # --- HOW IT WORKS SECTION ---
263
+ st.markdown('<div class="section-header">βš™οΈ How It Works</div>', unsafe_allow_html=True)
264
+ how_cols = st.columns(3)
265
+ steps = [
266
+ ("πŸ“€ Upload Video", "Upload your cricket video file. Our system processes it frame by frame."),
267
+ ("🧠 AI Analysis", "The AI analyzes key events, recognizes shots, and predicts outcomes."),
268
+ ("πŸ—£οΈ Generate Commentary", "High-quality, professional commentary is generated and ready for playback or download.")
269
+ ]
270
+ for col, (title, desc) in zip(how_cols, steps):
271
+ with col:
272
+ st.markdown(f'<div class="results-box"><h4>{title}</h4><p>{desc}</p></div>', unsafe_allow_html=True)
273
+
274
+ # --- KEY FEATURES SECTION ---
275
+ st.markdown('<div class="section-header">⭐ Key Features</div>', unsafe_allow_html=True)
276
+ feature_cols = st.columns(3)
277
+ features = [
278
+ ("🎯 Shot Recognition", "Accurately detects shots like cover drives, sweeps, pulls, and hooks."),
279
+ ("πŸ“Š Outcome Prediction", "Intelligently predicts boundaries, wickets, and other key outcomes."),
280
+ ("πŸŽ™οΈ Broadcast-Quality Commentary", "Generates dynamic commentary using proper cricket terminology and flow."),
281
+ ]
282
+ for col, (title, desc) in zip(feature_cols, features):
283
+ with col:
284
+ st.markdown(f'<div class="results-box"><h5>{title}</h5><p>{desc}</p></div>', unsafe_allow_html=True)
285
+
286
+ # --- FOOTER ---
287
+ st.markdown('<div class="footer">Made with ❀️ by AI Cricket Labs | © 2024</div>', unsafe_allow_html=True)