Spaces:
Running
Running
File size: 10,447 Bytes
019f3a5 436734f fd03c78 40e04c8 5d2e489 8fc67f3 2e469cf 7a2d05f 5c4b237 7a2d05f 5c4b237 8fc67f3 ac6e5d2 40e04c8 ac6e5d2 7d7426a 7a2d05f 7d7426a 7a2d05f fd03c78 ac6e5d2 7a2d05f fd03c78 b3f4c7c 2e469cf b3f4c7c 2e469cf 7a2d05f 2e469cf b3f4c7c 2e469cf b3f4c7c bf20bee 7a2d05f bf20bee 7a2d05f bf20bee ac6e5d2 4478b28 2e469cf 7d7426a 7a2d05f bf20bee 7d7426a 7a2d05f 2e469cf 7a2d05f 2e469cf 7a2d05f 2e469cf 7a2d05f 2e469cf 7a2d05f 2e469cf 40e04c8 2e469cf 2e0eb40 019f3a5 fd03c78 2e0eb40 7a2d05f 019f3a5 fd03c78 7a2d05f bf20bee 7a2d05f 40e04c8 fd03c78 7a2d05f fd03c78 40e04c8 bf20bee 40e04c8 fd03c78 ac6e5d2 fd03c78 7a2d05f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
import streamlit as st
from huggingface_hub import HfApi
import os
import json
from datetime import datetime
import cv2
import random
from PIL import Image
import string
import subprocess
import glob
import shutil
from groq import Groq
import tempfile
from pydub import AudioSegment
# Initialize the Hugging Face and Groq APIs
hf_api = HfApi(token=os.getenv("HF_API_TOKEN"))
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def generate_random_string(length=4):
return ''.join(random.choices(string.ascii_lowercase, k=length))
def add_random_to_filename(filename):
name, ext = os.path.splitext(filename)
random_string = generate_random_string()
return f"{name}-{random_string}{ext}"
def extract_thumbnail(video_path, thumbnail_path):
video = cv2.VideoCapture(video_path)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
random_frame = random.randint(0, total_frames - 1)
video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
success, frame = video.read()
if success:
cv2.imwrite(thumbnail_path, frame)
video.release()
return success
def save_custom_thumbnail(thumbnail_file, thumbnail_path):
img = Image.open(thumbnail_file)
img.save(thumbnail_path)
return True
def get_video_length(video_path):
video = cv2.VideoCapture(video_path)
fps = video.get(cv2.CAP_PROP_FPS)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
duration = int(total_frames / fps) if fps > 0 else 0
video.release()
return duration
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location, subtitle_location, duration):
return {
"fileName": video_name,
"title": title,
"description": description,
"uploader": uploader,
"uploadTimestamp": datetime.now().isoformat(),
"fileLocation": file_location,
"thumbnailLocation": thumbnail_location,
"subtitleLocation": subtitle_location,
"duration": duration,
"views": 0,
"likes": 0
}
def update_index_file(new_metadata_path):
temp_dir = "temp_repo"
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
try:
subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
metadata_dir = os.path.join(temp_dir, 'metadata')
json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
new_metadata_filename = os.path.basename(new_metadata_path)
new_full_path = f"{base_url}{new_metadata_filename}"
if new_full_path not in paths:
paths.append(new_full_path)
index_content = "{ " + "; ".join(paths) + "; }"
index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
os.makedirs(os.path.dirname(index_path), exist_ok=True)
with open(index_path, 'w') as f:
f.write(index_content)
hf_api.upload_file(
path_or_fileobj=index_path,
path_in_repo="metadata/video-index.json",
repo_id="vericudebuget/ok4231",
repo_type="space",
)
finally:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
def create_subtitles(video_path): # Renamed from generate_subtitles
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_audio:
# Convert video to mono 128kbps MP3
audio = AudioSegment.from_file(video_path)
audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
audio.export(temp_audio.name, format='mp3', bitrate='128k')
# Generate subtitles using Groq
with open(temp_audio.name, 'rb') as audio_file:
translation = groq_client.audio.translations.create(
file=(temp_audio.name, audio_file.read()),
model="whisper-large-v3",
response_format="verbose_json",
temperature=0.0
)
# Generate VTT content
vtt_content = "WEBVTT\n\n"
for segment in translation.segments:
start_time = segment['start']
end_time = segment['end']
text = segment['text'].strip()
start_time_vtt = f"{int(start_time // 3600):02}:{int((start_time % 3600) // 60):02}:{start_time % 60:06.3f}"
end_time_vtt = f"{int(end_time // 3600):02}:{int((end_time % 3600) // 60):02}:{end_time % 60:06.3f}"
vtt_content += f"{start_time_vtt} --> {end_time_vtt}\n{text}\n\n"
os.unlink(temp_audio.name) # Clean up temp file
return vtt_content
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None): # Renamed parameter
temp_dir = "temp"
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
try:
video_name = add_random_to_filename(original_video_name)
video_path = os.path.join(temp_dir, video_name)
base_name = os.path.splitext(video_name)[0]
thumbnail_name = f"{base_name}_thumb.jpg"
thumbnail_path = os.path.join(temp_dir, thumbnail_name)
json_name = f"{base_name}-index.json"
json_path = os.path.join(temp_dir, json_name)
with open(video_path, "wb") as f:
f.write(video_file.read())
if custom_thumbnail:
thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
else:
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
if not thumbnail_extracted:
st.error("Failed to process thumbnail")
return None
video_length = get_video_length(video_path)
# Generate and upload subtitles if requested and video is not too long
subtitle_location = ""
if should_generate_subs and video_length <= 3600: # 1 hour in seconds
try:
vtt_content = create_subtitles(video_path) # Using renamed function
subtitle_name = f"{base_name}.vtt"
subtitle_path = os.path.join(temp_dir, subtitle_name)
with open(subtitle_path, 'w') as f:
f.write(vtt_content)
subtitle_location = f"subtitles/{subtitle_name}"
hf_api.upload_file(
path_or_fileobj=subtitle_path,
path_in_repo=subtitle_location,
repo_id="vericudebuget/ok4231",
repo_type="space",
)
except Exception as e:
st.warning(f"Failed to generate subtitles: {str(e)}")
# Upload video and thumbnail
video_location = f"videos/{video_name}"
hf_api.upload_file(
path_or_fileobj=video_path,
path_in_repo=video_location,
repo_id="vericudebuget/ok4231",
repo_type="space",
)
thumbnail_location = f"thumbnails/{thumbnail_name}"
hf_api.upload_file(
path_or_fileobj=thumbnail_path,
path_in_repo=thumbnail_location,
repo_id="vericudebuget/ok4231",
repo_type="space",
)
# Generate and upload metadata
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location, subtitle_location, video_length)
with open(json_path, "w") as f:
json.dump(metadata, f, indent=2)
metadata_location = f"metadata/{json_name}"
hf_api.upload_file(
path_or_fileobj=json_path,
path_in_repo=metadata_location,
repo_id="vericudebuget/ok4231",
repo_type="space",
)
update_index_file(metadata_location)
return metadata
finally:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
# Streamlit app interface
st.title("Upload your video")
st.markdown("---")
uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov", "webm", "mkv"])
if uploaded_video:
with st.form("video_details"):
st.write("Video Details")
title = st.text_input("Title", placeholder="Enter video title")
description = st.text_area("Description", placeholder="Enter video description")
uploader = st.text_input("Uploader Name", placeholder="Enter your name")
# Create a temporary file to get video duration
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
temp_video.write(uploaded_video.getvalue())
video_duration = get_video_length(temp_video.name)
os.unlink(temp_video.name) # Clean up temp file
# Subtitle generation toggle, disabled if video is longer than 1 hour
should_generate_subs = st.toggle("Generate Subtitles", disabled=video_duration > 3600, value=True) # Renamed variable
if video_duration > 3600 and should_generate_subs:
st.warning("Subtitle generation is disabled for videos longer than 1 hour.")
custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
submit_button = st.form_submit_button("Upload Video")
if submit_button:
if not title or not uploader:
st.error("Please fill in the title and uploader name.")
else:
with st.spinner("Uploading video, generating thumbnail and metadata... This may take some time. Please wait."):
metadata = upload_video_to_hf(
uploaded_video,
uploaded_video.name,
title,
description,
uploader,
should_generate_subs, # Using renamed variable
custom_thumbnail
)
if metadata:
st.success("Upload completed successfully!")
st.json(metadata)
else:
st.info("Please upload a video file to begin.") |