CoolFace
Apppublic

Farxand/Decoder

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py51 linesDownload Raw Back to root
1import streamlit as st2from PIL import Image3import pytesseract4from transformers import pipeline5 6# Configure Tesseract (optional, specify path if necessary)7# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'8 9# Load the Question Answering model10qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")11 12# Streamlit App Layout13st.title("Question Paper Scanner and Answer Generator")14 15# Step 1: Upload an Image16st.header("Step 1: Upload the Question Paper")17uploaded_file = st.file_uploader("Upload an image of the question paper (jpg, png, or jpeg)", type=["jpg", "png", "jpeg"])18 19if uploaded_file:20    # Display the uploaded image21    image = Image.open(uploaded_file)22    st.image(image, caption="Uploaded Question Paper", use_column_width=True)23 24    # Step 2: OCR - Extract Text from Image25    st.header("Step 2: Extract Questions from the Image")26    extracted_text = pytesseract.image_to_string(image)27    st.text_area("Extracted Questions:", extracted_text, height=200)28 29    # Step 3: Generate Answers30    st.header("Step 3: Generate Answers")31    if st.button("Generate Answers"):32        if extracted_text.strip():33            context = extracted_text  # Use OCR output as context34            questions = context.split("\n")  # Split text line by line for questions35            st.subheader("Answers:")36            for question in questions:37                question = question.strip()38                if question:39                    try:40                        result = qa_pipeline(question=question, context=context)41                        st.write(f"**Q:** {question}")42                        st.write(f"**A:** {result['answer']}")43                    except Exception:44                        st.write(f"**Q:** {question}")45                        st.write("**A:** Unable to generate an answer.")46        else:47            st.warning("No text found in the image. Please upload a clearer image.")48 49# Footer50st.markdown("---")51st.markdown("Powered by Streamlit, Tesseract OCR, and Hugging Face Transformers")