Spaces:
Runtime error
Runtime error
File size: 7,472 Bytes
4ec6767 |
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 |
# To run streamlit, go to terminal and type: 'streamlit run app.py'
# Core Packages ###########################
import os
import shutil
import docx2txt
import PyPDF2
import streamlit as st
import openai
import requests
import json
#######################################################################################################################
current_path = os.path.abspath(os.path.dirname(__file__))
project_title = "ChatGPT Essay Evaluator"
project_icon = "46_Knowledge-white4.png"
# st.set_page_config(page_title=project_title, initial_sidebar_state='collapsed',page_icon=project_icon)
#######################################################################################################################
def read_pdf(file):
pdfReader = PyPDF2.PdfReader(file)
count = len(pdfReader.pages)
all_page_text = ""
for i in range(count):
page = pdfReader.pages[i]
all_page_text += page.extract_text()
return all_page_text
def build_criteria(criteria, min_score, max_score):
text = ""
num_criteria = len(criteria)
for i, val in enumerate(criteria):
text += "Criteria: \n"
text += f"({i+1}/{num_criteria}) {val} Minimum Score: {min_score[i]} | Maximum Score: {max_score[i]} \n\n"
return text
def run_chatgpt(essay, criteria_text):
# Define the endpoint URL and payload data
endpoint = "https://joshuafreeedu.pythonanywhere.com/evaluate-essay"
payload = {"essay_text": essay, "criteria": criteria_text}
response = requests.post(endpoint, json=payload)
return response.json()["evaluation"]
def main():
head_col = st.columns([1,8])
with head_col[0]:
st.image(project_icon)
with head_col[1]:
st.title(project_title)
st.markdown("***")
st.subheader("")
#########################################
# # instructions
# st.subheader("How to use: ")
# st.write("1a. Input your essay in the text box; or \n\n"
# "1b. Click on Upload Files to submit one or multiple essays saved in doc, docx, or txt format.")
# st.write("2. Click on \'Grade Essay\' button to run the model.")
#########################################
uploaded_file = st.file_uploader('Upload Files', accept_multiple_files=False, type=['docx','txt','pdf'])
ta_val = "" # Value for the text area
upload_flag = False
#If a file/s is uploaded, disable input in the text area; then, display the essays list
if uploaded_file:
upload_flag = True
# Parse the contents of the uploaded file according to their extension txt docx or pdf
if uploaded_file.name.split(".")[-1] == "docx": # docx files
contents = docx2txt.process(uploaded_file)
elif uploaded_file.name.split(".")[-1] == "pdf": # pdf files
contents = read_pdf(uploaded_file)
else: # txt files
contents = uploaded_file.read().decode("utf-8")
#ta_val will be the preview of all the essays in the text area; display index numbering if there are more than one file
ta_val += contents
# text area input for the essay, button to run the model, other widgets
response_ta = st.text_area("Essay:",placeholder="You can input your essay here instead of uploading a file.",height=500, value=ta_val, disabled=upload_flag)
if "criteria" not in st.session_state:
st.session_state["criteria"] = ["Cohesion", "Syntax", "Vocabulary", "Phraseology", "Grammar", "Conventions"]
st.session_state["min_scores"] = [0, 0, 0, 0, 0, 0]
st.session_state["max_scores"] = [10, 10, 10, 10, 10, 10]
st.session_state["remove_criteria"] = False
with st.form(key="criteria_form"):
col1, col2, col3 = st.columns(3)
with col1:
st.subheader("Criteria")
new_criteria = []
for i, val in enumerate(st.session_state["criteria"]):
new_criteria.append(st.text_input(f"Criteria {i + 1}", st.session_state["criteria"][i], label_visibility="collapsed"))
with col2:
st.subheader("Minimum Score")
new_min_scores = []
for i, val in enumerate(st.session_state["min_scores"]):
new_min_scores.append(st.number_input(f"Minimum Score {i + 1}", 0, st.session_state["max_scores"][i],
st.session_state["min_scores"][i], label_visibility="collapsed"))
with col3:
st.subheader("Maximum Score")
new_max_scores = []
for i, val in enumerate(st.session_state["max_scores"]):
new_max_scores.append(st.number_input(f"Maximum Score {i + 1}", st.session_state["min_scores"][i], 100,
st.session_state["max_scores"][i], label_visibility="collapsed"))
submit_criteria = col1.form_submit_button("Update Criteria")
add_criteria = col2.checkbox("Add criteria")
remove_criteria = col3.checkbox("Remove criteria")
if submit_criteria:
# Add new criterion
if add_criteria and remove_criteria:
st.warning("Cannot add and remove criteria at the same time.")
else:
if add_criteria:
new_criteria.append("Enter new criteria name..")
new_min_scores.append(0)
new_max_scores.append(10)
st.session_state["criteria"] = new_criteria
st.session_state["min_scores"] = new_min_scores
st.session_state["max_scores"] = new_max_scores
st._rerun()
# Remove criterion
if remove_criteria:
remove_list = st.multiselect("Select criteria to remove:", st.session_state["criteria"])
if remove_list:
new_criteria = [c for c in new_criteria if c not in remove_list]
new_min_scores = [s for c, s in zip(new_criteria, new_min_scores) if c not in remove_list]
new_max_scores = [s for c, s in zip(new_criteria, new_max_scores) if c not in remove_list]
st.session_state["criteria"] = new_criteria
st.session_state["min_scores"] = new_min_scores
st.session_state["max_scores"] = new_max_scores
st._rerun()
else:
st.session_state["criteria"] = new_criteria
st.session_state["min_scores"] = new_min_scores
st.session_state["max_scores"] = new_max_scores
st._rerun()
# send essay to chatGPT when the button is clicked
if st.button("Submit"):
if not ta_val: # if the text area is empty:
st.error("Please input the essay in the corresponding text area.")
else:
# ChatGPT Evaluation Section
criteria_text = build_criteria(st.session_state["criteria"], st.session_state["min_scores"],
st.session_state["max_scores"])
st.session_state.chatgpt_evaluation = run_chatgpt(ta_val, criteria_text)
if "chatgpt_evaluation" in st.session_state:
st.subheader("Evaluation: ")
st.write(st.session_state["chatgpt_evaluation"])
if __name__ == '__main__':
main()
# To run streamlit, go to terminal and type: 'streamlit run app-source.py'
|