Spaces:
Sleeping
Sleeping
import streamlit as st | |
import io | |
from PIL import Image | |
import easyocr | |
from transformers import pipeline, set_seed | |
# Create a text2text-generation pipeline with the "t5-base" model | |
pipe = pipeline("text2text-generation", model="t5-base") | |
# Initialize the EasyOCR reader for text extraction from images | |
ocr_reader = easyocr.Reader(['en']) | |
st.title("Text-to-Story Generator") | |
uploaded_file = st.file_uploader("Upload an image:") | |
if uploaded_file is not None: | |
# Read the uploaded image | |
image = Image.open(uploaded_file) | |
# Extract text from the image using OCR | |
ocr_results = ocr_reader.readtext(image) | |
extracted_text = " ".join([res[1] for res in ocr_results]) | |
st.markdown("**Extracted text:**") | |
st.markdown(extracted_text) | |
if extracted_text: | |
# Use the pipeline to generate a story based on the extracted text | |
story_prompt = f"Once upon a time, in a world where '{extracted_text}'," | |
set_seed(42) # Set a seed for reproducibility | |
story = pipe(story_prompt, max_length=300, do_sample=True)[0]["generated_text"] | |
st.markdown("**Story:**") | |
st.markdown(story) | |
else: | |
st.warning("No text extracted from the image.") | |
else: | |
st.markdown("Please upload an image to extract text and generate a story.") | |