File size: 1,290 Bytes
bec6716
45c4e80
 
 
82597df
bec6716
b3b15cc
63d09a4
bec6716
45c4e80
 
 
b3b15cc
bec6716
45c4e80
 
 
 
 
 
 
 
 
 
 
 
 
b3b15cc
82597df
 
b3b15cc
 
 
bec6716
45c4e80
bec6716
45c4e80
b3b15cc
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
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.")