krishna34-aimlcurious's picture
Update app.py
e917311 verified
raw
history blame contribute delete
964 Bytes
#Image to story generator
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
# load BLIP model from Hugging Face
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def generate_story(image):
# get the caption first
inputs = processor(images=image, return_tensors="pt")
out = model.generate(**inputs)
caption = processor.decode(out[0], skip_special_tokens=True)
# turn the caption into a story
story = f"Once upon a time, {caption.lower()} lived happily in a magical world full of adventures."
return story
demo = gr.Interface(
fn=generate_story,
inputs=gr.Image(type="pil"),
outputs="text",
title="🖼️ Image to Story Generator",
description="Upload any image, and I will write you a short story about it!"
)
demo.launch()