Spaces:
Paused
Paused
Dobin Yim
commited on
Commit
·
64cbd79
1
Parent(s):
2e4f8b2
zip
Browse files
final.py
CHANGED
@@ -34,6 +34,7 @@ import numpy as np
|
|
34 |
from sklearn.metrics.pairwise import cosine_similarity
|
35 |
import chainlit as cl
|
36 |
import asyncio
|
|
|
37 |
|
38 |
# Load environment variables
|
39 |
load_dotenv()
|
@@ -47,6 +48,7 @@ logger = logging.getLogger(__name__)
|
|
47 |
# Define constants
|
48 |
REFERENCE_DOCUMENT_PATH = './Excel Review.pdf'
|
49 |
UPLOAD_FOLDER = './uploads'
|
|
|
50 |
|
51 |
# Ensure the upload folder exists
|
52 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
@@ -154,9 +156,9 @@ def generate_embeddings(docs: List[Document]) -> List[List[float]]:
|
|
154 |
total_tokens = sum(len(doc.page_content) for doc in docs) # Approximate token count
|
155 |
return embeddings, total_tokens
|
156 |
|
157 |
-
def prepare_files():
|
158 |
-
unzip_file(
|
159 |
-
documents = read_files_from_directory(
|
160 |
reference_document = read_pdf(REFERENCE_DOCUMENT_PATH)
|
161 |
return documents, reference_document
|
162 |
|
@@ -258,8 +260,8 @@ def llm_similarity(answers, student_result):
|
|
258 |
|
259 |
return average_score, llm_score_tokens
|
260 |
|
261 |
-
def process_data() -> Tuple[float, float, int, int, int]:
|
262 |
-
documents, reference_document = prepare_files()
|
263 |
reference, answers, ref_gen_tokens = process_reference(reference_document)
|
264 |
student_result, student_gen_tokens = process_student(documents, reference)
|
265 |
reference_embeddings, student_embeddings, ref_tokens, student_tokens = split_docs(answers, student_result)
|
@@ -273,33 +275,61 @@ def process_data() -> Tuple[float, float, int, int, int]:
|
|
273 |
return average_similarity, average_score, ref_total_tokens, student_total_tokens, llm_total_tokens
|
274 |
|
275 |
async def process_grading():
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
@cl.on_chat_start
|
285 |
-
async def
|
286 |
-
|
|
|
287 |
|
|
|
|
|
|
|
|
|
|
|
288 |
|
|
|
|
|
|
|
289 |
|
290 |
-
#
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
@cl.on_message
|
294 |
async def on_message(message: cl.Message):
|
295 |
-
global user_wants_to_continue
|
296 |
|
297 |
if message.content.lower() == 'yes' and not user_wants_to_continue:
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
|
|
303 |
|
304 |
# Ask user if they want to continue after processing is done
|
305 |
user_wants_to_continue = True
|
@@ -307,27 +337,29 @@ async def on_message(message: cl.Message):
|
|
307 |
|
308 |
elif user_wants_to_continue:
|
309 |
if message.content.lower() == 'yes':
|
310 |
-
user_wants_to_continue = False
|
|
|
311 |
await cl.Message(content="Restarting the app...").send()
|
312 |
-
await asyncio.sleep(1)
|
313 |
python = sys.executable
|
314 |
-
os.execl(python, python, *sys.argv)
|
315 |
|
316 |
elif message.content.lower() == 'no':
|
317 |
-
user_wants_to_continue = False
|
|
|
318 |
await cl.Message(content="Okay, thank you for using the grading app. Restarting...").send()
|
319 |
-
await asyncio.sleep(1)
|
320 |
python = sys.executable
|
321 |
-
os.execl(python, python, *sys.argv)
|
322 |
|
323 |
else:
|
324 |
await cl.Message(content="Invalid response. Please type 'yes' or 'no'.").send()
|
325 |
|
326 |
elif message.content.lower() == 'no':
|
327 |
await cl.Message(content="Okay, thank you for using the grading app. Restarting...").send()
|
328 |
-
await asyncio.sleep(1)
|
329 |
python = sys.executable
|
330 |
-
os.execl(python, python, *sys.argv)
|
331 |
|
332 |
else:
|
333 |
await cl.Message(content="Please type 'yes' to start processing or 'no' to exit.").send()
|
|
|
34 |
from sklearn.metrics.pairwise import cosine_similarity
|
35 |
import chainlit as cl
|
36 |
import asyncio
|
37 |
+
import zipfile
|
38 |
|
39 |
# Load environment variables
|
40 |
load_dotenv()
|
|
|
48 |
# Define constants
|
49 |
REFERENCE_DOCUMENT_PATH = './Excel Review.pdf'
|
50 |
UPLOAD_FOLDER = './uploads'
|
51 |
+
TEMP_DIR = "./temp"
|
52 |
|
53 |
# Ensure the upload folder exists
|
54 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
156 |
total_tokens = sum(len(doc.page_content) for doc in docs) # Approximate token count
|
157 |
return embeddings, total_tokens
|
158 |
|
159 |
+
def prepare_files(zip_file_name: str):
|
160 |
+
unzip_file(os.path.join(UPLOAD_FOLDER, zip_file_name), TEMP_DIR)
|
161 |
+
documents = read_files_from_directory(os.path.join(TEMP_DIR, os.path.splitext(zip_file_name)[0]))
|
162 |
reference_document = read_pdf(REFERENCE_DOCUMENT_PATH)
|
163 |
return documents, reference_document
|
164 |
|
|
|
260 |
|
261 |
return average_score, llm_score_tokens
|
262 |
|
263 |
+
def process_data(zip_file_name: str) -> Tuple[float, float, int, int, int]:
|
264 |
+
documents, reference_document = prepare_files(zip_file_name)
|
265 |
reference, answers, ref_gen_tokens = process_reference(reference_document)
|
266 |
student_result, student_gen_tokens = process_student(documents, reference)
|
267 |
reference_embeddings, student_embeddings, ref_tokens, student_tokens = split_docs(answers, student_result)
|
|
|
275 |
return average_similarity, average_score, ref_total_tokens, student_total_tokens, llm_total_tokens
|
276 |
|
277 |
async def process_grading():
|
278 |
+
global uploaded_file_name
|
279 |
+
if uploaded_file_name:
|
280 |
+
try:
|
281 |
+
# Process the uploaded ZIP file
|
282 |
+
average_similarity, average_score, ref_total_tokens, student_total_tokens, llm_total_tokens = process_data(uploaded_file_name)
|
283 |
+
|
284 |
+
# Send results
|
285 |
+
await cl.Message(content=f"Processing complete. Results:\n"
|
286 |
+
f"Average Similarity: {average_similarity:.2f}\n"
|
287 |
+
f"Average Score: {average_score:.2f}\n"
|
288 |
+
f"Reference Total Tokens: {ref_total_tokens}\n"
|
289 |
+
f"Student Total Tokens: {student_total_tokens}\n"
|
290 |
+
f"LLM Total Tokens: {llm_total_tokens}").send()
|
291 |
+
except Exception as e:
|
292 |
+
await cl.Message(f"An error occurred while processing the zip file: {str(e)}").send()
|
293 |
+
else:
|
294 |
+
await cl.Message("No file has been uploaded yet. Please upload a ZIP file first.").send()
|
295 |
+
|
296 |
+
user_wants_to_continue = False
|
297 |
|
298 |
@cl.on_chat_start
|
299 |
+
async def start():
|
300 |
+
global uploaded_file_name
|
301 |
+
files = None
|
302 |
|
303 |
+
# Wait for the user to upload a file
|
304 |
+
while files is None:
|
305 |
+
files = await cl.AskFileMessage(
|
306 |
+
content="Please upload a zip file to begin!", accept={"application/zip": [".zip"]}
|
307 |
+
).send()
|
308 |
|
309 |
+
zip_file = files[0] # Assuming only one file is uploaded
|
310 |
+
file_path = os.path.join(UPLOAD_FOLDER, zip_file.name)
|
311 |
+
uploaded_file_name = zip_file.name
|
312 |
|
313 |
+
# Move the uploaded file to the desired location
|
314 |
+
os.rename(zip_file.path, file_path)
|
315 |
+
|
316 |
+
# Let the user know that the system is ready
|
317 |
+
await cl.Message(content=f"`{zip_file.name}` uploaded successfully!").send()
|
318 |
+
|
319 |
+
# Ask if the user wants to proceed with grading
|
320 |
+
await cl.Message(content="Do you want to proceed with the grading? (yes/no)").send()
|
321 |
|
322 |
@cl.on_message
|
323 |
async def on_message(message: cl.Message):
|
324 |
+
global user_wants_to_continue, uploaded_file_name
|
325 |
|
326 |
if message.content.lower() == 'yes' and not user_wants_to_continue:
|
327 |
+
if uploaded_file_name:
|
328 |
+
# Start processing
|
329 |
+
processing_message = cl.Message(content="Processing files...")
|
330 |
+
await processing_message.send()
|
331 |
+
await asyncio.sleep(0.5)
|
332 |
+
await process_grading()
|
333 |
|
334 |
# Ask user if they want to continue after processing is done
|
335 |
user_wants_to_continue = True
|
|
|
337 |
|
338 |
elif user_wants_to_continue:
|
339 |
if message.content.lower() == 'yes':
|
340 |
+
user_wants_to_continue = False
|
341 |
+
uploaded_file_name = None
|
342 |
await cl.Message(content="Restarting the app...").send()
|
343 |
+
await asyncio.sleep(1)
|
344 |
python = sys.executable
|
345 |
+
os.execl(python, python, *sys.argv)
|
346 |
|
347 |
elif message.content.lower() == 'no':
|
348 |
+
user_wants_to_continue = False
|
349 |
+
uploaded_file_name = None
|
350 |
await cl.Message(content="Okay, thank you for using the grading app. Restarting...").send()
|
351 |
+
await asyncio.sleep(1)
|
352 |
python = sys.executable
|
353 |
+
os.execl(python, python, *sys.argv)
|
354 |
|
355 |
else:
|
356 |
await cl.Message(content="Invalid response. Please type 'yes' or 'no'.").send()
|
357 |
|
358 |
elif message.content.lower() == 'no':
|
359 |
await cl.Message(content="Okay, thank you for using the grading app. Restarting...").send()
|
360 |
+
await asyncio.sleep(1)
|
361 |
python = sys.executable
|
362 |
+
os.execl(python, python, *sys.argv)
|
363 |
|
364 |
else:
|
365 |
await cl.Message(content="Please type 'yes' to start processing or 'no' to exit.").send()
|