namuisam commited on
Commit
ade46e5
·
verified ·
1 Parent(s): fb4efe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
3
 
4
  # Function definitions
5
  def img2text(url):
@@ -28,55 +29,50 @@ def main():
28
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
29
  st.header("Turn Your Image to Audio Story")
30
 
31
- # File uploader
32
  uploaded_file = st.file_uploader("Select an Image...")
33
 
34
  if uploaded_file is not None:
35
- # Save the uploaded file locally.
36
  bytes_data = uploaded_file.getvalue()
37
- with open(uploaded_file.name, "wb") as file:
38
- file.write(bytes_data)
39
- st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
40
 
41
- # Initialize session state if not present.
42
- if "scenario" not in st.session_state:
43
  st.session_state.scenario = None
44
- if "story" not in st.session_state:
45
  st.session_state.story = None
46
- if "audio_data" not in st.session_state:
47
  st.session_state.audio_data = None
48
-
 
 
 
 
 
 
49
  # Stage 1: Image to Text
50
  if st.session_state.scenario is None:
51
  st.text("Processing img2text...")
52
  st.session_state.scenario = img2text(uploaded_file.name)
53
  st.write(st.session_state.scenario)
54
 
55
- # Stage 2: Text to Story (only if not generated or after regeneration)
56
  if st.session_state.story is None:
57
  st.text("Generating a story...")
58
  st.session_state.story = text2story(st.session_state.scenario)
59
- st.session_state.audio_data = text2audio(st.session_state.story)
60
  st.write(st.session_state.story)
61
 
62
- # Two buttons: "Play Audio" and "Story regeneration"
63
- col1, col2 = st.columns(2)
64
-
65
- with col1:
66
- if st.button("Play Audio"):
67
- st.audio(
68
- st.session_state.audio_data["audio"],
69
- format="audio/wav",
70
- start_time=0,
71
- sample_rate=st.session_state.audio_data["sampling_rate"]
72
- )
73
 
74
- with col2:
75
- if st.button("Story regeneration"):
76
- st.session_state.story = text2story(st.session_state.scenario)
77
- st.session_state.audio_data = text2audio(st.session_state.story)
78
- st.write("New Story:")
79
- st.write(st.session_state.story)
 
 
80
 
81
  if __name__ == "__main__":
82
  main()
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import hashlib
4
 
5
  # Function definitions
6
  def img2text(url):
 
29
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
30
  st.header("Turn Your Image to Audio Story")
31
 
 
32
  uploaded_file = st.file_uploader("Select an Image...")
33
 
34
  if uploaded_file is not None:
35
+ # Get file bytes and compute a hash
36
  bytes_data = uploaded_file.getvalue()
37
+ file_hash = hashlib.sha256(bytes_data).hexdigest()
 
 
38
 
39
+ # Reset session state only if the file content has changed
40
+ if ("last_uploaded_hash" not in st.session_state) or (st.session_state.last_uploaded_hash != file_hash):
41
  st.session_state.scenario = None
 
42
  st.session_state.story = None
 
43
  st.session_state.audio_data = None
44
+ st.session_state.last_uploaded_hash = file_hash
45
+
46
+ # Save the uploaded file locally.
47
+ with open(uploaded_file.name, "wb") as file:
48
+ file.write(bytes_data)
49
+ st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
50
+
51
  # Stage 1: Image to Text
52
  if st.session_state.scenario is None:
53
  st.text("Processing img2text...")
54
  st.session_state.scenario = img2text(uploaded_file.name)
55
  st.write(st.session_state.scenario)
56
 
57
+ # Stage 2: Text to Story
58
  if st.session_state.story is None:
59
  st.text("Generating a story...")
60
  st.session_state.story = text2story(st.session_state.scenario)
 
61
  st.write(st.session_state.story)
62
 
63
+ # Stage 3: Story to Audio data
64
+ if st.session_state.audio_data is None:
65
+ st.text("Generating audio data...")
66
+ st.session_state.audio_data = text2audio(st.session_state.story)
 
 
 
 
 
 
 
67
 
68
+ # Play Audio button – uses stored audio_data.
69
+ if st.button("Play Audio"):
70
+ st.audio(
71
+ st.session_state.audio_data["audio"],
72
+ format="audio/wav",
73
+ start_time=0,
74
+ sample_rate=st.session_state.audio_data["sampling_rate"]
75
+ )
76
 
77
  if __name__ == "__main__":
78
  main()