Spaces:
Sleeping
Sleeping
| import torch | |
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| from gtts import gTTS | |
| import tempfile | |
| import gradio as gr | |
| # Load the image captioning model | |
| processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
| model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") | |
| def generate_description(image): | |
| """Generates a textual description of the given image using a pre-trained BLIP model.""" | |
| inputs = processor(image, return_tensors="pt").to(model.device) | |
| output = model.generate(**inputs) | |
| description = processor.decode(output[0], skip_special_tokens=True) | |
| return description | |
| def text_to_speech(text): | |
| """Converts text to speech using gTTS and returns the audio file path.""" | |
| tts = gTTS(text=text, lang='en') | |
| temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
| tts.save(temp_audio.name) | |
| return temp_audio.name | |
| def process_image(image): | |
| """Processes the uploaded image to generate description and return audio file.""" | |
| description = generate_description(image) | |
| return description | |
| def get_audio(description): | |
| """Generates the audio file for the given description.""" | |
| return text_to_speech(description) | |
| # Build Gradio Interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Image Description and Audio Transcript App") | |
| gr.Markdown("Upload an image to get an AI-generated description. Click the button to hear the description.") | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil") | |
| text_output = gr.Textbox(label="Generated Description") | |
| generate_btn = gr.Button("Generate Description") | |
| audio_btn = gr.Button("Click here for an audio transcript") | |
| audio_output = gr.Audio() | |
| generate_btn.click(process_image, inputs=[image_input], outputs=[text_output]) | |
| audio_btn.click(get_audio, inputs=[text_output], outputs=[audio_output]) | |
| # Launch the Gradio app | |
| demo.launch() | |