Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pytesseract
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Load pre-trained model for question-answering
|
| 8 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 9 |
+
|
| 10 |
+
# Function to extract text using OCR
|
| 11 |
+
def extract_text_from_image(image_file):
|
| 12 |
+
image = Image.open(image_file)
|
| 13 |
+
text = pytesseract.image_to_string(image)
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
# Process text to extract questions
|
| 17 |
+
def extract_questions_from_text(text):
|
| 18 |
+
# Extract lines that look like questions
|
| 19 |
+
questions = re.findall(r'(?:[^\n]*\?)', text)
|
| 20 |
+
return questions
|
| 21 |
+
|
| 22 |
+
# Extract student name and roll number
|
| 23 |
+
def extract_student_info(text):
|
| 24 |
+
name = re.search(r"NAME\s*=\s*([\w\s]+)", text, re.IGNORECASE)
|
| 25 |
+
roll_no = re.search(r"Roll\s*NO\s*=\s*(\d+)", text, re.IGNORECASE)
|
| 26 |
+
student_name = name.group(1).strip() if name else "Unknown"
|
| 27 |
+
roll_number = roll_no.group(1).strip() if roll_no else "Unknown"
|
| 28 |
+
return student_name, roll_number
|
| 29 |
+
|
| 30 |
+
# Grade answers using QA model
|
| 31 |
+
def grade_answer(question, context):
|
| 32 |
+
result = qa_pipeline(question=question, context=context)
|
| 33 |
+
return result['score'], "Correct" if result['score'] > 0.5 else "Incorrect"
|
| 34 |
+
|
| 35 |
+
# Streamlit App
|
| 36 |
+
st.title("Handwritten Answer Sheet Grading System")
|
| 37 |
+
st.write("Upload an image or handwritten file to process.")
|
| 38 |
+
|
| 39 |
+
# Upload image
|
| 40 |
+
uploaded_image = st.file_uploader("Upload Handwritten Image", type=["png", "jpg", "jpeg"])
|
| 41 |
+
|
| 42 |
+
if uploaded_image:
|
| 43 |
+
st.image(uploaded_image, caption="Uploaded Handwritten File", use_column_width=True)
|
| 44 |
+
|
| 45 |
+
# Extract text using OCR
|
| 46 |
+
extracted_text = extract_text_from_image(uploaded_image)
|
| 47 |
+
st.subheader("Extracted Text")
|
| 48 |
+
st.text(extracted_text)
|
| 49 |
+
|
| 50 |
+
# Extract student info
|
| 51 |
+
student_name, roll_number = extract_student_info(extracted_text)
|
| 52 |
+
st.subheader(f"Student Name: {student_name}")
|
| 53 |
+
st.subheader(f"Roll No: {roll_number}")
|
| 54 |
+
|
| 55 |
+
# Extract questions
|
| 56 |
+
questions = extract_questions_from_text(extracted_text)
|
| 57 |
+
st.subheader("Extracted Questions")
|
| 58 |
+
for i, question in enumerate(questions):
|
| 59 |
+
st.write(f"Q{i+1}: {question}")
|
| 60 |
+
|
| 61 |
+
# Grade the answers
|
| 62 |
+
st.subheader("Grading Results")
|
| 63 |
+
for question in questions:
|
| 64 |
+
score, feedback = grade_answer(question, extracted_text)
|
| 65 |
+
st.write(f"**Question:** {question}")
|
| 66 |
+
st.write(f"**Score:** {score:.2f}")
|
| 67 |
+
st.write(f"**Feedback:** {feedback}")
|
| 68 |
+
st.write("---")
|