frankai98 commited on
Commit
27983d9
·
verified ·
1 Parent(s): e8b9998

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -21,37 +21,38 @@ if 'processed_data' not in st.session_state:
21
  'audio': None
22
  }
23
 
24
- # JavaScript timer component
25
  def timer():
26
  return """
27
- <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
28
  <script>
29
- var timerInterval;
30
- function updateTimer() {
31
- var start = Date.now();
32
- var timerElement = document.getElementById('timer');
33
-
34
- timerInterval = setInterval(function() {
35
- var elapsed = Date.now() - start;
36
- var minutes = Math.floor(elapsed / 60000);
37
- var seconds = Math.floor((elapsed % 60000) / 1000);
38
- timerElement.innerHTML = '⏱️ Elapsed: ' +
39
- (minutes < 10 ? '0' : '') + minutes + ':' +
40
- (seconds < 10 ? '0' : '') + seconds;
41
- }, 1000);
42
  }
43
- updateTimer();
44
 
45
- // Listen for custom stop event
46
- document.addEventListener('stopTimer', function() {
47
- clearInterval(timerInterval);
48
- document.getElementById('timer').style.color = '#00cc00';
49
- });
 
 
 
50
 
51
- // Cleanup on error or page unload
52
- window.addEventListener('beforeunload', function() {
53
- clearInterval(timerInterval);
54
- });
 
 
 
 
 
 
 
 
55
  </script>
56
  """
57
 
@@ -143,7 +144,11 @@ if uploaded_file is not None:
143
  status_text.success("**✅ Generation complete!**")
144
  html("""
145
  <script>
146
- document.dispatchEvent(new Event('stopTimer'));
 
 
 
 
147
  </script>
148
  """)
149
 
@@ -155,8 +160,11 @@ if uploaded_file is not None:
155
  except Exception as e:
156
  html("""
157
  <script>
158
- document.dispatchEvent(new Event('stopTimer'));
159
- document.getElementById('timer').remove();
 
 
 
160
  </script>
161
  """)
162
  status_text.error(f"**❌ Error:** {str(e)}")
 
21
  'audio': None
22
  }
23
 
24
+ # Modified JavaScript timer component
25
  def timer():
26
  return """
 
27
  <script>
28
+ // Remove existing timer if present
29
+ var existingTimer = window.parent.document.getElementById('parent-timer');
30
+ if (existingTimer) {
31
+ existingTimer.remove();
32
+ clearInterval(window.parent.timerInterval);
 
 
 
 
 
 
 
 
33
  }
 
34
 
35
+ // Create new timer element in parent document
36
+ var timerDiv = window.parent.document.createElement('div');
37
+ timerDiv.id = 'parent-timer';
38
+ timerDiv.style.fontSize = '16px';
39
+ timerDiv.style.color = '#666';
40
+ timerDiv.style.marginBottom = '10px';
41
+ timerDiv.innerHTML = '⏱️ Elapsed: 00:00';
42
+ window.parent.document.body.appendChild(timerDiv);
43
 
44
+ var start = Date.now();
45
+ var timerInterval = setInterval(function() {
46
+ var elapsed = Date.now() - start;
47
+ var minutes = Math.floor(elapsed / 60000);
48
+ var seconds = Math.floor((elapsed % 60000) / 1000);
49
+ timerDiv.innerHTML = '⏱️ Elapsed: ' +
50
+ (minutes < 10 ? '0' : '') + minutes + ':' +
51
+ (seconds < 10 ? '0' : '') + seconds;
52
+ }, 1000);
53
+
54
+ // Store the interval ID in the parent window
55
+ window.parent.timerInterval = timerInterval;
56
  </script>
57
  """
58
 
 
144
  status_text.success("**✅ Generation complete!**")
145
  html("""
146
  <script>
147
+ clearInterval(window.parent.timerInterval);
148
+ var timerDiv = window.parent.document.getElementById('parent-timer');
149
+ if (timerDiv) {
150
+ timerDiv.style.color = '#00cc00';
151
+ }
152
  </script>
153
  """)
154
 
 
160
  except Exception as e:
161
  html("""
162
  <script>
163
+ clearInterval(window.parent.timerInterval);
164
+ var timerDiv = window.parent.document.getElementById('parent-timer');
165
+ if (timerDiv) {
166
+ timerDiv.remove();
167
+ }
168
  </script>
169
  """)
170
  status_text.error(f"**❌ Error:** {str(e)}")