File size: 1,191 Bytes
bec6716
45c4e80
 
 
bec6716
 
bcddb2d
 
bec6716
45c4e80
 
 
bcddb2d
bec6716
45c4e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcddb2d
bec6716
45c4e80
bec6716
45c4e80
 
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
import streamlit as st
import io
from PIL import Image
import easyocr
from transformers import pipeline

# Create a text2text-generation pipeline with the "google/flan-t5-base" model
pipe = pipeline("text2text-generation", model="google/flan-t5-base")

# Initialize the EasyOCR reader for text extraction from images
ocr_reader = easyocr.Reader(['en'])

st.title("Text Explanation Model")

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 concise explanation
        explanation = pipe(extracted_text, max_length=30, do_sample=True)[0]["generated_text"]
        st.markdown("**Explanation (5-6 lines):**")
        st.markdown(explanation)
    else:
        st.warning("No text extracted from the image.")

else:
    st.markdown("Please upload an image to extract text and generate an explanation.")