File size: 964 Bytes
59a7d5d e657115 59a7d5d e917311 59a7d5d e657115 59a7d5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#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() |