frankai98 commited on
Commit
e854579
·
verified ·
1 Parent(s): d97169f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -16
app.py CHANGED
@@ -3,10 +3,8 @@ nest_asyncio.apply()
3
 
4
  import streamlit as st
5
  from transformers import pipeline
6
- import torch
7
  from gtts import gTTS
8
  import io
9
- import time
10
  from streamlit.components.v1 import html
11
  import asyncio
12
 
@@ -20,12 +18,12 @@ if 'processed_data' not in st.session_state:
20
  'story': None,
21
  'audio': None
22
  }
23
- if 'timer_frozen' not in st.session_state:
24
- st.session_state.timer_frozen = False
25
  if 'timer_started' not in st.session_state:
26
  st.session_state.timer_started = False
 
 
27
 
28
- # JavaScript timer component
29
  def timer():
30
  return """
31
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
@@ -33,7 +31,6 @@ def timer():
33
  (function() {
34
  var start = Date.now();
35
  var timerElement = document.getElementById('timer');
36
- // Save the interval id globally
37
  window.myTimerInterval = setInterval(function() {
38
  var elapsed = Date.now() - start;
39
  var minutes = Math.floor(elapsed / 60000);
@@ -76,7 +73,6 @@ def text2story(text):
76
  do_sample=True,
77
  temperature=0.7
78
  )[0]["generated_text"]
79
- # Adjust response extraction as needed (this was in your original code)
80
  return response[2]["content"]
81
 
82
  def text2audio(story_text):
@@ -86,15 +82,15 @@ def text2audio(story_text):
86
  audio_io.seek(0)
87
  return {'audio': audio_io, 'sampling_rate': 16000}
88
 
89
- # Only start the timer once (when image is uploaded) if not frozen.
90
- if not st.session_state.timer_started and not st.session_state.timer_frozen:
91
- html(timer(), height=50)
92
- st.session_state.timer_started = True
93
-
94
  # UI components
95
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
96
 
97
  if uploaded_file is not None:
 
 
 
 
 
98
  # Initialize progress containers
99
  status_text = st.empty()
100
  progress_bar = st.progress(0)
@@ -133,9 +129,9 @@ if uploaded_file is not None:
133
  )
134
  progress_bar.progress(100)
135
 
136
- # Final status and optionally change timer color
137
  status_text.success("**✅ Generation complete!**")
138
  html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
 
139
  except Exception as e:
140
  html("<script>document.getElementById('timer').remove();</script>")
141
  status_text.error(f"**❌ Error:** {str(e)}")
@@ -145,15 +141,14 @@ if uploaded_file is not None:
145
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
146
  st.write("**Story:**", st.session_state.processed_data['story'])
147
 
148
- # Audio playback with timer freeze
149
  if st.button("Play Audio of the Story Generated"):
150
  if st.session_state.processed_data.get('audio'):
151
- # Stop the timer by clearing the interval
152
  st.markdown(
153
  "<script>if(window.myTimerInterval){ clearInterval(window.myTimerInterval); }</script>",
154
  unsafe_allow_html=True
155
  )
156
- # Set flag so that timer HTML is not re-rendered
157
  st.session_state.timer_frozen = True
158
  audio_data = st.session_state.processed_data['audio']
159
  st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
 
3
 
4
  import streamlit as st
5
  from transformers import pipeline
 
6
  from gtts import gTTS
7
  import io
 
8
  from streamlit.components.v1 import html
9
  import asyncio
10
 
 
18
  'story': None,
19
  'audio': None
20
  }
 
 
21
  if 'timer_started' not in st.session_state:
22
  st.session_state.timer_started = False
23
+ if 'timer_frozen' not in st.session_state:
24
+ st.session_state.timer_frozen = False
25
 
26
+ # JavaScript timer component (defines window.myTimerInterval for later stopping)
27
  def timer():
28
  return """
29
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
 
31
  (function() {
32
  var start = Date.now();
33
  var timerElement = document.getElementById('timer');
 
34
  window.myTimerInterval = setInterval(function() {
35
  var elapsed = Date.now() - start;
36
  var minutes = Math.floor(elapsed / 60000);
 
73
  do_sample=True,
74
  temperature=0.7
75
  )[0]["generated_text"]
 
76
  return response[2]["content"]
77
 
78
  def text2audio(story_text):
 
82
  audio_io.seek(0)
83
  return {'audio': audio_io, 'sampling_rate': 16000}
84
 
 
 
 
 
 
85
  # UI components
86
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
87
 
88
  if uploaded_file is not None:
89
+ # Only inject the timer once after an image is uploaded
90
+ if not st.session_state.timer_started and not st.session_state.timer_frozen:
91
+ st.session_state.timer_started = True
92
+ html(timer(), height=50)
93
+
94
  # Initialize progress containers
95
  status_text = st.empty()
96
  progress_bar = st.progress(0)
 
129
  )
130
  progress_bar.progress(100)
131
 
 
132
  status_text.success("**✅ Generation complete!**")
133
  html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
134
+
135
  except Exception as e:
136
  html("<script>document.getElementById('timer').remove();</script>")
137
  status_text.error(f"**❌ Error:** {str(e)}")
 
141
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
142
  st.write("**Story:**", st.session_state.processed_data['story'])
143
 
144
+ # Audio playback and timer freeze
145
  if st.button("Play Audio of the Story Generated"):
146
  if st.session_state.processed_data.get('audio'):
147
+ # Inject JS to stop the timer and freeze it
148
  st.markdown(
149
  "<script>if(window.myTimerInterval){ clearInterval(window.myTimerInterval); }</script>",
150
  unsafe_allow_html=True
151
  )
 
152
  st.session_state.timer_frozen = True
153
  audio_data = st.session_state.processed_data['audio']
154
  st.audio(audio_data['audio'].getvalue(), format="audio/mp3")