basharat8763/P07_ImageCaptioning
0
1import torch2import gradio as gr3from PIL import Image4import scipy.io.wavfile as wavfile5 6# Use a pipeline as a high-level helper7from transformers import pipeline8 9caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")10 11narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")12 13 14 15 16def generate_audio(text):17 # Generate the narrated text18 narrated_text = narrator(text)19 20 # Save the audio to a WAV file21 wavfile.write("output.wav", rate=narrated_text["sampling_rate"],22 data=narrated_text["audio"][0])23 # Return the path to the saved audio file24 return "output.wav"25 26 27def caption_my_image(pil_image):28 semantics = caption_image(images=pil_image)[0]['generated_text']29 return generate_audio(semantics)30 31 32demo = gr.Interface(33 fn=caption_my_image,34 inputs=[gr.Image(label="Select Image", type="pil")],35 outputs=[gr.Audio(label="Image Caption")],36 title="Project 07: Image Captioning",37 description="As understood from the title, if not already, this application will caption your image"38)39 40demo.launch()41 