dobinyim commited on
Commit
b177f87
·
verified ·
1 Parent(s): a39c45e

Update final.py

Browse files
Files changed (1) hide show
  1. final.py +21 -5
final.py CHANGED
@@ -11,6 +11,7 @@ import sys
11
  import os
12
  import asyncio
13
  import shutil
 
14
  from readfile import prepare_files, USER_FILES_DIR
15
  from typing import List, Dict, Tuple
16
  from dotenv import load_dotenv
@@ -91,12 +92,22 @@ async def start():
91
  content="Please upload a zip file to begin!", accept={"application/zip": [".zip"]}
92
  ).send()
93
 
94
- zip_file = files[0] # Assuming only one file is uploaded
95
- file_path = os.path.join(USER_FILES_DIR, zip_file.name)
96
  uploaded_file_name = zip_file.name
97
 
98
- # Move the uploaded file to the user files directory
99
- shutil.move(zip_file.path, file_path)
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Let the user know that the system is ready
102
  await cl.Message(content=f"`{zip_file.name}` uploaded successfully!").send()
@@ -108,8 +119,10 @@ async def process_grading():
108
  global uploaded_file_name
109
  if uploaded_file_name:
110
  try:
 
 
111
  # Process the uploaded ZIP file
112
- average_similarity, average_score, ref_total_tokens, student_total_tokens, llm_total_tokens = process_data(uploaded_file_name, llm_score_prompt_template)
113
 
114
  # Send results
115
  await cl.Message(content=f"Processing complete. Results:\n"
@@ -118,6 +131,9 @@ async def process_grading():
118
  f"Reference Total Tokens: {ref_total_tokens}\n"
119
  f"Student Total Tokens: {student_total_tokens}\n"
120
  f"LLM Total Tokens: {llm_total_tokens}").send()
 
 
 
121
  except Exception as e:
122
  await cl.Message(content=f"An error occurred while processing the zip file: {str(e)}").send()
123
  else:
 
11
  import os
12
  import asyncio
13
  import shutil
14
+ import tempfile
15
  from readfile import prepare_files, USER_FILES_DIR
16
  from typing import List, Dict, Tuple
17
  from dotenv import load_dotenv
 
92
  content="Please upload a zip file to begin!", accept={"application/zip": [".zip"]}
93
  ).send()
94
 
95
+ zip_file: AskFileResponse = files[0] # Assuming only one file is uploaded
 
96
  uploaded_file_name = zip_file.name
97
 
98
+ # Get the CHAINLIT_USER_FILES_DIR from environment variables
99
+ user_files_dir = os.environ.get('CHAINLIT_USER_FILES_DIR', '/tmp/chainlit_user_files')
100
+
101
+ # Create a temporary directory to store the uploaded file
102
+ with tempfile.TemporaryDirectory() as temp_dir:
103
+ temp_file_path = os.path.join(temp_dir, zip_file.name)
104
+
105
+ # Copy the uploaded file to the temporary directory
106
+ shutil.copy(zip_file.path, temp_file_path)
107
+
108
+ # Move the file to the user files directory
109
+ os.makedirs(user_files_dir, exist_ok=True)
110
+ shutil.move(temp_file_path, os.path.join(user_files_dir, zip_file.name))
111
 
112
  # Let the user know that the system is ready
113
  await cl.Message(content=f"`{zip_file.name}` uploaded successfully!").send()
 
119
  global uploaded_file_name
120
  if uploaded_file_name:
121
  try:
122
+ user_files_dir = os.environ.get('CHAINLIT_USER_FILES_DIR', '/tmp/chainlit_user_files')
123
+ file_path = os.path.join(user_files_dir, uploaded_file_name)
124
  # Process the uploaded ZIP file
125
+ average_similarity, average_score, ref_total_tokens, student_total_tokens, llm_total_tokens = process_data(file_path, llm_score_prompt_template)
126
 
127
  # Send results
128
  await cl.Message(content=f"Processing complete. Results:\n"
 
131
  f"Reference Total Tokens: {ref_total_tokens}\n"
132
  f"Student Total Tokens: {student_total_tokens}\n"
133
  f"LLM Total Tokens: {llm_total_tokens}").send()
134
+
135
+ # Remove the file after processing
136
+ os.remove(file_path)
137
  except Exception as e:
138
  await cl.Message(content=f"An error occurred while processing the zip file: {str(e)}").send()
139
  else: