TensorFlo commited on
Commit
20d4a20
·
1 Parent(s): d521cdd

working version

Browse files
Files changed (4) hide show
  1. app.py +34 -15
  2. example.xlsx +0 -0
  3. src/transcribe_image.py +30 -1
  4. src/utils.py +42 -0
app.py CHANGED
@@ -5,14 +5,9 @@ import os
5
  from openpyxl import load_workbook, Workbook
6
  from io import BytesIO
7
  import openai
 
8
 
9
- def save_workbook_to_bytes(wb):
10
- # Save the workbook into a BytesIO object (in memory, not on disk)
11
- byte_io = BytesIO()
12
- wb.save(byte_io)
13
- byte_io.seek(0) # Go to the beginning of the BytesIO buffer
14
- return byte_io.getvalue()
15
-
16
 
17
  openai_api_key = os.getenv("OPENAI_API_KEY")
18
 
@@ -21,12 +16,20 @@ openai.api_key = openai_api_key
21
 
22
  st.title("AutoAssess: Student Essay Transcription and Assessment")
23
 
24
- st.title("AutoAssess")
25
- st.write("If you see this, the basic app is loading correctly!")
26
-
27
  # Upload folder of images
28
- uploaded_files = st.file_uploader("Upload a folder of student essays (images)", type=['jpg', 'jpeg', 'png'], accept_multiple_files=True)
 
 
 
 
29
 
 
 
 
 
 
 
30
  # replace uploaded files with files loading from directory
31
  # image_dir = "data/images"
32
  # uploaded_files = []
@@ -37,9 +40,25 @@ uploaded_files = st.file_uploader("Upload a folder of student essays (images)",
37
  # Text inputs for question and criteria
38
  # essay_question = st.text_input("Enter the essay question:")
39
  # grading_criteria = st.text_area("Enter grading criteria or relevant marking information:")
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- essay_question = "What is beauty?"
42
- grading_criteria = "1. Introduction\n2. Body\n3. Conclusion\n4. Grammar\n5. Spelling\n6. Punctuation\n7. Originality\n8. Creativity"
 
 
 
43
 
44
  # Upload Excel file with student IDs and page count
45
  excel_file = st.file_uploader("Upload Excel file with student IDs and page count", type=["xlsx"])
@@ -67,8 +86,8 @@ if st.button("Process Essays"):
67
 
68
  # Display the download button
69
  st.download_button(
70
- label="Download the Excel file",
71
  data=excel_file,
72
- file_name="results.xlsx",
73
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
74
  )
 
5
  from openpyxl import load_workbook, Workbook
6
  from io import BytesIO
7
  import openai
8
+ import base64
9
 
10
+ from src.utils import save_workbook_to_bytes, serve_excel_for_download, image_to_base64, download_image
 
 
 
 
 
 
11
 
12
  openai_api_key = os.getenv("OPENAI_API_KEY")
13
 
 
16
 
17
  st.title("AutoAssess: Student Essay Transcription and Assessment")
18
 
19
+ st.subheader("Upload Student Essays")
 
 
20
  # Upload folder of images
21
+ uploaded_files = st.file_uploader("Upload a folder of student essays (images). Please make sure that the images are in order i.e. first image is the first page of the first student, the second image is the second page of the first student (if it exists) untill all images of the first student are uploaded which should be followed by the ordererd pages of the second student, etc.", type=['jpg', 'jpeg', 'png'], accept_multiple_files=True)
22
+
23
+ # Path to your example images (you need to place them in your project directory or provide a URL)
24
+ image_1_path = "example_essay_1.png" # Example file path
25
+ image_2_path = "example_essay_2.png" # Example file path
26
 
27
+ # Example images section
28
+ st.text("If you don't have any images to upload, you can download (and reupload) the example images below.")
29
+
30
+ # Download buttons for the two example essays (images)
31
+ download_image(image_1_path, "example_essay_1.png")
32
+ download_image(image_2_path, "example_essay_2.png")
33
  # replace uploaded files with files loading from directory
34
  # image_dir = "data/images"
35
  # uploaded_files = []
 
40
  # Text inputs for question and criteria
41
  # essay_question = st.text_input("Enter the essay question:")
42
  # grading_criteria = st.text_area("Enter grading criteria or relevant marking information:")
43
+ st.subheader("Specify Essay Question")
44
+ st.text("For example: What is beauty?")
45
+ essay_question = st.text_input("Enter the essay question:")
46
+
47
+ st.subheader("Specify Grading Criteria")
48
+ st.text("For example: Please follow the german grading system where 1 is the best grade and 6 is the worst grade. An essay with a grade of 1 should be well structured, have a clear introduction, body and conclusion, be free of grammatical errors, spelling mistakes and punctuation errors, be original and creative.")
49
+ grading_criteria = st.text_area("Enter grading criteria or relevant marking information:")
50
+
51
+
52
+ # essay_question = "What is beauty?"
53
+ # grading_criteria = "1. Introduction\n2. Body\n3. Conclusion\n4. Grammar\n5. Spelling\n6. Punctuation\n7. Originality\n8. Creativity"
54
+
55
+
56
 
57
+ # Path to the example Excel file in the repository
58
+ st.subheader("Specify Grading Criteria")
59
+ example_file_path ="example.xlsx"
60
+ serve_excel_for_download(example_file_path)
61
+ st.text("Please download the example Excel file to see the expected format. You will need to adjust the student IDs and page counts. Note that if you provide a grade and reason for a student, the system will not reassess that student's essay. Further, this will help te system make better assessments by calibrating it to your style of grading. Note that grade and reason must both be specified.")
62
 
63
  # Upload Excel file with student IDs and page count
64
  excel_file = st.file_uploader("Upload Excel file with student IDs and page count", type=["xlsx"])
 
86
 
87
  # Display the download button
88
  st.download_button(
89
+ label="Download the assessed Excel file",
90
  data=excel_file,
91
+ file_name="assessments.xlsx",
92
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
93
  )
example.xlsx ADDED
Binary file (9.06 kB). View file
 
src/transcribe_image.py CHANGED
@@ -1,6 +1,34 @@
1
  import base64
2
  from openai import OpenAI
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Function to encode the image
6
  def encode_image(image_path):
@@ -11,7 +39,8 @@ def encode_image(image_path):
11
  def encode_image_from_uploaded_file(image):
12
  # Convert image to bytes
13
  assert image is not None, "No image uploaded."
14
- image_bytes = image.read()
 
15
  return base64.b64encode(image_bytes).decode('utf-8')
16
 
17
  def transcribe_image(image_file):
 
1
  import base64
2
  from openai import OpenAI
3
  import os
4
+ from PIL import Image
5
+ import io
6
+
7
+ def resize_image(image, max_size=800):
8
+ """Resize the image to ensure the max side length is `max_size` while maintaining aspect ratio."""
9
+ # Open the image using Pillow
10
+ img = Image.open(image)
11
+
12
+ # Get the current width and height of the image
13
+ width, height = img.size
14
+
15
+ # Resize the image if necessary
16
+ if width > height:
17
+ new_width = max_size
18
+ new_height = int((new_width / width) * height)
19
+ else:
20
+ new_height = max_size
21
+ new_width = int((new_height / height) * width)
22
+
23
+ # Resize the image using the LANCZOS filter for high-quality rescaling
24
+ img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
25
+
26
+ # Save the resized image to a BytesIO object to later encode to base64
27
+ img_byte_arr = io.BytesIO()
28
+ img.save(img_byte_arr, format='PNG')
29
+ img_byte_arr.seek(0) # Rewind the BytesIO object to the beginning
30
+
31
+ return img_byte_arr
32
 
33
  # Function to encode the image
34
  def encode_image(image_path):
 
39
  def encode_image_from_uploaded_file(image):
40
  # Convert image to bytes
41
  assert image is not None, "No image uploaded."
42
+ resized_image = resize_image(image)
43
+ image_bytes = resized_image.read()
44
  return base64.b64encode(image_bytes).decode('utf-8')
45
 
46
  def transcribe_image(image_file):
src/utils.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ from io import BytesIO
3
+ import streamlit as st
4
+
5
+
6
+
7
+ def save_workbook_to_bytes(wb):
8
+ # Save the workbook into a BytesIO object (in memory, not on disk)
9
+ byte_io = BytesIO()
10
+ wb.save(byte_io)
11
+ byte_io.seek(0) # Go to the beginning of the BytesIO buffer
12
+ return byte_io.getvalue()
13
+
14
+ # Function to serve the file for download
15
+ def serve_excel_for_download(file_path):
16
+ # Open the Excel file in binary mode
17
+ with open(file_path, "rb") as f:
18
+ file_data = f.read()
19
+
20
+ # Provide the download button for the user
21
+ st.download_button(
22
+ label="Download Example Excel File",
23
+ data=file_data,
24
+ file_name="example.xlsx",
25
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
26
+ )
27
+
28
+ # Function to convert image to base64 so that it can be displayed
29
+ def image_to_base64(image_path):
30
+ with open(image_path, "rb") as image_file:
31
+ return base64.b64encode(image_file.read()).decode("utf-8")
32
+
33
+ # Function to allow downloading the example images
34
+ def download_image(image_path, image_name):
35
+ with open(image_path, "rb") as file:
36
+ btn = st.download_button(
37
+ label=f"Download {image_name}",
38
+ data=file,
39
+ file_name=image_name,
40
+ mime="image/png"
41
+ )
42
+ return btn