jpcabangon commited on
Commit
f9cf7b8
·
1 Parent(s): 535d03c

fix output dataframes; update examples

Browse files
app.py CHANGED
@@ -66,20 +66,39 @@ def convert_ave_to_score_range(score, max, min):
66
 
67
  def run_model(answer, min_score, max_score):
68
  evaluation = 0
69
- if len(answer) > 20:
70
- model = load_model()
71
- evaluation = predict(answer,model)
72
- else:
73
- st.error("Your answer is too short.")
74
 
75
  # get the average of the score evaluations
76
  ave = evaluation['Grade'].mean()
77
 
78
- grade = convert_ave_to_score_range(ave,max_score,min_score)
79
  grade = round(grade)
80
  final_grade = max_score if max_score < grade else grade
 
81
  return evaluation, final_grade
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  def main():
84
  head_col = st.columns([1,8])
85
  with head_col[0]:
@@ -94,32 +113,89 @@ def main():
94
  st.subheader("")
95
  #########################################
96
  # instructions
97
- st.subheader("How to use: \n"
98
- "1. Input your essay in the text box.\n"
99
- "2. Click on \'Grade Essay\' button to run the model.")
 
100
 
101
  #########################################
102
 
103
- # text area input for the prompt and response, button to run the model, other widgets
104
- response_ta = st.text_area("Essay:",placeholder="Input the answer / essay here.",height=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  col1,col2,col3 = st.columns(3)
106
  min_score = col1.number_input('Minimum Score',0,100,0)
107
  max_score = col2.number_input('Maximum Score',0,100,10)
108
  run_button = st.button("Grade Essay")
109
 
110
- # button is clicked
 
111
  if run_button:
112
- if not response_ta: # if any text area is empty:
113
- st.error("Please input the essay in their corresponding text area.")
114
- if min_score >= max_score:
115
  st.error("Minimum score must be less than maximum score.")
116
  else: # run model
117
- eval_df, score = run_model(answer=response_ta, min_score=min_score, max_score=max_score)
118
- # output message template
119
- msg = f"Your essay score is: {score} (Minimum Possible Score: {min_score} | Maximum Possible Score: {max_score})"
120
- st.write(msg)
121
- st.write("Score breakdown (1-4):")
122
- st.dataframe(eval_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  #########################################
125
  # examples section
@@ -127,39 +203,69 @@ def main():
127
  st.markdown("***")
128
  st.subheader("")
129
 
130
- # # generate examples dropdown
131
- # # st.subheader("Here are some example prompts and responses for you to try:")
132
- # examples = []
133
- # selected_example = st.selectbox('Select an example prompt:',examples)
134
- #
135
- # # default minimum and maximum scores for each example
136
- # mins_ex = [2,1,0,0,0,0,0,0]
137
- # maxes_ex = [12,6,3,3,4,4,30,60]
138
- #
139
- # # widgets and button to run on examples
140
- # prompt_ta_ex = st.text_area("Essay Prompt:",placeholder="Input the question or the essay prompt here.", value=selected_example,key='prompt_ta_ex',height=175)
141
- # response_ta_ex = st.text_area("Essay Response:",placeholder="Input the answer / essay response here.",key='response_ta_ex',height=250)
142
- # col1_ex, col2_ex, col3_ex = st.columns(3)
143
- # selected_min = mins_ex[examples.index(selected_example)]
144
- # selected_max = maxes_ex[examples.index(selected_example)]
145
- # min_score_ex = col1_ex.number_input('Minimum Score',0,100,key='min_score_ex',value=selected_min)
146
- # max_score_ex = col2_ex.number_input('Maximum Score',0,100,key='max_score_ex',value=selected_max)
147
- # passing_rate_ex = col3_ex.number_input('Passing Rate',0.0,1.0,0.6,step=0.05,key='passing_rate_ex')
148
- # run_button_ex = st.button("Grade Example Essay")
149
- #
150
- # # button is clicked
151
- # if run_button_ex:
152
- # if not prompt_ta_ex or not response_ta_ex: # if any text area is empty:
153
- # st.error("Please input the prompt / response in their corresponding text area.")
154
- # else: # run model
155
- # score, passed = run_model(answer=response_ta_ex, min_score=min_score_ex, max_score=max_score_ex, passing_rate=passing_rate_ex)
156
- # # output message template
157
- # msg_ex = f"Your essay score is: {int(score)} (Minimum Possible Score: {min_score_ex} | Maximum Possible Score: {max_score_ex})"
158
- #
159
- # if passed: # check if score achieved passes the threshold
160
- # st.success(f"Congratulations! {msg_ex}")
161
- # if not passed:
162
- # st.warning(msg_ex)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
 
165
  if __name__ == '__main__':
 
66
 
67
  def run_model(answer, min_score, max_score):
68
  evaluation = 0
69
+
70
+ st.write('Loading model..')
71
+ model = load_model()
72
+ st.write('Grading essay..')
73
+ evaluation = predict(answer,model)
74
 
75
  # get the average of the score evaluations
76
  ave = evaluation['Grade'].mean()
77
 
78
+ grade = convert_ave_to_score_range(ave, max_score, min_score)
79
  grade = round(grade)
80
  final_grade = max_score if max_score < grade else grade
81
+
82
  return evaluation, final_grade
83
 
84
+
85
+ def run_model_on_list(answers, min_score, max_score):
86
+ evaluations = []
87
+ final_grades = []
88
+ st.write('Loading model..')
89
+ model = load_model()
90
+
91
+ for answer in answers:
92
+ st.write(f'Grading essay #{answers.index(answer)+1}..')
93
+ evaluations.append(predict(answer,model))
94
+ ave = evaluations[answers.index(answer)]['Grade'].mean()
95
+
96
+ grade = convert_ave_to_score_range(ave, max_score, min_score)
97
+ grade = round(grade)
98
+ final_grades.append(max_score if max_score < grade else grade)
99
+
100
+ return evaluations, final_grades
101
+
102
  def main():
103
  head_col = st.columns([1,8])
104
  with head_col[0]:
 
113
  st.subheader("")
114
  #########################################
115
  # instructions
116
+ st.subheader("How to use: ")
117
+ st.write("1a. Input your essay in the text box; or \n\n"
118
+ "1b. Click on Upload Files to submit one or multiple essays saved in doc, docx, or txt format.")
119
+ st.write("2. Click on \'Grade Essay\' button to run the model.")
120
 
121
  #########################################
122
 
123
+ uploaded_files = st.file_uploader('Upload Files', accept_multiple_files=True, type=['doc','docx','txt'])
124
+ essays = [] # List of essays extracted from uploaded files
125
+ filenames = [] # list of the filenames; used in the final output dataframe
126
+ ta_val = "" # Value for the text area
127
+ upload_flag = False
128
+
129
+ #If a file/s is uploaded, disable input in the text area; then, display the essays list
130
+ if uploaded_files:
131
+ upload_flag = True
132
+
133
+ # iterate through each uploaded file
134
+ for uploaded_file in uploaded_files:
135
+ contents = "" # Compile all the essays from each file and display them on the text area
136
+ filenames.append(uploaded_file.name) # Add each file name to the list
137
+
138
+ # Read each line and convert to string format; compile all the lines into the variable 'contents'
139
+ for line in uploaded_file.getvalue().decode('utf-8').splitlines():
140
+ contents += line + "\n"
141
+ #Add the compiled contents of the file into the 'essays' list before going to the next uploaded file
142
+ essays.append(contents)
143
+ #ta_val will be the preview of all the essays in the text area; display index numbering if there are more than one file
144
+ ta_val += f"[{uploaded_files.index(uploaded_file)}]\n" + contents + "\n" if len(uploaded_files)>1 else contents
145
+
146
+ # text area input for the essay, button to run the model, other widgets
147
+ response_ta = st.text_area("Essay:",placeholder="Input the answer / essay here.",height=500, value=ta_val, disabled=upload_flag)
148
  col1,col2,col3 = st.columns(3)
149
  min_score = col1.number_input('Minimum Score',0,100,0)
150
  max_score = col2.number_input('Maximum Score',0,100,10)
151
  run_button = st.button("Grade Essay")
152
 
153
+ st.session_state["final_df"] = pd.DataFrame()
154
+ # run the model when the button is clicked
155
  if run_button:
156
+ if not response_ta: # if the text area is empty:
157
+ st.error("Please input the essay in the corresponding text area.")
158
+ elif min_score >= max_score:
159
  st.error("Minimum score must be less than maximum score.")
160
  else: # run model
161
+ if not upload_flag:
162
+ eval_df, score = run_model(answer=response_ta, min_score=min_score, max_score=max_score)
163
+ # output message template
164
+ msg = f"Your essay score is: {score} (Minimum Possible Score: {min_score} | Maximum Possible Score: {max_score})"
165
+ st.write(msg)
166
+ st.write("Score breakdown (1-4):")
167
+ st.dataframe(eval_df)
168
+ else:
169
+ # 'evals' is a list of dataframes [DataFrame]
170
+ # 'scores' is a list of the grades [int]
171
+ evals, scores = run_model_on_list(essays, min_score, max_score)
172
+
173
+ # Display the final grade for each uploaded file
174
+ grades_df = pd.DataFrame({'Filename':filenames,'Final Grade':scores})
175
+ st.write("Grading done!")
176
+ st.dataframe(grades_df)
177
+
178
+ st.write("Merging grades with evaluations..")
179
+
180
+ # Add a column 'Filename' to each set of evaluation, and set the value to the corresponding file name
181
+ for f in filenames:
182
+ evals[filenames.index(f)]['Filename'] = f
183
+
184
+ # Combine the list of evaluation dataframes 'evals' into one single dataframe 'evals_df'
185
+ evals_df = pd.concat([df for df in evals])
186
+
187
+ # Combine the Grades with the Evaluations, then show it
188
+ final_df = grades_df.merge(evals_df, on='Filename')
189
+ st.dataframe(final_df)
190
+
191
+ st.session_state["final_df"] = final_df
192
+
193
+ # Button for downloading the combined grades and evaluations into a csv file
194
+ if st.button("Download results"):
195
+ if st.session_state["final_df"].empty:
196
+ st.error("Please grade an essay.")
197
+ else:
198
+ st.session_state["final_df"].to_csv()
199
 
200
  #########################################
201
  # examples section
 
203
  st.markdown("***")
204
  st.subheader("")
205
 
206
+ # generate examples dropdown
207
+ st.subheader("Here are a few example essays:")
208
+ examples = {}
209
+ examples_fnames = []
210
+ examples_dir = os.path.join(current_path,'examples')
211
+ for ex in os.listdir(examples_dir):
212
+ examples[ex] = open(os.path.join(examples_dir, ex), 'rb')
213
+ examples_fnames.append(ex)
214
+
215
+ selected_example = st.multiselect('Select an example essay:',examples_fnames)
216
+ ex_names = []
217
+ ex_essays = []
218
+ ta_val_ex = ""
219
+
220
+
221
+ # iterate through each selected example
222
+ for example in selected_example:
223
+ contents_ex = "" # Compile all the essays from each file and display them on the text area
224
+ ex_names.append(example) # Add each file name to the list
225
+
226
+ # Read each line and convert to string format; compile all the lines into the variable 'contents'
227
+ for line in examples[example].read().decode('utf-8').splitlines():
228
+ contents_ex += line + "\n"
229
+ # Add the compiled contents of the file into the 'essays' list before going to the next uploaded file
230
+ ex_essays.append(contents_ex)
231
+ # ta_val will be the preview of all the essays in the text area; display index numbering if there are more than one file
232
+ ta_val_ex += f"[{selected_example.index(example)}]\n" + contents_ex + "\n" if len(selected_example) > 1 else contents_ex
233
+
234
+ # widgets and button to run on examples
235
+ response_ta_ex = st.text_area("Essay/s:",placeholder="Your selected example essay/s will display here.",value=ta_val_ex,key='response_ta_ex',height=500, disabled=True)
236
+ col1_ex, col2_ex, col3_ex = st.columns(3)
237
+ min_score_ex = col1_ex.number_input('Minimum Score',0,100,0,key='min_score_ex')
238
+ max_score_ex = col2_ex.number_input('Maximum Score',0,100,10,key='max_score_ex')
239
+ run_button_ex = st.button("Grade Example Essay/s")
240
+
241
+ # button is clicked
242
+ if run_button_ex:
243
+ if not response_ta_ex: # if any text area is empty:
244
+ st.error("Please input the essay in their corresponding text area.")
245
+ if min_score_ex >= max_score_ex:
246
+ st.error("Minimum score must be less than maximum score.")
247
+ else: # run model
248
+ # 'evals' is a list of dataframes [DataFrame]
249
+ # 'scores' is a list of the grades [int]
250
+ evals_ex, scores_ex = run_model_on_list(ex_essays, min_score_ex, max_score_ex)
251
+
252
+ # Display the final grade for each uploaded file
253
+ grades_df_ex = pd.DataFrame({'Filename': ex_names, 'Final Grade': scores_ex})
254
+ st.write("Grading done!")
255
+ st.dataframe(grades_df_ex)
256
+
257
+ st.write("Merging grades with evaluations..")
258
+
259
+ # Add a column 'Filename' to each set of evaluation, and set the value to the corresponding file name
260
+ for f in ex_names:
261
+ evals_ex[ex_names.index(f)]['Filename'] = f
262
+
263
+ # Combine the list of evaluation dataframes 'evals' into one single dataframe 'evals_df'
264
+ evals_df_ex = pd.concat([df for df in evals_ex])
265
+
266
+ # Combine the Grades with the Evaluations, then show it
267
+ final_df_ex = grades_df_ex.merge(evals_df_ex, on='Filename')
268
+ st.dataframe(final_df_ex)
269
 
270
 
271
  if __name__ == '__main__':
examples/sample essay 1.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Computer headphones are a type of headphones that are specifically designed to be used with computers and other digital devices for listening to music, videos, and other audio content. Computer headphones, just like any other electronic device, may have some drawbacks such as poor sound quality, uncomfortable fit, or short lifespan which can make the listening experience less enjoyable. Computer headphones can provide a more immersive and personalized listening experience, as well as allowing for privacy and concentration while working on the computer. In conclusion, computer headphones have their own set of advantages and disadvantages, but overall, they can enhance the audio experience for computer users by providing better sound quality, comfort and privacy.
examples/sample essay 2.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ My famly is so nice. I have a mommmy and a daddy and a baby brother. My mommy makes me yummi food and my daddy plays with me. My baby brother is so cute. He makse a lot of noise but I like him. I love my family because they are care for me. They give hugs and kisses to me and tell that I am special. I love my family very much.
examples/sample essay 3.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Tell draw quality feedback advantage brick layout. Having wind dive flying under electricity can feedback hard ice. Full of counters, water attacks stars with seldom pity. Generate bars of plastic paper, rocks form gaseous drinks unsafe for bathing.
examples/sample essay 4.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ They are often an apex predator kept as pets. They are and are endangered in the wild. Quadrupedal mammals with long necks and legs, and Giraffes are tall, they are native to Africa. They are the living animal on earth. They come in a tallest wide variety of, and can be found in shapes and sizes nearly every body of Earth. They world and come in a wide range are water on found all over the of sizes, colors and shapes, from the tiny hummingbird to the large ostrich.
2
+
3
+ Extant species of the genus they are the only homo, and are unique they shape and use tools and technology in the way to adapt to their environment. They can be supercomputers, and are used in a wide range of classified as personal computers, servers, mainframes, and applications such as. They are known to be communication, entertainment, and scientific research carriers of disease and pests, and are considered as a many areas. They can pest species in cause damage to buildings and crops, and can threat to also pose a human health.
4
+
5
+ Systematic study of the natural science is the world through observation and experimentation, with the goal of discovering and understanding the underlying principles and laws of the universe. It encompasses a wide range of disciplines including physics, chemistry, biology, geology, and astronomy, and forms the basis for many fields of study, from medicine to engineering.
examples/sample essay 5.txt ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
2
+
3
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
4
+
5
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
6
+
7
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
8
+
9
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
10
+
11
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
12
+
13
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
14
+
15
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
16
+
17
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
18
+
19
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
20
+
21
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
22
+
23
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
24
+
25
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
26
+
27
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
28
+
29
+ In conclusion, the talking camera was a revolutionary device that changed the way people captured and shared memories. Its advanced technology, user-friendly interface, and innovative features made it an essential tool for anyone looking to preserve their memories in a more personal and meaningful way.
examples/sample essay 6.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ I am writer. I write things. I write stuff. I like writing. Writing is my passion. I want to be famous as a writer. I want to make a living by writing. I want people to read my written work. I write about many things. I am a writer.
examples/sample essay 7.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ I am writer. I write things. I write stuff. I like writing. Writing is my passion. I want to be famous as a writer. I want to make a living by writing. I want people to read my written work. I write about many things. I am a writer.
2
+
3
+ I write creatively. I can also write academically and technically. I practiced writing a lot. Writing is an essential skill that I use to express myself and share my ideas. I write for many purposes. Sometimes, I collaborate with other writers. I love writing.
examples/sample essay 8.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ I am writer. I write things. I write stuff. I like writing. Writing is my passion. I want to be famous as a writer. I want to make a living by writing. I want people to read my written work. I write about many things. I am a writer.
2
+
3
+ I write creatively. I can also write academically and technically. I practiced writing a lot. Writing is an essential skill that I use to express myself and share my ideas. I write for many purposes. Sometimes, I collaborate with other writers. I love writing.
4
+
5
+ I am good at writing. At least, I think I am good at writing. I write with grammar and vocabulary. I write to change the world and inspire others. I write so that others will learn to write too. Writing helps me when I'm sad. I hope writing helps others when they are sad too. Writing is good.