|
|
|
import gradio as gr |
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
from PIL import Image |
|
|
|
|
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") |
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") |
|
|
|
def generate_story(image): |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
out = model.generate(**inputs) |
|
caption = processor.decode(out[0], skip_special_tokens=True) |
|
|
|
|
|
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() |