dobinyim commited on
Commit
e9013f4
·
verified ·
1 Parent(s): fc8f238

Update final.py

Browse files
Files changed (1) hide show
  1. final.py +27 -6
final.py CHANGED
@@ -84,6 +84,11 @@ uploaded_file_name = None
84
  import os
85
  import shutil
86
 
 
 
 
 
 
87
  @cl.on_chat_start
88
  async def start():
89
  global uploaded_file_name
@@ -95,27 +100,43 @@ async def start():
95
  content="Please upload a zip file to begin!", accept={"application/zip": [".zip"]}
96
  ).send()
97
 
98
- zip_file = files[0] # Assuming only one file is uploaded
99
  uploaded_file_name = zip_file.name
100
 
 
 
 
101
  # Get the CHAINLIT_USER_FILES_DIR from environment variables
102
  user_files_dir = os.environ.get('CHAINLIT_USER_FILES_DIR', '/tmp/chainlit_user_files')
103
 
104
  # Ensure the user files directory exists
105
  os.makedirs(user_files_dir, exist_ok=True)
106
 
107
- # Define the destination path
108
- dest_path = os.path.join(user_files_dir, zip_file.name)
109
 
110
- # Copy the file to the destination
111
- shutil.copy(zip_file.path, dest_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  # Let the user know that the system is ready
114
  await cl.Message(content=f"`{zip_file.name}` uploaded successfully!").send()
115
 
116
  # Ask if the user wants to proceed with grading
117
  await cl.Message(content="Do you want to proceed with the grading? (yes/no)").send()
118
-
119
  async def process_grading():
120
  global uploaded_file_name
121
  if uploaded_file_name:
 
84
  import os
85
  import shutil
86
 
87
+ import os
88
+ import shutil
89
+ import chainlit as cl
90
+ from chainlit.types import AskFileResponse
91
+
92
  @cl.on_chat_start
93
  async def start():
94
  global uploaded_file_name
 
100
  content="Please upload a zip file to begin!", accept={"application/zip": [".zip"]}
101
  ).send()
102
 
103
+ zip_file: AskFileResponse = files[0] # Assuming only one file is uploaded
104
  uploaded_file_name = zip_file.name
105
 
106
+ # Print out the attributes of the zip_file object for debugging
107
+ print(f"zip_file attributes: {dir(zip_file)}")
108
+
109
  # Get the CHAINLIT_USER_FILES_DIR from environment variables
110
  user_files_dir = os.environ.get('CHAINLIT_USER_FILES_DIR', '/tmp/chainlit_user_files')
111
 
112
  # Ensure the user files directory exists
113
  os.makedirs(user_files_dir, exist_ok=True)
114
 
115
+ # Save the uploaded file directly to the user files directory
116
+ file_path = os.path.join(user_files_dir, zip_file.name)
117
 
118
+ # Attempt to read the file content and save it
119
+ try:
120
+ with open(file_path, "wb") as f:
121
+ f.write(zip_file.content) # This may need to be adjusted based on the attributes of zip_file
122
+ except AttributeError as e:
123
+ print(f"AttributeError: {e}")
124
+ # If zip_file.content doesn't exist, try another method
125
+ try:
126
+ with open(zip_file.path, "rb") as src_file:
127
+ with open(file_path, "wb") as dest_file:
128
+ shutil.copyfileobj(src_file, dest_file)
129
+ except Exception as e:
130
+ print(f"Error while copying file: {e}")
131
+ await cl.Message(content=f"Error while copying file: {e}").send()
132
+ return
133
 
134
  # Let the user know that the system is ready
135
  await cl.Message(content=f"`{zip_file.name}` uploaded successfully!").send()
136
 
137
  # Ask if the user wants to proceed with grading
138
  await cl.Message(content="Do you want to proceed with the grading? (yes/no)").send()
139
+
140
  async def process_grading():
141
  global uploaded_file_name
142
  if uploaded_file_name: