Spaces:
Sleeping
Sleeping
File size: 1,509 Bytes
8533608 9322a5c 8987036 9322a5c 4ef111f 8533608 4ef111f 8533608 4ef111f 8533608 4ef111f 8533608 f4f24b0 8533608 f4f24b0 8533608 f4f24b0 4ef111f f4f24b0 8533608 f4f24b0 4ef111f f4f24b0 4ef111f 8533608 4ef111f |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# import part
import streamlit as st
from PIL import Image
import time
from transformers import pipeline
# Load models (once globally)
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
story_gen = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
# function part
def generate_image_caption(image):
"""Generates a caption for the given image using a pre-trained model."""
result = img2caption(image)
return result[0]['generated_text']
def text2story(text):
"""Generates a story from the given caption using a story generation model."""
story_text = story_gen(text)[0]['generated_text']
return story_text
# main part
# App title
st.title("Assignment")
# Write some text
st.write("Image to Story")
# File uploader for image
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
# Display image with spinner
if uploaded_image is not None:
with st.spinner("Processing..."):
time.sleep(1) # Simulate a delay
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Generate caption
caption = generate_image_caption(uploaded_image)
st.write(f"**Generated Caption:** {caption}")
# Button to generate story
if st.button("Generate Story from Caption"):
story = text2story(caption)
st.markdown("**Generated Story:**")
st.write(story)
|