TAneKAnz's picture
create app.py
cdb3457 verified
raw
history blame
No virus
2.57 kB
import gradio as gr
import os
import time
import json
import yaml
import pandas as pd
from datetime import datetime
from moviepy.editor import VideoFileClip
# Function to process the video
def process_video(video):
start_time = time.time()
# Save the uploaded video file
video_path = os.path.join("uploads", video.name)
video.save(video_path)
# Extract video details
video_clip = VideoFileClip(video_path)
video_duration = video_clip.duration
memory_usage = os.path.getsize(video_path) # in bytes
process_time = time.time() - start_time
upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Collect the log
log = {
"filename": video.name,
"collect_date_time": upload_time,
"video_duration": video_duration,
"process_time": process_time,
"memory_usage": memory_usage
}
# Save log in different formats
save_log(log)
return video_path, log
def save_log(log):
# Save to CSV
csv_file = "logs.csv"
df = pd.DataFrame([log])
if os.path.exists(csv_file):
df.to_csv(csv_file, mode='a', header=False, index=False)
else:
df.to_csv(csv_file, index=False)
# Save to JSON
json_file = "logs.json"
if os.path.exists(json_file):
with open(json_file, 'r') as f:
logs = json.load(f)
logs.append(log)
with open(json_file, 'w') as f:
json.dump(logs, f, indent=4)
else:
with open(json_file, 'w') as f:
json.dump([log], f, indent=4)
# Save to YAML
yaml_file = "logs.yaml"
if os.path.exists(yaml_file):
with open(yaml_file, 'r') as f:
logs = yaml.safe_load(f)
logs.append(log)
with open(yaml_file, 'w') as f:
yaml.dump(logs, f)
else:
with open(yaml_file, 'w') as f:
yaml.dump([log], f)
def display_logs():
logs = []
if os.path.exists("logs.json"):
with open("logs.json", 'r') as f:
logs = json.load(f)
return logs
# Create Gradio interface
upload_video = gr.Interface(
fn=process_video,
inputs=gr.inputs.File(label="Upload Video"),
outputs=[gr.outputs.Video(label="Processed Video"), gr.outputs.JSON(label="Log Details")],
live=True
)
view_logs = gr.Interface(
fn=display_logs,
inputs=None,
outputs=gr.outputs.JSON(label="Logs History")
)
app = gr.TabbedInterface(
[upload_video, view_logs],
["Upload and Process Video", "View Logs"]
)
if __name__ == "__main__":
app.launch()