Spaces:
Sleeping
Sleeping
| import subprocess | |
| import sys | |
| import streamlit as st | |
| from PIL import Image | |
| def install_packages(): | |
| required_packages = { | |
| 'streamlit': 'streamlit', | |
| 'transformers': 'transformers', | |
| 'gtts': 'gtts', | |
| 'tensorflow': 'tensorflow', | |
| 'tf-keras': 'tf-keras', # Required for compatibility | |
| 'torch': 'torch' # Sometimes helps with transformer compatibility | |
| } | |
| for package in required_packages.values(): | |
| try: | |
| __import__(package) | |
| except ImportError: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", package, "--upgrade"]) | |
| # Install required packages at the start | |
| install_packages() | |
| # Now import the rest of the libraries | |
| from transformers import BlipProcessor, TFBlipForConditionalGeneration | |
| from gtts import gTTS | |
| import tensorflow as tf | |
| import warnings | |
| # Suppress unnecessary warnings | |
| warnings.filterwarnings('ignore') | |
| # Load the image captioning model | |
| def load_model(): | |
| # Verify TensorFlow is using the correct Keras | |
| import keras | |
| st.write(f"Keras version: {keras.__version__}") | |
| st.write(f"TensorFlow version: {tf.__version__}") | |
| processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
| model = TFBlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") | |
| return processor, model | |
| try: | |
| processor, model = load_model() | |
| except Exception as e: | |
| st.error(f"Failed to load model: {str(e)}") | |
| st.error("Please ensure you have installed tf-keras with: pip install tf-keras") | |
| st.stop() | |
| def generate_caption(image): | |
| inputs = processor(image, return_tensors="tf") | |
| out = model.generate(**inputs) | |
| caption = processor.decode(out[0], skip_special_tokens=True) | |
| return caption | |
| def generate_story(caption): | |
| return f"Once upon a time, {caption}. It was a fascinating sight that sparked imagination and wonder." | |
| def text_to_speech(text): | |
| tts = gTTS(text=text, lang='en') | |
| tts.save("story.mp3") | |
| return "story.mp3" | |
| # Streamlit UI | |
| def main(): | |
| st.title("π AI Storytelling Application") | |
| st.write("Upload an image and let AI generate a story for you!") | |
| with st.expander("βΉοΈ Requirements"): | |
| st.write(""" | |
| - Python 3.7+ | |
| - TensorFlow 2.x | |
| - tf-keras (not Keras 3) | |
| """) | |
| uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Your Uploaded Image", use_column_width=True) | |
| if st.button("β¨ Generate Story"): | |
| with st.spinner("Creating your magical story..."): | |
| try: | |
| # Generate caption | |
| caption = generate_caption(image) | |
| with st.expander("π Image Caption"): | |
| st.write(caption) | |
| # Generate story | |
| story = generate_story(caption) | |
| st.subheader("π Generated Story") | |
| st.write(story) | |
| # Convert to speech | |
| audio_file = text_to_speech(story) | |
| st.subheader("π Audio Version") | |
| st.audio(audio_file, format='audio/mp3') | |
| except Exception as e: | |
| st.error(f"An error occurred: {str(e)}") | |
| st.error("If this is a Keras-related error, try: pip install tf-keras") | |
| if __name__ == "__main__": | |
| main() |