File size: 12,081 Bytes
40b8198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import gradio as gr
import base64
from typing import List
from io import BytesIO
from PIL import Image
import zipfile
from openai import OpenAI

def encode_image(image_path):
    """Encodes an image file to base64."""
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def generate_prompt(question, marking_scheme, student_response):
    """Generates the grading prompt for the OpenAI API."""
    prompt = f"""
    Question: {question}

    Marking Scheme: {marking_scheme}

    Student Response: {student_response}

    As an expert in this field, please grade the student's response based on the marking scheme provided. Provide detailed scores and feedback, and a well-tabulated breakdown of scores.
    """
    return prompt

def read_file_content(file):
    """Reads the content of a file."""
    with open(file.name, 'r') as f:
        return f.read()

def grade_student_answers(client, marking_scheme, student_answers):
    """Grades student answers using the OpenAI API."""
    prompt = f"""
    Marking Scheme:
    {marking_scheme}

    Student Answers:
    {student_answers}

    Grade the student answers based on the marking scheme. Use appropriate Checkmark (✓) and (X). Provide a detailed feedback and score as a percentage.

    The output should resemble that of a Professor!
    """
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are an expert Quiz grader."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=1048
    )
    return response.choices[0].message.content.strip()

def grade_explanatory_test_text(api_key, question_file, marking_scheme_file, student_responses):
    """Grades explanatory test text files."""
    client = OpenAI(api_key=api_key)
    output_files = []
    try:
        question = read_file_content(question_file)
        marking_scheme = read_file_content(marking_scheme_file)
        for student_file in student_responses:
            student_name = os.path.splitext(os.path.basename(student_file.name))[0]
            student_response = read_file_content(student_file)
            prompt = generate_prompt(question, marking_scheme, student_response)
            response = client.chat.completions.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant"},
                    {"role": "user", "content": prompt}
                ],
                max_tokens=3500,
                temperature=0.0
            )
            grade = response.choices[0].message.content.strip()
            output_filename = f"{student_name}_grade.txt"
            with open(output_filename, 'w') as out_f:
                out_f.write(grade)
            output_files.append(output_filename)
        
        zip_filename = "graded_results.zip"
        with zipfile.ZipFile(zip_filename, 'w') as zip_file:
            for file in output_files:
                zip_file.write(file)
        
        return zip_filename
    except Exception as e:
        return f"An error occurred: {e}"

def extract_text_from_image(api_key, image_file):
    """Extracts text from an image using the OpenAI API."""
    try:
        client = OpenAI(api_key=api_key)
        
        with Image.open(image_file) as img:
            img = img.convert("RGB")
            img.thumbnail((1280, 1280))
            buffer = BytesIO()
            img.save(buffer, format="JPEG")
            base64_image = base64.b64encode(buffer.getvalue()).decode('utf-8')
        
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": "You are a helpful assistant"},
                {"role": "user", "content": [
                    {"type":"text", "text": "Extract the text from this image. It is a student exam script, where the student is answering multiple choice questions. Write out the text in the image. Don't include any other text in your output."},
                    {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
                ]}
            ],
            max_tokens=1048
        )
        
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"An error occurred while extracting text from the image: {e}"

def grade_explanatory_test_image(api_key, question_file, marking_scheme_file, student_responses):
    """Grades explanatory test image files."""
    client = OpenAI(api_key=api_key)
    output_files = []
    try:
        question = read_file_content(question_file)
        marking_scheme = read_file_content(marking_scheme_file)
        for image_file in student_responses:
            student_name = os.path.splitext(os.path.basename(image_file.name))[0]
            student_response = extract_text_from_image(api_key, image_file)
            if "An error occurred" in student_response:
                return student_response
            prompt = generate_prompt(question, marking_scheme, student_response)
            response = client.chat.completions.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant"},
                    {"role": "user", "content": prompt}
                ],
                max_tokens=3500,
                temperature=0.0
            )
            grade = response.choices[0].message.content.strip()
            output_filename = f"{student_name}_grade.txt"
            with open(output_filename, 'w') as out_f:
                out_f.write(grade)
            output_files.append(output_filename)
        
        zip_filename = "graded_results.zip"
        with zipfile.ZipFile(zip_filename, 'w') as zip_file:
            for file in output_files:
                zip_file.write(file)
        
        return zip_filename
    except Exception as e:
        return f"An error occurred: {e}"

def grade_multiple_choice_test(api_key, marking_scheme_file, images):
    """Grades multiple choice test image files."""
    client = OpenAI(api_key=api_key)
    output_files = []
    try:
        marking_scheme = read_file_content(marking_scheme_file)
        for image_file in images:
            student_name = os.path.splitext(os.path.basename(image_file.name))[0]
            student_answers = extract_text_from_image(api_key, image_file)
            if "An error occurred" in student_answers:
                return student_answers
            
            grade = grade_student_answers(client, marking_scheme, student_answers)
            
            output_filename = f"{student_name}_grade.txt"
            with open(output_filename, 'w') as out_f:
                out_f.write(grade)
            output_files.append(output_filename)
        
        zip_filename = "graded_results.zip"
        with zipfile.ZipFile(zip_filename, 'w') as zip_file:
            for file in output_files:
                zip_file.write(file)
        
        return zip_filename
    except Exception as e:
        return f"An error occurred: {e}"

def handle_choice(choice):
    """Handles the user choice for test type and updates the UI accordingly."""
    if choice == "Explanatory Test":
        return [
            gr.update(visible=True),  # question_file
            gr.update(visible=True),  # marking_scheme_explanatory_file
            gr.update(visible=True),  # explanatory_type
            gr.update(visible=False), # marking_scheme_mcq_file
            gr.update(visible=False), # image_input_mcq
            gr.update(visible=False), # student_responses_text
            gr.update(visible=False)  # student_responses_image
        ]
    else:
        return [
            gr.update(visible=False), # question_file
            gr.update(visible=False), # marking_scheme_explanatory_file
            gr.update(visible=False), # explanatory_type
            gr.update(visible=True),  # marking_scheme_mcq_file
            gr.update(visible=True),  # image_input_mcq
            gr.update(visible=False), # student_responses_text
            gr.update(visible=False)  # student_responses_image
        ]

def handle_explanatory_type(explanatory_type):
    """Handles the explanatory test type and updates the UI accordingly."""
    if explanatory_type == "Text Files":
        return [
            gr.update(visible=True),  # student_responses_text
            gr.update(visible=False)  # student_responses_image
        ]
    else:
        return [
            gr.update(visible=False), # student_responses_text
            gr.update(visible=True)   # student_responses_image
        ]

def clear_inputs():
    """Clears all input fields."""
    return [
        "",  # API key
        None,  # choice
        gr.update(value=None, visible=False),  # explanatory_type
        gr.update(visible=False),  # question_file
        gr.update(visible=False),  # marking_scheme_explanatory_file
        gr.update(visible=False),  # student_responses_text
        gr.update(visible=False),  # student_responses_image
        gr.update(visible=False),  # marking_scheme_mcq_file
        gr.update(visible=False),  # image_input_mcq
        None   # output_file
    ]

# Gradio Interface
with gr.Blocks() as demo:
    api_key = gr.Textbox(label="OpenAI API Key", type="password")

    choice = gr.Radio(["Explanatory Test", "Multiple Choice Test"], label="Choose the type of test to grade")
    explanatory_type = gr.Radio(["Text Files", "Image Files"], label="Choose the type of explanatory test", visible=False)

    # Explanatory test inputs
    question_file = gr.File(label="Upload Question File", visible=False)
    marking_scheme_explanatory_file = gr.File(label="Upload Marking Scheme File", visible=False)
    student_responses_text = gr.File(label="Upload Student Response Text Files", file_count='multiple', visible=False)
    student_responses_image = gr.File(label="Upload Student Response Image Files", file_count='multiple', visible=False)

    # Multiple choice test inputs
    marking_scheme_mcq_file = gr.File(label="Upload Marking Scheme File", visible=False)
    image_input_mcq = gr.File(label="Upload Student Answer Images", file_count='multiple', visible=False)

    output_file = gr.File(label="Download Graded Results")

    # Handle choice to show/hide appropriate inputs
    choice.change(fn=handle_choice, inputs=choice, outputs=[question_file, marking_scheme_explanatory_file, explanatory_type, marking_scheme_mcq_file, image_input_mcq])
    explanatory_type.change(fn=handle_explanatory_type, inputs=explanatory_type, outputs=[student_responses_text, student_responses_image])

    # Submit and Clear buttons
    submit_btn = gr.Button("Submit")
    clear_btn = gr.Button("Clear")

    submit_btn.click(
        fn=lambda api_key, choice, explanatory_type, question_file, marking_scheme_explanatory_file, student_responses_text, student_responses_image, marking_scheme_mcq_file, image_input_mcq:
        grade_explanatory_test_text(api_key, question_file, marking_scheme_explanatory_file, student_responses_text) if explanatory_type == "Text Files" else
        grade_explanatory_test_image(api_key, question_file, marking_scheme_explanatory_file, student_responses_image) if choice == "Explanatory Test" else
        grade_multiple_choice_test(api_key, marking_scheme_mcq_file, image_input_mcq),
        inputs=[api_key, choice, explanatory_type, question_file, marking_scheme_explanatory_file, student_responses_text, student_responses_image, marking_scheme_mcq_file, image_input_mcq],
        outputs=output_file
    )

    clear_btn.click(
        fn=clear_inputs,
        inputs=[],
        outputs=[api_key, choice, explanatory_type, question_file, marking_scheme_explanatory_file,
                student_responses_text, student_responses_image, marking_scheme_mcq_file,
                image_input_mcq, output_file]
    )

if __name__ == "__main__":
    demo.launch()