Text2Story / app.py
mkoot007's picture
Update app.py
b3b15cc
raw
history blame
No virus
1.23 kB
import streamlit as st
import io
from PIL import Image
import easyocr
from transformers import pipeline
# 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"Create a story based on the following text: '{extracted_text}'"
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.")