Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
import yaml
|
6 |
+
import pandas as pd
|
7 |
+
from datetime import datetime
|
8 |
+
from moviepy.editor import VideoFileClip
|
9 |
+
|
10 |
+
# Function to process the video
|
11 |
+
def process_video(video):
|
12 |
+
start_time = time.time()
|
13 |
+
|
14 |
+
# Save the uploaded video file
|
15 |
+
video_path = os.path.join("uploads", video.name)
|
16 |
+
video.save(video_path)
|
17 |
+
|
18 |
+
# Extract video details
|
19 |
+
video_clip = VideoFileClip(video_path)
|
20 |
+
video_duration = video_clip.duration
|
21 |
+
memory_usage = os.path.getsize(video_path) # in bytes
|
22 |
+
process_time = time.time() - start_time
|
23 |
+
upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
24 |
+
|
25 |
+
# Collect the log
|
26 |
+
log = {
|
27 |
+
"filename": video.name,
|
28 |
+
"collect_date_time": upload_time,
|
29 |
+
"video_duration": video_duration,
|
30 |
+
"process_time": process_time,
|
31 |
+
"memory_usage": memory_usage
|
32 |
+
}
|
33 |
+
|
34 |
+
# Save log in different formats
|
35 |
+
save_log(log)
|
36 |
+
|
37 |
+
return video_path, log
|
38 |
+
|
39 |
+
def save_log(log):
|
40 |
+
# Save to CSV
|
41 |
+
csv_file = "logs.csv"
|
42 |
+
df = pd.DataFrame([log])
|
43 |
+
if os.path.exists(csv_file):
|
44 |
+
df.to_csv(csv_file, mode='a', header=False, index=False)
|
45 |
+
else:
|
46 |
+
df.to_csv(csv_file, index=False)
|
47 |
+
|
48 |
+
# Save to JSON
|
49 |
+
json_file = "logs.json"
|
50 |
+
if os.path.exists(json_file):
|
51 |
+
with open(json_file, 'r') as f:
|
52 |
+
logs = json.load(f)
|
53 |
+
logs.append(log)
|
54 |
+
with open(json_file, 'w') as f:
|
55 |
+
json.dump(logs, f, indent=4)
|
56 |
+
else:
|
57 |
+
with open(json_file, 'w') as f:
|
58 |
+
json.dump([log], f, indent=4)
|
59 |
+
|
60 |
+
# Save to YAML
|
61 |
+
yaml_file = "logs.yaml"
|
62 |
+
if os.path.exists(yaml_file):
|
63 |
+
with open(yaml_file, 'r') as f:
|
64 |
+
logs = yaml.safe_load(f)
|
65 |
+
logs.append(log)
|
66 |
+
with open(yaml_file, 'w') as f:
|
67 |
+
yaml.dump(logs, f)
|
68 |
+
else:
|
69 |
+
with open(yaml_file, 'w') as f:
|
70 |
+
yaml.dump([log], f)
|
71 |
+
|
72 |
+
def display_logs():
|
73 |
+
logs = []
|
74 |
+
if os.path.exists("logs.json"):
|
75 |
+
with open("logs.json", 'r') as f:
|
76 |
+
logs = json.load(f)
|
77 |
+
return logs
|
78 |
+
|
79 |
+
# Create Gradio interface
|
80 |
+
upload_video = gr.Interface(
|
81 |
+
fn=process_video,
|
82 |
+
inputs=gr.inputs.File(label="Upload Video"),
|
83 |
+
outputs=[gr.outputs.Video(label="Processed Video"), gr.outputs.JSON(label="Log Details")],
|
84 |
+
live=True
|
85 |
+
)
|
86 |
+
|
87 |
+
view_logs = gr.Interface(
|
88 |
+
fn=display_logs,
|
89 |
+
inputs=None,
|
90 |
+
outputs=gr.outputs.JSON(label="Logs History")
|
91 |
+
)
|
92 |
+
|
93 |
+
app = gr.TabbedInterface(
|
94 |
+
[upload_video, view_logs],
|
95 |
+
["Upload and Process Video", "View Logs"]
|
96 |
+
)
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
app.launch()
|