Lindalynn commited on
Commit
520e7ca
ยท
verified ยท
1 Parent(s): 6e529e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -8,7 +8,9 @@ def install_packages():
8
  'streamlit': 'streamlit',
9
  'transformers': 'transformers',
10
  'gtts': 'gtts',
11
- 'tensorflow': 'tensorflow'
 
 
12
  }
13
 
14
  for package in required_packages.values():
@@ -24,15 +26,29 @@ install_packages()
24
  from transformers import BlipProcessor, TFBlipForConditionalGeneration
25
  from gtts import gTTS
26
  import tensorflow as tf
 
 
 
 
27
 
28
  # Load the image captioning model
29
  @st.cache_resource
30
  def load_model():
 
 
 
 
 
31
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
32
  model = TFBlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
33
  return processor, model
34
 
35
- processor, model = load_model()
 
 
 
 
 
36
 
37
  def generate_caption(image):
38
  inputs = processor(image, return_tensors="tf")
@@ -50,8 +66,15 @@ def text_to_speech(text):
50
 
51
  # Streamlit UI
52
  def main():
53
- st.title("Storytelling Application")
54
  st.write("Upload an image and let AI generate a story for you!")
 
 
 
 
 
 
 
55
 
56
  uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
57
 
@@ -59,26 +82,27 @@ def main():
59
  image = Image.open(uploaded_file)
60
  st.image(image, caption="Your Uploaded Image", use_column_width=True)
61
 
62
- if st.button("Generate Story"):
63
- with st.spinner("Creating your story..."):
64
  try:
65
  # Generate caption
66
  caption = generate_caption(image)
67
- st.subheader("Image Caption:")
68
- st.write(caption)
69
 
70
  # Generate story
71
  story = generate_story(caption)
72
- st.subheader("Generated Story:")
73
  st.write(story)
74
 
75
  # Convert to speech
76
  audio_file = text_to_speech(story)
77
- st.subheader("Audio Version:")
78
  st.audio(audio_file, format='audio/mp3')
79
 
80
  except Exception as e:
81
  st.error(f"An error occurred: {str(e)}")
 
82
 
83
  if __name__ == "__main__":
84
  main()
 
8
  'streamlit': 'streamlit',
9
  'transformers': 'transformers',
10
  'gtts': 'gtts',
11
+ 'tensorflow': 'tensorflow',
12
+ 'tf-keras': 'tf-keras', # Required for compatibility
13
+ 'torch': 'torch' # Sometimes helps with transformer compatibility
14
  }
15
 
16
  for package in required_packages.values():
 
26
  from transformers import BlipProcessor, TFBlipForConditionalGeneration
27
  from gtts import gTTS
28
  import tensorflow as tf
29
+ import warnings
30
+
31
+ # Suppress unnecessary warnings
32
+ warnings.filterwarnings('ignore')
33
 
34
  # Load the image captioning model
35
  @st.cache_resource
36
  def load_model():
37
+ # Verify TensorFlow is using the correct Keras
38
+ import keras
39
+ st.write(f"Keras version: {keras.__version__}")
40
+ st.write(f"TensorFlow version: {tf.__version__}")
41
+
42
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
43
  model = TFBlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
44
  return processor, model
45
 
46
+ try:
47
+ processor, model = load_model()
48
+ except Exception as e:
49
+ st.error(f"Failed to load model: {str(e)}")
50
+ st.error("Please ensure you have installed tf-keras with: pip install tf-keras")
51
+ st.stop()
52
 
53
  def generate_caption(image):
54
  inputs = processor(image, return_tensors="tf")
 
66
 
67
  # Streamlit UI
68
  def main():
69
+ st.title("๐Ÿ“– AI Storytelling Application")
70
  st.write("Upload an image and let AI generate a story for you!")
71
+
72
+ with st.expander("โ„น๏ธ Requirements"):
73
+ st.write("""
74
+ - Python 3.7+
75
+ - TensorFlow 2.x
76
+ - tf-keras (not Keras 3)
77
+ """)
78
 
79
  uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
80
 
 
82
  image = Image.open(uploaded_file)
83
  st.image(image, caption="Your Uploaded Image", use_column_width=True)
84
 
85
+ if st.button("โœจ Generate Story"):
86
+ with st.spinner("Creating your magical story..."):
87
  try:
88
  # Generate caption
89
  caption = generate_caption(image)
90
+ with st.expander("๐Ÿ” Image Caption"):
91
+ st.write(caption)
92
 
93
  # Generate story
94
  story = generate_story(caption)
95
+ st.subheader("๐Ÿ“– Generated Story")
96
  st.write(story)
97
 
98
  # Convert to speech
99
  audio_file = text_to_speech(story)
100
+ st.subheader("๐Ÿ”Š Audio Version")
101
  st.audio(audio_file, format='audio/mp3')
102
 
103
  except Exception as e:
104
  st.error(f"An error occurred: {str(e)}")
105
+ st.error("If this is a Keras-related error, try: pip install tf-keras")
106
 
107
  if __name__ == "__main__":
108
  main()