frankai98 commited on
Commit
7bdb95c
·
verified ·
1 Parent(s): e1a461b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -42
app.py CHANGED
@@ -22,26 +22,36 @@ if 'processed_data' not in st.session_state:
22
  }
23
 
24
 
25
- # Timer component with direct DOM manipulation
26
  def timer():
27
  return """
28
- <div id="streamlit-timer" style="
29
- position: fixed;
30
- top: 15px;
31
- right: 15px;
32
- padding: 10px 20px;
33
- background: #ffffff;
34
- border-radius: 25px;
35
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
36
- z-index: 999999;
37
- border: 1px solid #e0e0e0;
38
- font-family: Arial, sans-serif;
39
- font-size: 16px;
40
- color: #333;
41
- ">
42
- ⏱️ <span id="timer-display">00:00</span>
43
- </div>
44
- <script src="https://gist.githubusercontent.com/streamlit-resources/1837910cd9e3d5f8b7e4a/raw/timer.js"></script>
 
 
 
 
 
 
 
 
 
 
45
  """
46
 
47
 
@@ -92,14 +102,9 @@ if uploaded_file is not None:
92
  status_text = st.empty()
93
  progress_bar = st.progress(0)
94
 
95
- # Inject timer HTML
96
- st.markdown("""
97
- <div id="timer-container"></div>
98
- """, unsafe_allow_html=True)
99
 
100
- # Load timer system
101
- html(timer(), height=0, width=0)
102
-
103
 
104
  try:
105
  # Save uploaded file
@@ -137,15 +142,7 @@ if uploaded_file is not None:
137
 
138
  # Final status and stop timer
139
  status_text.success("**✅ Generation complete!**")
140
- html("""
141
- <script>
142
- if (window.timerInterval) {
143
- clearInterval(window.timerInterval);
144
- document.getElementById('streamlit-timer').style.background = '#e8f5e9';
145
- document.getElementById('streamlit-timer').style.color = '#2e7d32';
146
- }
147
- </script>
148
- """)
149
 
150
  # Show results
151
  # st.subheader("Results")
@@ -153,18 +150,16 @@ if uploaded_file is not None:
153
  st.write("**Story:**", st.session_state.processed_data['story'])
154
 
155
  except Exception as e:
156
- html("""
157
- <script>
158
- if (window.timerInterval) {
159
- clearInterval(window.timerInterval);
160
- document.getElementById('streamlit-timer').remove();
161
- }
162
- </script>
163
- """)
164
  status_text.error(f"**❌ Error:** {str(e)}")
165
  progress_bar.empty()
166
  raise e
167
 
 
 
 
 
 
168
  # Audio playback
169
  if st.button("Play Audio of the Story Generated"):
170
  if st.session_state.processed_data.get('audio'):
 
22
  }
23
 
24
 
25
+ # JavaScript timer component
26
  def timer():
27
  return """
28
+ <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
29
+ <script>
30
+ function updateTimer() {
31
+ var start = Date.now();
32
+ var timerElement = document.getElementById('timer');
33
+
34
+ var interval = 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
+ // Cleanup when component is removed
44
+ return function() {
45
+ clearInterval(interval);
46
+ }
47
+ }
48
+ var cleanup = updateTimer();
49
+
50
+ // Handle Streamlit's component cleanup
51
+ window.addEventListener('beforeunload', function() {
52
+ cleanup();
53
+ });
54
+ </script>
55
  """
56
 
57
 
 
102
  status_text = st.empty()
103
  progress_bar = st.progress(0)
104
 
105
+ # Start JavaScript timer
106
+ html(timer(), height=50)
 
 
107
 
 
 
 
108
 
109
  try:
110
  # Save uploaded file
 
142
 
143
  # Final status and stop timer
144
  status_text.success("**✅ Generation complete!**")
145
+ html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
 
 
 
 
 
 
 
 
146
 
147
  # Show results
148
  # st.subheader("Results")
 
150
  st.write("**Story:**", st.session_state.processed_data['story'])
151
 
152
  except Exception as e:
153
+ html("<script>document.getElementById('timer').remove();</script>")
 
 
 
 
 
 
 
154
  status_text.error(f"**❌ Error:** {str(e)}")
155
  progress_bar.empty()
156
  raise e
157
 
158
+
159
+ finally:
160
+ pass # Timer cleanup handled by JavaScript
161
+
162
+
163
  # Audio playback
164
  if st.button("Play Audio of the Story Generated"):
165
  if st.session_state.processed_data.get('audio'):