Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,17 +67,23 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
|
|
| 67 |
|
| 68 |
def update_index_file(new_metadata_path):
|
| 69 |
temp_dir = "temp_repo"
|
|
|
|
|
|
|
| 70 |
if os.path.exists(temp_dir):
|
| 71 |
shutil.rmtree(temp_dir)
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 74 |
subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
|
|
|
|
|
|
|
| 75 |
metadata_dir = os.path.join(temp_dir, 'metadata')
|
| 76 |
json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
|
| 77 |
|
| 78 |
-
base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
|
| 79 |
paths = []
|
| 80 |
|
|
|
|
| 81 |
for f in json_files:
|
| 82 |
file_timestamp = datetime.now().isoformat() # Get the current timestamp
|
| 83 |
file_path = f"{base_url}{os.path.basename(f)}"
|
|
@@ -92,15 +98,19 @@ def update_index_file(new_metadata_path):
|
|
| 92 |
if not any(entry['url'] == new_full_path for entry in paths):
|
| 93 |
paths.append({"url": new_full_path, "timestamp": file_timestamp})
|
| 94 |
|
| 95 |
-
#
|
|
|
|
|
|
|
|
|
|
| 96 |
index_content = json.dumps(paths, indent=2)
|
| 97 |
|
|
|
|
| 98 |
index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
|
| 99 |
os.makedirs(os.path.dirname(index_path), exist_ok=True)
|
| 100 |
with open(index_path, 'w') as f:
|
| 101 |
f.write(index_content)
|
| 102 |
|
| 103 |
-
# Upload the updated index file
|
| 104 |
hf_api.upload_file(
|
| 105 |
path_or_fileobj=index_path,
|
| 106 |
path_in_repo="metadata/video-index.json",
|
|
@@ -109,6 +119,7 @@ def update_index_file(new_metadata_path):
|
|
| 109 |
)
|
| 110 |
|
| 111 |
finally:
|
|
|
|
| 112 |
if os.path.exists(temp_dir):
|
| 113 |
shutil.rmtree(temp_dir)
|
| 114 |
|
|
@@ -119,6 +130,11 @@ def create_subtitles(video_path): # Renamed from generate_subtitles
|
|
| 119 |
audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
|
| 120 |
audio.export(temp_audio.name, format='mp3', bitrate='128k')
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Generate subtitles using Groq
|
| 123 |
with open(temp_audio.name, 'rb') as audio_file:
|
| 124 |
translation = groq_client.audio.translations.create(
|
|
@@ -143,6 +159,7 @@ def create_subtitles(video_path): # Renamed from generate_subtitles
|
|
| 143 |
os.unlink(temp_audio.name) # Clean up temp file
|
| 144 |
return vtt_content
|
| 145 |
|
|
|
|
| 146 |
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None): # Renamed parameter
|
| 147 |
temp_dir = "temp"
|
| 148 |
if not os.path.exists(temp_dir):
|
|
@@ -252,9 +269,15 @@ if uploaded_video:
|
|
| 252 |
os.unlink(temp_video.name) # Clean up temp file
|
| 253 |
|
| 254 |
# Subtitle generation toggle, disabled if video is longer than 1 hour
|
| 255 |
-
should_generate_subs = st.toggle("Generate Subtitles", disabled=video_duration >
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
if video_duration > 3600 and should_generate_subs:
|
| 257 |
-
st.warning("
|
|
|
|
|
|
|
| 258 |
|
| 259 |
custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
|
| 260 |
|
|
|
|
| 67 |
|
| 68 |
def update_index_file(new_metadata_path):
|
| 69 |
temp_dir = "temp_repo"
|
| 70 |
+
|
| 71 |
+
# Remove existing temp directory if it exists
|
| 72 |
if os.path.exists(temp_dir):
|
| 73 |
shutil.rmtree(temp_dir)
|
| 74 |
|
| 75 |
try:
|
| 76 |
+
# Clone the Hugging Face repo
|
| 77 |
subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
|
| 78 |
+
|
| 79 |
+
# Find all existing JSON metadata files
|
| 80 |
metadata_dir = os.path.join(temp_dir, 'metadata')
|
| 81 |
json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
|
| 82 |
|
| 83 |
+
base_url = "https://huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
|
| 84 |
paths = []
|
| 85 |
|
| 86 |
+
# Collect existing metadata files with timestamps
|
| 87 |
for f in json_files:
|
| 88 |
file_timestamp = datetime.now().isoformat() # Get the current timestamp
|
| 89 |
file_path = f"{base_url}{os.path.basename(f)}"
|
|
|
|
| 98 |
if not any(entry['url'] == new_full_path for entry in paths):
|
| 99 |
paths.append({"url": new_full_path, "timestamp": file_timestamp})
|
| 100 |
|
| 101 |
+
# Sort the paths by timestamp in descending order (latest to oldest)
|
| 102 |
+
paths.sort(key=lambda x: x['timestamp'], reverse=True)
|
| 103 |
+
|
| 104 |
+
# Convert the paths list to a JSON format
|
| 105 |
index_content = json.dumps(paths, indent=2)
|
| 106 |
|
| 107 |
+
# Write the sorted index to 'video-index.json'
|
| 108 |
index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
|
| 109 |
os.makedirs(os.path.dirname(index_path), exist_ok=True)
|
| 110 |
with open(index_path, 'w') as f:
|
| 111 |
f.write(index_content)
|
| 112 |
|
| 113 |
+
# Upload the updated index file to the Hugging Face space
|
| 114 |
hf_api.upload_file(
|
| 115 |
path_or_fileobj=index_path,
|
| 116 |
path_in_repo="metadata/video-index.json",
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
finally:
|
| 122 |
+
# Clean up by removing the temp directory
|
| 123 |
if os.path.exists(temp_dir):
|
| 124 |
shutil.rmtree(temp_dir)
|
| 125 |
|
|
|
|
| 130 |
audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
|
| 131 |
audio.export(temp_audio.name, format='mp3', bitrate='128k')
|
| 132 |
|
| 133 |
+
# Check if the audio is silent
|
| 134 |
+
if audio.dBFS < -90: # Adjust the threshold as needed
|
| 135 |
+
os.unlink(temp_audio.name) # Clean up temp file
|
| 136 |
+
return "No audio detected in the video."
|
| 137 |
+
|
| 138 |
# Generate subtitles using Groq
|
| 139 |
with open(temp_audio.name, 'rb') as audio_file:
|
| 140 |
translation = groq_client.audio.translations.create(
|
|
|
|
| 159 |
os.unlink(temp_audio.name) # Clean up temp file
|
| 160 |
return vtt_content
|
| 161 |
|
| 162 |
+
|
| 163 |
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None): # Renamed parameter
|
| 164 |
temp_dir = "temp"
|
| 165 |
if not os.path.exists(temp_dir):
|
|
|
|
| 269 |
os.unlink(temp_video.name) # Clean up temp file
|
| 270 |
|
| 271 |
# Subtitle generation toggle, disabled if video is longer than 1 hour
|
| 272 |
+
should_generate_subs = st.toggle("Generate Subtitles", disabled=video_duration > 7200, value=True) # Renamed variable
|
| 273 |
+
if should_generate_subs:
|
| 274 |
+
st.warning("Subtitles will be generated automatically, and if the video is in another language, it will be translated automatically into English.")
|
| 275 |
+
if video_duration > 1180:
|
| 276 |
+
st.warning("Hey there! Just wanted to warn you that uploading pirated movies is not allowed.")
|
| 277 |
if video_duration > 3600 and should_generate_subs:
|
| 278 |
+
st.warning("Warning, for videos longer than an hour, generating subtitles will take some time! Please wait :)")
|
| 279 |
+
if video_duration > 7000:
|
| 280 |
+
st.warning("Now that's a long video. It will take a long time to upload. Make sure you have the right uploader details!")
|
| 281 |
|
| 282 |
custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
|
| 283 |
|