frankai98 commited on
Commit
ef13d6a
·
verified ·
1 Parent(s): 6538314

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -58
app.py CHANGED
@@ -21,53 +21,64 @@ if 'processed_data' not in st.session_state:
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.cssText = `
39
  font-size: 16px;
40
  color: #666;
41
- margin: 10px 0;
42
- padding: 8px;
43
  background: #f0f2f6;
44
- border-radius: 4px;
45
- position: sticky;
46
- top: 0;
47
- z-index: 999;
48
- `;
49
- timerDiv.innerHTML = '⏱️ Elapsed: 00:00';
 
 
 
 
 
 
50
 
51
- // Insert timer just below the header
52
- var header = window.parent.document.querySelector('[data-testid="stHeader"]');
53
- if (header) {
54
- header.insertAdjacentElement('afterend', timerDiv);
55
- } else {
56
- window.parent.document.body.prepend(timerDiv);
 
 
 
57
  }
58
 
59
- var start = Date.now();
60
- var timerInterval = setInterval(function() {
61
- var elapsed = Date.now() - start;
62
- var minutes = Math.floor(elapsed / 60000);
63
- var seconds = Math.floor((elapsed % 60000) / 1000);
64
- timerDiv.innerHTML = '⏱️ Elapsed: ' +
65
- (minutes < 10 ? '0' : '') + minutes + ':' +
66
- (seconds < 10 ? '0' : '') + seconds;
67
- }, 1000);
68
 
69
- // Store the interval ID in the parent window
70
- window.parent.timerInterval = timerInterval;
 
 
 
 
 
 
 
 
 
 
 
71
  </script>
72
  """
73
 
@@ -112,14 +123,14 @@ def text2audio(story_text):
112
 
113
  # UI components
114
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
115
-
116
  if uploaded_file is not None:
117
  # Initialize progress containers
118
  status_text = st.empty()
119
  progress_bar = st.progress(0)
120
 
121
  # Start JavaScript timer
122
- html(timer(), height=50)
123
 
124
  try:
125
  # Save uploaded file
@@ -134,7 +145,7 @@ if uploaded_file is not None:
134
  st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
135
 
136
  # Stage 1: Image to Text
137
- status_text.markdown("**🖼️ Analyzing image...**")
138
  progress_bar.progress(0)
139
  st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
140
  progress_bar.progress(33)
@@ -157,15 +168,7 @@ if uploaded_file is not None:
157
 
158
  # Final status and stop timer
159
  status_text.success("**✅ Generation complete!**")
160
- html("""
161
- <script>
162
- clearInterval(window.parent.timerInterval);
163
- var timerDiv = window.parent.document.getElementById('parent-timer');
164
- if (timerDiv) {
165
- timerDiv.style.color = '#00cc00';
166
- }
167
- </script>
168
- """)
169
 
170
  # Show results
171
  # st.subheader("Results")
@@ -173,15 +176,7 @@ if uploaded_file is not None:
173
  st.write("**Story:**", st.session_state.processed_data['story'])
174
 
175
  except Exception as e:
176
- html("""
177
- <script>
178
- clearInterval(window.parent.timerInterval);
179
- var timerDiv = window.parent.document.getElementById('parent-timer');
180
- if (timerDiv) {
181
- timerDiv.remove();
182
- }
183
- </script>
184
- """)
185
  status_text.error(f"**❌ Error:** {str(e)}")
186
  progress_bar.empty()
187
  raise e
 
21
  'audio': None
22
  }
23
 
24
+
25
  # Modified JavaScript timer component
26
  def timer():
27
  return """
28
+ <div id="main-timer" style="
29
+ position: fixed;
30
+ top: 20px;
31
+ right: 20px;
 
 
 
 
 
 
 
 
32
  font-size: 16px;
33
  color: #666;
34
+ padding: 8px 15px;
 
35
  background: #f0f2f6;
36
+ border-radius: 20px;
37
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
38
+ z-index: 1000000;
39
+ border: 1px solid #ddd;
40
+ ">
41
+ ⏱️ <span id="time-display">00:00</span>
42
+ </div>
43
+ <script>
44
+ // Timer system
45
+ let shouldRun = true;
46
+ const startTime = Date.now();
47
+ const timerElement = document.getElementById('time-display');
48
 
49
+ function updateTimer() {
50
+ if (!shouldRun) return;
51
+
52
+ const elapsed = Math.floor((Date.now() - startTime) / 1000);
53
+ const minutes = Math.floor(elapsed / 60).toString().padStart(2, '0');
54
+ const seconds = (elapsed % 60).toString().padStart(2, '0');
55
+
56
+ timerElement.textContent = `${minutes}:${seconds}`;
57
+ requestAnimationFrame(updateTimer);
58
  }
59
 
60
+ // Store control in parent window
61
+ window.parent.timerControls = {
62
+ stop: () => {
63
+ shouldRun = false;
64
+ document.getElementById('main-timer').style.backgroundColor = '#e6ffe6';
65
+ document.getElementById('main-timer').style.borderColor = '#99ff99';
66
+ }
67
+ };
 
68
 
69
+ // Start updates
70
+ updateTimer();
71
+ </script>
72
+ """
73
+
74
+ # Modified stop command
75
+ def stop_timer():
76
+ return """
77
+ <script>
78
+ // Safely stop timer if exists
79
+ if (window.parent.timerControls) {
80
+ window.parent.timerControls.stop();
81
+ }
82
  </script>
83
  """
84
 
 
123
 
124
  # UI components
125
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
126
+
127
  if uploaded_file is not None:
128
  # Initialize progress containers
129
  status_text = st.empty()
130
  progress_bar = st.progress(0)
131
 
132
  # Start JavaScript timer
133
+ html(timer(), height=0)
134
 
135
  try:
136
  # Save uploaded file
 
145
  st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
146
 
147
  # Stage 1: Image to Text
148
+ status_text.markdown("**🖼️ Generating caption...**")
149
  progress_bar.progress(0)
150
  st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
151
  progress_bar.progress(33)
 
168
 
169
  # Final status and stop timer
170
  status_text.success("**✅ Generation complete!**")
171
+ html(stop_timer())
 
 
 
 
 
 
 
 
172
 
173
  # Show results
174
  # st.subheader("Results")
 
176
  st.write("**Story:**", st.session_state.processed_data['story'])
177
 
178
  except Exception as e:
179
+ html(stop_timer())
 
 
 
 
 
 
 
 
180
  status_text.error(f"**❌ Error:** {str(e)}")
181
  progress_bar.empty()
182
  raise e