vericudebuget commited on
Commit
fd03c78
·
verified ·
1 Parent(s): c8cd9cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -16
app.py CHANGED
@@ -1,14 +1,29 @@
1
  import streamlit as st
2
  from huggingface_hub import HfApi
3
  import os
 
 
4
 
5
  # Initialize the Hugging Face API with the token
6
  api = HfApi(token=os.getenv("HF_API_TOKEN"))
7
 
8
- # Function to handle video uploads to Hugging Face
9
- def upload_video_to_hf(video_file, video_name):
 
 
 
 
 
 
 
 
 
 
 
10
  # Save the video temporarily to a file
11
  video_path = os.path.join("temp", video_name)
 
 
12
 
13
  # Ensure the temp directory exists
14
  if not os.path.exists("temp"):
@@ -17,29 +32,62 @@ def upload_video_to_hf(video_file, video_name):
17
  # Write the video content to a file
18
  with open(video_path, "wb") as f:
19
  f.write(video_file.read())
20
-
21
  # Upload the video to Hugging Face
 
22
  api.upload_file(
23
  path_or_fileobj=video_path,
24
- path_in_repo=f"videos/{video_name}",
25
  repo_id="vericudebuget/ok4231",
26
  repo_type="space",
27
  )
28
-
29
- return f"Uploaded {video_name} successfully to Hugging Face!"
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Streamlit app interface
32
- st.title("Video Uploader to Hugging Face")
 
33
 
34
  # File uploader for video input
35
- uploaded_video = st.file_uploader("Upload your video", type=["mp4", "avi", "mov"])
36
 
37
  if uploaded_video:
38
- # Show the name of the uploaded video
39
- st.write(f"Uploaded video: {uploaded_video.name}")
40
-
41
- # Upload the video when the button is clicked
42
- if st.button("Upload to Hugging Face"):
43
- with st.spinner("Uploading..."):
44
- result = upload_video_to_hf(uploaded_video, uploaded_video.name)
45
- st.success(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from huggingface_hub import HfApi
3
  import os
4
+ import json
5
+ from datetime import datetime
6
 
7
  # Initialize the Hugging Face API with the token
8
  api = HfApi(token=os.getenv("HF_API_TOKEN"))
9
 
10
+ def generate_metadata(video_name, title, description, uploader, file_location):
11
+ return {
12
+ "fileName": video_name,
13
+ "title": title,
14
+ "description": description,
15
+ "uploader": uploader,
16
+ "uploadTimestamp": datetime.now().isoformat(),
17
+ "fileLocation": file_location,
18
+ "views": 0,
19
+ "likes": 0
20
+ }
21
+
22
+ def upload_video_to_hf(video_file, video_name, title, description, uploader):
23
  # Save the video temporarily to a file
24
  video_path = os.path.join("temp", video_name)
25
+ json_name = f"{os.path.splitext(video_name)[0]}-index.json"
26
+ json_path = os.path.join("temp", json_name)
27
 
28
  # Ensure the temp directory exists
29
  if not os.path.exists("temp"):
 
32
  # Write the video content to a file
33
  with open(video_path, "wb") as f:
34
  f.write(video_file.read())
35
+
36
  # Upload the video to Hugging Face
37
+ file_location = f"videos/{video_name}"
38
  api.upload_file(
39
  path_or_fileobj=video_path,
40
+ path_in_repo=file_location,
41
  repo_id="vericudebuget/ok4231",
42
  repo_type="space",
43
  )
44
+
45
+ # Generate and upload metadata JSON
46
+ metadata = generate_metadata(video_name, title, description, uploader, file_location)
47
+ with open(json_path, "w") as f:
48
+ json.dump(metadata, f, indent=2)
49
+
50
+ api.upload_file(
51
+ path_or_fileobj=json_path,
52
+ path_in_repo=f"metadata/{json_name}",
53
+ repo_id="vericudebuget/ok4231",
54
+ repo_type="space",
55
+ )
56
+
57
+ return metadata
58
 
59
  # Streamlit app interface
60
+ st.title("Upload your video")
61
+ st.markdown("---")
62
 
63
  # File uploader for video input
64
+ uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov"])
65
 
66
  if uploaded_video:
67
+ # Show the video details form
68
+ with st.form("video_details"):
69
+ st.write("Video Details")
70
+ title = st.text_input("Title", placeholder="Enter video title")
71
+ description = st.text_area("Description", placeholder="Enter video description")
72
+ uploader = st.text_input("Uploader Name", placeholder="Enter your name")
73
+
74
+ # Upload button within the form
75
+ submit_button = st.form_submit_button("Upload Video")
76
+
77
+ if submit_button:
78
+ if not title or not uploader:
79
+ st.error("Please fill in the title and uploader name.")
80
+ else:
81
+ with st.spinner("Uploading video and generating metadata..."):
82
+ metadata = upload_video_to_hf(
83
+ uploaded_video,
84
+ uploaded_video.name,
85
+ title,
86
+ description,
87
+ uploader
88
+ )
89
+ st.success("Upload completed successfully!")
90
+ st.json(metadata)
91
+
92
+ else:
93
+ st.info("Please upload a video file to begin.")