vericudebuget commited on
Commit
2e469cf
·
verified ·
1 Parent(s): 45059bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -102
app.py CHANGED
@@ -7,6 +7,9 @@ import cv2
7
  import random
8
  from PIL import Image
9
  import string
 
 
 
10
 
11
  # Initialize the Hugging Face API with the token
12
  api = HfApi(token=os.getenv("HF_API_TOKEN"))
@@ -49,49 +52,50 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
49
  }
50
 
51
  def update_index_file(new_metadata_path):
52
- index_file_path = "metadata/index-video.json"
53
- temp_dir = "temp"
54
- temp_index_path = os.path.join(temp_dir, "temp_index.json")
 
55
 
56
  try:
57
- # Try to download existing index file
58
- existing_content = api.download_file(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  repo_id="vericudebuget/ok4231",
60
  repo_type="space",
61
- path_in_repo=index_file_path
62
  )
63
- current_paths = existing_content.decode().strip()
64
-
65
- # If the file exists but is empty, initialize it
66
- if not current_paths:
67
- updated_paths = new_metadata_path
68
- else:
69
- # Split existing paths and add the new one
70
- paths_list = current_paths.split(';')
71
- if new_metadata_path not in paths_list:
72
- paths_list.append(new_metadata_path)
73
- updated_paths = ';'.join(paths_list)
74
-
75
- except Exception as e:
76
- # If file doesn't exist or can't be loaded, start with just the new path
77
- updated_paths = new_metadata_path
78
-
79
- # Create temporary file for updated index
80
- os.makedirs(temp_dir, exist_ok=True)
81
- with open(temp_index_path, "w") as f:
82
- f.write(updated_paths)
83
-
84
- # Upload updated index file
85
- api.upload_file(
86
- path_or_fileobj=temp_index_path,
87
- path_in_repo=index_file_path,
88
- repo_id="vericudebuget/ok4231",
89
- repo_type="space",
90
- )
91
 
92
- # Clean up temporary file
93
- if os.path.exists(temp_index_path):
94
- os.remove(temp_index_path)
 
95
 
96
  def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
97
  # Create temp paths
@@ -99,71 +103,72 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
99
  if not os.path.exists(temp_dir):
100
  os.makedirs(temp_dir)
101
 
102
- # Generate randomized filenames
103
- video_name = add_random_to_filename(original_video_name)
104
- video_path = os.path.join(temp_dir, video_name)
105
-
106
- base_name = os.path.splitext(video_name)[0]
107
- thumbnail_name = f"{base_name}_thumb.jpg"
108
- thumbnail_path = os.path.join(temp_dir, thumbnail_name)
109
-
110
- json_name = f"{base_name}-index.json"
111
- json_path = os.path.join(temp_dir, json_name)
112
-
113
- # Write the video content to a file
114
- with open(video_path, "wb") as f:
115
- f.write(video_file.read())
116
-
117
- # Handle thumbnail
118
- if custom_thumbnail:
119
- thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
120
- else:
121
- thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
122
-
123
- if not thumbnail_extracted:
124
- st.error("Failed to process thumbnail")
125
- return None
126
-
127
- # Upload the video
128
- video_location = f"videos/{video_name}"
129
- api.upload_file(
130
- path_or_fileobj=video_path,
131
- path_in_repo=video_location,
132
- repo_id="vericudebuget/ok4231",
133
- repo_type="space",
134
- )
135
-
136
- # Upload the thumbnail
137
- thumbnail_location = f"thumbnails/{thumbnail_name}"
138
- api.upload_file(
139
- path_or_fileobj=thumbnail_path,
140
- path_in_repo=thumbnail_location,
141
- repo_id="vericudebuget/ok4231",
142
- repo_type="space",
143
- )
144
-
145
- # Generate and upload metadata JSON
146
- metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
147
- with open(json_path, "w") as f:
148
- json.dump(metadata, f, indent=2)
149
-
150
- metadata_location = f"metadata/{json_name}"
151
- api.upload_file(
152
- path_or_fileobj=json_path,
153
- path_in_repo=metadata_location,
154
- repo_id="vericudebuget/ok4231",
155
- repo_type="space",
156
- )
157
-
158
- # Update the index file
159
- update_index_file(metadata_location)
160
-
161
- # Cleanup temp files
162
- for file_path in [video_path, thumbnail_path, json_path]:
163
- if os.path.exists(file_path):
164
- os.remove(file_path)
165
 
166
- return metadata
 
 
 
167
 
168
  # Streamlit app interface
169
  st.title("Upload your video")
@@ -202,6 +207,5 @@ if uploaded_video:
202
  if metadata:
203
  st.success("Upload completed successfully!")
204
  st.json(metadata)
205
-
206
  else:
207
  st.info("Please upload a video file to begin.")
 
7
  import random
8
  from PIL import Image
9
  import string
10
+ import subprocess
11
+ import glob
12
+ import shutil
13
 
14
  # Initialize the Hugging Face API with the token
15
  api = HfApi(token=os.getenv("HF_API_TOKEN"))
 
52
  }
53
 
54
  def update_index_file(new_metadata_path):
55
+ # Create a temporary directory for cloning
56
+ temp_dir = "temp_repo"
57
+ if os.path.exists(temp_dir):
58
+ shutil.rmtree(temp_dir)
59
 
60
  try:
61
+ # Clone the repository
62
+ subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
63
+
64
+ # Find all JSON files in the metadata directory
65
+ metadata_dir = os.path.join(temp_dir, 'metadata')
66
+ json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
67
+
68
+ # Create the paths string
69
+ base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
70
+ paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
71
+
72
+ # Add the new metadata path if it's not already there
73
+ new_metadata_filename = os.path.basename(new_metadata_path)
74
+ new_full_path = f"{base_url}{new_metadata_filename}"
75
+ if new_full_path not in paths:
76
+ paths.append(new_full_path)
77
+
78
+ # Create the index content
79
+ index_content = "{ " + "; ".join(paths) + "; }"
80
+
81
+ # Write to a temporary file
82
+ index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
83
+ os.makedirs(os.path.dirname(index_path), exist_ok=True)
84
+ with open(index_path, 'w') as f:
85
+ f.write(index_content)
86
+
87
+ # Upload the index file
88
+ api.upload_file(
89
+ path_or_fileobj=index_path,
90
+ path_in_repo="metadata/video-index.json",
91
  repo_id="vericudebuget/ok4231",
92
  repo_type="space",
 
93
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ finally:
96
+ # Clean up
97
+ if os.path.exists(temp_dir):
98
+ shutil.rmtree(temp_dir)
99
 
100
  def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
101
  # Create temp paths
 
103
  if not os.path.exists(temp_dir):
104
  os.makedirs(temp_dir)
105
 
106
+ try:
107
+ # Generate randomized filenames
108
+ video_name = add_random_to_filename(original_video_name)
109
+ video_path = os.path.join(temp_dir, video_name)
110
+
111
+ base_name = os.path.splitext(video_name)[0]
112
+ thumbnail_name = f"{base_name}_thumb.jpg"
113
+ thumbnail_path = os.path.join(temp_dir, thumbnail_name)
114
+
115
+ json_name = f"{base_name}-index.json"
116
+ json_path = os.path.join(temp_dir, json_name)
117
+
118
+ # Write the video content to a file
119
+ with open(video_path, "wb") as f:
120
+ f.write(video_file.read())
121
+
122
+ # Handle thumbnail
123
+ if custom_thumbnail:
124
+ thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
125
+ else:
126
+ thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
127
+
128
+ if not thumbnail_extracted:
129
+ st.error("Failed to process thumbnail")
130
+ return None
131
+
132
+ # Upload the video
133
+ video_location = f"videos/{video_name}"
134
+ api.upload_file(
135
+ path_or_fileobj=video_path,
136
+ path_in_repo=video_location,
137
+ repo_id="vericudebuget/ok4231",
138
+ repo_type="space",
139
+ )
140
+
141
+ # Upload the thumbnail
142
+ thumbnail_location = f"thumbnails/{thumbnail_name}"
143
+ api.upload_file(
144
+ path_or_fileobj=thumbnail_path,
145
+ path_in_repo=thumbnail_location,
146
+ repo_id="vericudebuget/ok4231",
147
+ repo_type="space",
148
+ )
149
+
150
+ # Generate and upload metadata JSON
151
+ metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
152
+ with open(json_path, "w") as f:
153
+ json.dump(metadata, f, indent=2)
154
+
155
+ metadata_location = f"metadata/{json_name}"
156
+ api.upload_file(
157
+ path_or_fileobj=json_path,
158
+ path_in_repo=metadata_location,
159
+ repo_id="vericudebuget/ok4231",
160
+ repo_type="space",
161
+ )
162
+
163
+ # Update the index file
164
+ update_index_file(metadata_location)
165
+
166
+ return metadata
 
 
167
 
168
+ finally:
169
+ # Cleanup temp files
170
+ if os.path.exists(temp_dir):
171
+ shutil.rmtree(temp_dir)
172
 
173
  # Streamlit app interface
174
  st.title("Upload your video")
 
207
  if metadata:
208
  st.success("Upload completed successfully!")
209
  st.json(metadata)
 
210
  else:
211
  st.info("Please upload a video file to begin.")