Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import streamlit as stfrom PIL import Imageimport easyocrfrom transformers import AutoTokenizer, AutoModelForCausalLM
# Define a function to extract text from an image using easyocrdef extract_text(image): reader = easyocr.Reader(['en']) extracted_text = reader.readtext(image) return ' '.join([result[1] for result in extracted_text])
# Define a function to generate an explanation using the "HuggingFaceH4/zephyr-7b-alpha" modeldef explain_text(text, tokenizer, model): input_ids = tokenizer.encode(text, return_tensors="pt", max_length=256, truncation=True) explanation_ids = model.generate(input_ids, max_length=100, num_return_sequences=1) explanation = tokenizer.decode(explanation_ids[0], skip_special_tokens=True) return explanation
st.title("Text Extraction and Explanation")
# Allow users to upload an imageuploaded_file = st.file_uploader("Upload an image:")
# Load the text generation model and tokenizertokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")model = AutoModelForCausalLM.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
if uploaded_file is not None: # Read the uploaded image image = Image.open(uploaded_file)
# Extract text from the image using easyocr extracted_text = extract_text(image)
# Explain the extracted text using the "HuggingFaceH4/zephyr-7b-alpha" model explanation = explain_text(extracted_text, tokenizer, model)
# Display the extracted text and explanation st.markdown("**Extracted text:**") st.markdown(extracted_text)
st.markdown("**Explanation:**") st.markdown(explanation)
else: st.markdown("Please upload an image to extract text and get an explanation.")app.pyAdded+46