CoolFace
Apppublic

vericudebuget/server-data

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py311 linesDownload Raw Back to root
1import streamlit as st2from huggingface_hub import HfApi3import os4import json5from datetime import datetime6import cv27import random8from PIL import Image9import string10import subprocess11import glob12import shutil13from groq import Groq14import tempfile15from pydub import AudioSegment16 17# Initialize the Hugging Face and Groq APIs18hf_api = HfApi(token=os.getenv("HF_API_TOKEN"))19groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))20 21def generate_random_string(length=4):22    return ''.join(random.choices(string.ascii_lowercase, k=length))23 24def add_random_to_filename(filename):25    name, ext = os.path.splitext(filename)26    random_string = generate_random_string()27    return f"{name}-{random_string}{ext}"28 29def extract_thumbnail(video_path, thumbnail_path):30    video = cv2.VideoCapture(video_path)31    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))32    random_frame = random.randint(0, total_frames - 1)33    video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)34    success, frame = video.read()35    if success:36        cv2.imwrite(thumbnail_path, frame)37    video.release()38    return success39 40def save_custom_thumbnail(thumbnail_file, thumbnail_path):41    img = Image.open(thumbnail_file)42    img.save(thumbnail_path)43    return True44 45def get_video_length(video_path):46    video = cv2.VideoCapture(video_path)47    fps = video.get(cv2.CAP_PROP_FPS)48    total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))49    duration = int(total_frames / fps) if fps > 0 else 050    video.release()51    return duration52 53def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location, subtitle_location, duration):54    return {55        "fileName": video_name,56        "title": title,57        "description": description,58        "uploader": uploader,59        "uploadTimestamp": datetime.now().isoformat(),60        "fileLocation": file_location,61        "thumbnailLocation": thumbnail_location,62        "subtitleLocation": subtitle_location,63        "duration": duration,64        "views": 0,65        "likes": 066    }67 68def update_index_file(new_metadata_path):69    temp_dir = "temp_repo"70    71    # Remove existing temp directory if it exists72    if os.path.exists(temp_dir):73        shutil.rmtree(temp_dir)74    75    try:76        77        # Clone the Hugging Face repo78        subprocess.run('GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/spaces/vericudebuget/ok4231 ' + temp_dir, 79               shell=True, 80               check=True)81        82        # Find all existing JSON metadata files83        metadata_dir = os.path.join(temp_dir, 'metadata')84        json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))85        86        base_url = "https://huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"87        paths = []88        89        # Collect existing metadata files with timestamps90        for f in json_files:91            file_timestamp = datetime.now().isoformat()  # Get the current timestamp92            file_path = f"{base_url}{os.path.basename(f)}"93            paths.append({"url": file_path, "timestamp": file_timestamp})94        95        # Add the new metadata file with the current timestamp96        new_metadata_filename = os.path.basename(new_metadata_path)97        new_full_path = f"{base_url}{new_metadata_filename}"98        file_timestamp = datetime.now().isoformat()  # Get timestamp for the new metadata file99        100        # Check if the new file is already in the list, if not, add it101        if not any(entry['url'] == new_full_path for entry in paths):102            paths.append({"url": new_full_path, "timestamp": file_timestamp})103        104        # Sort the paths by timestamp in descending order (latest to oldest)105        paths.sort(key=lambda x: x['timestamp'], reverse=True)106        107        # Convert the paths list to a JSON format108        index_content = json.dumps(paths, indent=2)109        110        # Write the sorted index to 'video-index.json'111        index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')112        os.makedirs(os.path.dirname(index_path), exist_ok=True)113        with open(index_path, 'w') as f:114            f.write(index_content)115        116        # Upload the updated index file to the Hugging Face space117        hf_api.upload_file(118            path_or_fileobj=index_path,119            path_in_repo="metadata/video-index.json",120            repo_id="vericudebuget/ok4231",121            repo_type="space",122        )123    124    finally:125        # Clean up by removing the temp directory126        if os.path.exists(temp_dir):127            shutil.rmtree(temp_dir)128 129def create_subtitles(video_path):  # Renamed from generate_subtitles130    with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_audio:131        # Convert video to mono 128kbps MP3132        audio = AudioSegment.from_file(video_path)133        audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)134        audio.export(temp_audio.name, format='mp3', bitrate='128k')135 136        # Generate subtitles using Groq137        with open(temp_audio.name, 'rb') as audio_file:138            translation = groq_client.audio.translations.create(139                file=(temp_audio.name, audio_file.read()),140                model="whisper-large-v3",141                response_format="verbose_json",142                temperature=0.0143            )144    145    # Generate VTT content146    vtt_content = "WEBVTT\n\n"147    for segment in translation.segments:148        start_time = segment['start']149        end_time = segment['end']150        text = segment['text'].strip()151        152        start_time_vtt = f"{int(start_time // 3600):02}:{int((start_time % 3600) // 60):02}:{start_time % 60:06.3f}"153        end_time_vtt = f"{int(end_time // 3600):02}:{int((end_time % 3600) // 60):02}:{end_time % 60:06.3f}"154        155        vtt_content += f"{start_time_vtt} --> {end_time_vtt}\n{text}\n\n"156    157    os.unlink(temp_audio.name)  # Clean up temp file158    return vtt_content159 160 161def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None):162    temp_dir = "temp"163    if not os.path.exists(temp_dir):164        os.makedirs(temp_dir)165    166    try:167        video_name = add_random_to_filename(original_video_name)168        video_path = os.path.join(temp_dir, video_name)169        170        base_name = os.path.splitext(video_name)[0]171        thumbnail_name = f"{base_name}_thumb.jpg"172        thumbnail_path = os.path.join(temp_dir, thumbnail_name)173        174        json_name = f"{base_name}-index.json"175        json_path = os.path.join(temp_dir, json_name)176        177        with open(video_path, "wb") as f:178            f.write(video_file.read())179        180        if custom_thumbnail:181            thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)182        else:183            thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)184        185        if not thumbnail_extracted:186            st.error("Failed to process thumbnail")187            return None188        189        video_length = get_video_length(video_path)190        191        # Analyze audio level192        audio = AudioSegment.from_file(video_path)193        audio_dBFS = audio.dBFS194        195        # Generate and upload subtitles if requested and video is not too long196        subtitle_location = ""197        if should_generate_subs and video_length <= 3600:  # 1 hour in seconds198            if audio_dBFS < -90:199                subtitle_location = ""  # Set to empty if audio is too quiet200            else:201                try:202                    vtt_content = create_subtitles(video_path)  # Using renamed function203                    subtitle_name = f"{base_name}.vtt"204                    subtitle_path = os.path.join(temp_dir, subtitle_name)205                    206                    with open(subtitle_path, 'w') as f:207                        f.write(vtt_content)208                    209                    subtitle_location = f"subtitles/{subtitle_name}"210                    hf_api.upload_file(211                        path_or_fileobj=subtitle_path,212                        path_in_repo=subtitle_location,213                        repo_id="vericudebuget/ok4231",214                        repo_type="space",215                    )216                except Exception as e:217                    st.warning(f"Failed to generate subtitles: {str(e)}")218 219        # Upload video and thumbnail220        video_location = f"videos/{video_name}"221        hf_api.upload_file(222            path_or_fileobj=video_path,223            path_in_repo=video_location,224            repo_id="vericudebuget/ok4231",225            repo_type="space",226        )227        228        thumbnail_location = f"thumbnails/{thumbnail_name}"229        hf_api.upload_file(230            path_or_fileobj=thumbnail_path,231            path_in_repo=thumbnail_location,232            repo_id="vericudebuget/ok4231",233            repo_type="space",234        )235        236        # Generate and upload metadata237        metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location, subtitle_location, video_length)238        with open(json_path, "w") as f:239            json.dump(metadata, f, indent=2)240        241        metadata_location = f"metadata/{json_name}"242        hf_api.upload_file(243            path_or_fileobj=json_path,244            path_in_repo=metadata_location,245            repo_id="vericudebuget/ok4231",246            repo_type="space",247        )248        249        update_index_file(metadata_location)250        251        return metadata252    253    finally:254        if os.path.exists(temp_dir):255            shutil.rmtree(temp_dir)256 257# Streamlit app interface258st.title("Upload your video")259st.markdown("---")260 261uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov", "webm", "mkv"])262 263if uploaded_video:264    with st.form("video_details"):265        st.write("Video Details")266        title = st.text_input("Title", placeholder="Enter video title")267        description = st.text_area("Description", placeholder="Enter video description")268        uploader = st.text_input("Uploader Name", placeholder="Enter your name")269        270        # Create a temporary file to get video duration271        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:272            temp_video.write(uploaded_video.getvalue())273            video_duration = get_video_length(temp_video.name)274        os.unlink(temp_video.name)  # Clean up temp file275        276        # Subtitle generation toggle, disabled if video is longer than 2 hours277        should_generate_subs = st.toggle("Generate Subtitles. - If enabled, the subtitles will automatically be translated into English.", disabled=video_duration > 7200, value=True)  # Renamed variable278        279        280        if video_duration > 1180:281            st.warning("Hey there! Just wanted to warn you that uploading pirated movies is not allowed.")282        283        if video_duration > 3600 and should_generate_subs:284            st.warning("Warning, for videos longer than an hour, generating subtitles will take some time! Please wait :)")285        286        if video_duration > 7000:287            st.warning("Now that's a long video. It will take a long time to upload. Make sure you have the right uploader details!")288        289        custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])290        291        submit_button = st.form_submit_button("Upload Video")292        293        if submit_button:294            if not title or not uploader:295                st.error("Please fill in the title and uploader name.")296            else:297                with st.spinner("Uploading video, generating thumbnail and metadata... This may take some time. Please wait."):298                    metadata = upload_video_to_hf(299                        uploaded_video, 300                        uploaded_video.name, 301                        title, 302                        description, 303                        uploader,304                        should_generate_subs,  # Using renamed variable305                        custom_thumbnail306                    )307                    if metadata:308                        st.success("Upload completed successfully!")309                        st.json(metadata)310else:311    st.info("Please upload a video file to begin.")