Spaces:
Sleeping
Sleeping
import streamlit as st | |
import google.generativeai as genai | |
import os | |
import tempfile | |
MODEL_ID = "gemini-2.0-flash-exp" | |
genai.configure(api_key=os.getenv("GEMINI_API_KEY")) | |
model = genai.GenerativeModel(MODEL_ID) | |
chat = model.start_chat() | |
# Initialize session state variables | |
if 'essay_question' not in st.session_state: | |
st.session_state.essay_question = "" | |
if 'scoring_rubric' not in st.session_state: | |
st.session_state.scoring_rubric = "" | |
if "subject" not in st.session_state: | |
st.session_state.subject = "" | |
if "topic" not in st.session_state: | |
st.session_state.topic = "" | |
def generate_essay_question(subject, topic, difficulty): | |
try: | |
prompt = f"Create an essay question on the topic '{topic}' in the context of {subject} with a {difficulty} difficulty level. Output the essay question only. Do not provide any other information." | |
response = model.generate_content(prompt) | |
return response.text | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
return None | |
def generate_scoring_rubric(essay_question): | |
return f"Scoring Rubric for the essay question: {essay_question}\n\n1. Clarity: 10 points\n2. Argument Strength: 10 points\n3. Grammar: 10 points\n4. Creativity: 10 points\n5. Relevance to Topic: 10 points\n\nTotal: 50 points" | |
def capture_exam_photo(): | |
img_file_buffer = None | |
image_path = "" | |
# Use the camera to capture an image | |
img_file_buffer = st.camera_input("Take a picture") | |
if img_file_buffer is not None: | |
# Save the image to a temporary file | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: | |
temp_file.write(img_file_buffer.read()) | |
image_path = temp_file.name | |
return image_path | |
else: | |
st.warning("Please capture an image of the exam paper.") | |
return None | |
def main(): | |
# Streamlit UI | |
st.title("π AI Assisted Essay Scoring App") | |
about = """# Essay Grading System π | |
Welcome to the **Essay Grading System**! This interactive app helps educators generate essay questions, create scoring rubrics, and automate grading using AI. | |
## π Features | |
β **Essay Question Generator** β Select a subject, topic, and difficulty to generate thought-provoking essay questions. | |
β **Scoring Rubric Builder** β Automatically generate a rubric and refine it as needed. | |
β **AI-Powered Grading** β Upload a photo of an exam paper and let AI assess it based on the rubric. | |
## π― How It Works | |
1οΈβ£ **Go to the "Essay Question" tab** β Choose your subject, enter a topic, and set the difficulty. | |
2οΈβ£ **Switch to "Scoring Rubric"** β A grading rubric is generated based on the question, and you can edit it. | |
3οΈβ£ **Move to "Grading Outputs"** β Upload an image of a handwritten essay, and the AI will grade it based on the rubric. | |
π **Enhance efficiency and accuracy in grading with this AI-assisted tool!** | |
### π About the Creator | |
**Created by:** *Louie F. Cervantes, M.Eng. (Information Engineering)* | |
**(c) 2025 West Visayas State University** | |
""" | |
with st.expander("How to Use this App"): | |
st.markdown(about) | |
tabs = ["Essay Question", "Scoring Rubric", "Grading Outputs"] | |
selected_tab = st.sidebar.radio("Select a Tab", tabs) | |
if selected_tab == "Essay Question": | |
st.header("Generate Essay Question") | |
if st.session_state.subject != "": | |
subject = st.text_input("Enter Subject", st.session_state.subject) | |
else: | |
subject = st.text_input("Enter Subject") | |
if st.session_state.subject != "": | |
topic = st.text_input("Enter Topic", st.session_state.topic) | |
else: | |
topic = st.text_input("Enter Topic") | |
difficulty = st.selectbox("Select Difficulty", ["Easy", "Moderate", "Difficult"]) | |
st.session_state.subject = subject | |
st.session_state.topic = topic | |
if st.button("Generate Question"): | |
if subject and topic and difficulty: | |
st.session_state.essay_question = generate_essay_question(subject, topic, difficulty) | |
st.success("Essay question generated successfully!") | |
else: | |
st.warning("Please provide the subject and topic.") | |
if st.session_state.essay_question: | |
st.subheader("Generated Essay Question:") | |
st.write(st.session_state.essay_question) | |
elif selected_tab == "Scoring Rubric": | |
st.header("Scoring Rubric") | |
if not st.session_state.essay_question: | |
st.warning("Please generate an essay question first.") | |
else: | |
if not st.session_state.scoring_rubric: | |
st.session_state.scoring_rubric = generate_scoring_rubric(st.session_state.essay_question) | |
st.subheader("Generated Scoring Rubric:") | |
st.session_state.scoring_rubric = st.text_area("Edit Rubric", st.session_state.scoring_rubric, height=200) | |
elif selected_tab == "Grading Outputs": | |
st.header("Grading Outputs") | |
if not st.session_state.essay_question or not st.session_state.scoring_rubric: | |
st.warning("Please generate both an essay question and a scoring rubric first.") | |
else: | |
exam_image_path = capture_exam_photo() | |
mime_type = "image/jpeg" | |
if exam_image_path is not None: | |
# Upload the file with the correct MIME type | |
file_data = genai.upload_file(exam_image_path, mime_type=mime_type) | |
multimodal_prompt = f"Extract all the text from the image. Score the essay exam response to the Essay Question: {st.session_state.essay_question}\nScoring Rubric: {st.session_state.scoring_rubric}. Provide feednack on the essay response." | |
# Send file and prompt to Gemini API | |
response = chat.send_message( | |
[ | |
multimodal_prompt, | |
file_data | |
] | |
) | |
st.subheader("AI Grading Response:") | |
# Display Gemini response | |
st.markdown(response.text) | |
if __name__ == "__main__": | |
main() |