Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -107,52 +107,36 @@ def download_notebook_from_drive(drive_service, temp_dir):
|
|
| 107 |
request = drive_service.files().get_media(fileId=notebook_file_id)
|
| 108 |
notebook_path = os.path.join(temp_dir, 'video-to-bvh-converter.ipynb')
|
| 109 |
with open(notebook_path, 'wb') as f:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
f.write(downloader.stream)
|
| 113 |
-
|
| 114 |
# Add kernel specification to notebook
|
| 115 |
with open(notebook_path, 'r') as f:
|
| 116 |
notebook_content = json.load(f)
|
| 117 |
|
| 118 |
-
# Add proper kernel specification
|
| 119 |
notebook_content['metadata']['kernelspec'] = {
|
| 120 |
"display_name": "Python 3",
|
| 121 |
"language": "python",
|
| 122 |
"name": "python3"
|
| 123 |
}
|
| 124 |
|
| 125 |
-
# Ensure language_info is complete
|
| 126 |
notebook_content['metadata']['language_info'] = {
|
| 127 |
"name": "python",
|
| 128 |
"version": "3.10.12",
|
| 129 |
"mimetype": "text/x-python",
|
| 130 |
-
"codemirror_mode": {
|
| 131 |
-
"name": "ipython",
|
| 132 |
-
"version": 3
|
| 133 |
-
},
|
| 134 |
"pygments_lexer": "ipython3",
|
| 135 |
"nbconvert_exporter": "python",
|
| 136 |
"file_extension": ".py"
|
| 137 |
}
|
| 138 |
-
|
| 139 |
-
# Write back the modified notebook
|
| 140 |
with open(notebook_path, 'w') as f:
|
| 141 |
json.dump(notebook_content, f)
|
| 142 |
-
|
| 143 |
return notebook_path
|
| 144 |
except Exception as e:
|
| 145 |
st.error(f"Failed to download notebook from Drive: {e}")
|
| 146 |
raise
|
| 147 |
|
| 148 |
-
def check_kernel_status(api, notebook_slug):
|
| 149 |
-
try:
|
| 150 |
-
status_response = api.kernel_status(notebook_slug)
|
| 151 |
-
return status_response
|
| 152 |
-
except Exception as e:
|
| 153 |
-
st.error(f"Error checking kernel status: {e}")
|
| 154 |
-
return None
|
| 155 |
-
|
| 156 |
def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
| 157 |
try:
|
| 158 |
drive_service = setup_drive_service()
|
|
@@ -161,6 +145,11 @@ def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
|
| 161 |
kernel_file = os.path.join(temp_dir, 'kernel.ipynb')
|
| 162 |
shutil.copy(local_notebook_path, kernel_file)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
code_file = "kernel.ipynb"
|
| 165 |
kernel_type = "notebook"
|
| 166 |
|
|
@@ -173,8 +162,8 @@ def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
|
| 173 |
"enable_gpu": True,
|
| 174 |
"enable_internet": True,
|
| 175 |
"is_private": True,
|
| 176 |
-
"accelerator": "gpu",
|
| 177 |
-
"gpu_product": "T4x2",
|
| 178 |
"competition_sources": [],
|
| 179 |
"dataset_sources": [],
|
| 180 |
"kernel_sources": []
|
|
@@ -186,8 +175,8 @@ def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
|
| 186 |
cmd = f"kaggle kernels push -p {temp_dir}"
|
| 187 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 188 |
if result.returncode != 0:
|
| 189 |
-
st.error(f"
|
| 190 |
-
raise Exception
|
| 191 |
|
| 192 |
os.remove(local_notebook_path)
|
| 193 |
os.remove(kernel_file)
|
|
@@ -197,7 +186,18 @@ def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
|
| 197 |
except Exception as e:
|
| 198 |
st.error(f"Failed to push kernel: {str(e)}")
|
| 199 |
raise
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
def process_video(api, drive_service, video_file):
|
| 202 |
video_file_id = None
|
| 203 |
bvh_file_id = None
|
|
@@ -233,7 +233,7 @@ def process_video(api, drive_service, video_file):
|
|
| 233 |
|
| 234 |
while time.time() - start_time < overall_timeout:
|
| 235 |
try:
|
| 236 |
-
status_response =
|
| 237 |
current_status = status_response.status if hasattr(status_response, 'status') else 'unknown'
|
| 238 |
|
| 239 |
if current_status in ['queued', 'running']:
|
|
|
|
| 107 |
request = drive_service.files().get_media(fileId=notebook_file_id)
|
| 108 |
notebook_path = os.path.join(temp_dir, 'video-to-bvh-converter.ipynb')
|
| 109 |
with open(notebook_path, 'wb') as f:
|
| 110 |
+
f.write(request.execute()) # Directly write the downloaded content
|
| 111 |
+
|
|
|
|
|
|
|
| 112 |
# Add kernel specification to notebook
|
| 113 |
with open(notebook_path, 'r') as f:
|
| 114 |
notebook_content = json.load(f)
|
| 115 |
|
|
|
|
| 116 |
notebook_content['metadata']['kernelspec'] = {
|
| 117 |
"display_name": "Python 3",
|
| 118 |
"language": "python",
|
| 119 |
"name": "python3"
|
| 120 |
}
|
| 121 |
|
|
|
|
| 122 |
notebook_content['metadata']['language_info'] = {
|
| 123 |
"name": "python",
|
| 124 |
"version": "3.10.12",
|
| 125 |
"mimetype": "text/x-python",
|
| 126 |
+
"codemirror_mode": {"name": "ipython", "version": 3},
|
|
|
|
|
|
|
|
|
|
| 127 |
"pygments_lexer": "ipython3",
|
| 128 |
"nbconvert_exporter": "python",
|
| 129 |
"file_extension": ".py"
|
| 130 |
}
|
| 131 |
+
|
|
|
|
| 132 |
with open(notebook_path, 'w') as f:
|
| 133 |
json.dump(notebook_content, f)
|
| 134 |
+
|
| 135 |
return notebook_path
|
| 136 |
except Exception as e:
|
| 137 |
st.error(f"Failed to download notebook from Drive: {e}")
|
| 138 |
raise
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
def push_kaggle_kernel(api, temp_dir, notebook_slug):
|
| 141 |
try:
|
| 142 |
drive_service = setup_drive_service()
|
|
|
|
| 145 |
kernel_file = os.path.join(temp_dir, 'kernel.ipynb')
|
| 146 |
shutil.copy(local_notebook_path, kernel_file)
|
| 147 |
|
| 148 |
+
# Verify kernel spec
|
| 149 |
+
with open(kernel_file, 'r') as f:
|
| 150 |
+
metadata_content = json.load(f)['metadata']
|
| 151 |
+
st.write("Notebook metadata:", metadata_content) # Debug output
|
| 152 |
+
|
| 153 |
code_file = "kernel.ipynb"
|
| 154 |
kernel_type = "notebook"
|
| 155 |
|
|
|
|
| 162 |
"enable_gpu": True,
|
| 163 |
"enable_internet": True,
|
| 164 |
"is_private": True,
|
| 165 |
+
"accelerator": "gpu",
|
| 166 |
+
"gpu_product": "T4x2",
|
| 167 |
"competition_sources": [],
|
| 168 |
"dataset_sources": [],
|
| 169 |
"kernel_sources": []
|
|
|
|
| 175 |
cmd = f"kaggle kernels push -p {temp_dir}"
|
| 176 |
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
| 177 |
if result.returncode != 0:
|
| 178 |
+
st.error(f"Kernel push failed: {result.stderr}")
|
| 179 |
+
raise Exception(f"Push failed: {result.stderr}")
|
| 180 |
|
| 181 |
os.remove(local_notebook_path)
|
| 182 |
os.remove(kernel_file)
|
|
|
|
| 186 |
except Exception as e:
|
| 187 |
st.error(f"Failed to push kernel: {str(e)}")
|
| 188 |
raise
|
| 189 |
+
|
| 190 |
+
def check_kernel_exists(api, notebook_slug):
|
| 191 |
+
try:
|
| 192 |
+
kernels = api.kernels_list(mine=True, search=notebook_slug)
|
| 193 |
+
for kernel in kernels:
|
| 194 |
+
if kernel.ref == notebook_slug:
|
| 195 |
+
return True
|
| 196 |
+
return False
|
| 197 |
+
except Exception as e:
|
| 198 |
+
st.error(f"Kernel check failed: {e}")
|
| 199 |
+
return False
|
| 200 |
+
|
| 201 |
def process_video(api, drive_service, video_file):
|
| 202 |
video_file_id = None
|
| 203 |
bvh_file_id = None
|
|
|
|
| 233 |
|
| 234 |
while time.time() - start_time < overall_timeout:
|
| 235 |
try:
|
| 236 |
+
status_response = api.kernels_status(notebook_slug)
|
| 237 |
current_status = status_response.status if hasattr(status_response, 'status') else 'unknown'
|
| 238 |
|
| 239 |
if current_status in ['queued', 'running']:
|