File size: 3,076 Bytes
019f3a5
436734f
 
fd03c78
 
5c4b237
4478b28
5ff2b1d
5c4b237
fd03c78
 
 
 
 
 
 
 
 
 
 
 
 
019f3a5
 
fd03c78
 
4478b28
019f3a5
 
 
 
 
436734f
019f3a5
fd03c78
436734f
fd03c78
436734f
 
fd03c78
436734f
 
 
fd03c78
 
 
 
 
 
 
 
 
 
 
 
 
 
2e0eb40
019f3a5
fd03c78
 
2e0eb40
019f3a5
fd03c78
019f3a5
 
fd03c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from huggingface_hub import HfApi
import os
import json
from datetime import datetime

# Initialize the Hugging Face API with the token
api = HfApi(token=os.getenv("HF_API_TOKEN"))

def generate_metadata(video_name, title, description, uploader, file_location):
    return {
        "fileName": video_name,
        "title": title,
        "description": description,
        "uploader": uploader,
        "uploadTimestamp": datetime.now().isoformat(),
        "fileLocation": file_location,
        "views": 0,
        "likes": 0
    }

def upload_video_to_hf(video_file, video_name, title, description, uploader):
    # Save the video temporarily to a file
    video_path = os.path.join("temp", video_name)
    json_name = f"{os.path.splitext(video_name)[0]}-index.json"
    json_path = os.path.join("temp", json_name)
    
    # Ensure the temp directory exists
    if not os.path.exists("temp"):
        os.makedirs("temp")
    
    # Write the video content to a file
    with open(video_path, "wb") as f:
        f.write(video_file.read())
    
    # Upload the video to Hugging Face
    file_location = f"videos/{video_name}"
    api.upload_file(
        path_or_fileobj=video_path,
        path_in_repo=file_location,
        repo_id="vericudebuget/ok4231",
        repo_type="space",
    )
    
    # Generate and upload metadata JSON
    metadata = generate_metadata(video_name, title, description, uploader, file_location)
    with open(json_path, "w") as f:
        json.dump(metadata, f, indent=2)
    
    api.upload_file(
        path_or_fileobj=json_path,
        path_in_repo=f"metadata/{json_name}",
        repo_id="vericudebuget/ok4231",
        repo_type="space",
    )
    
    return metadata

# Streamlit app interface
st.title("Upload your video")
st.markdown("---")

# File uploader for video input
uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov"])

if uploaded_video:
    # Show the video details form
    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")
        
        # Upload button within the form
        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 and generating metadata..."):
                    metadata = upload_video_to_hf(
                        uploaded_video, 
                        uploaded_video.name, 
                        title, 
                        description, 
                        uploader
                    )
                    st.success("Upload completed successfully!")
                    st.json(metadata)

else:
    st.info("Please upload a video file to begin.")