jschwab21 commited on
Commit
90b9bf8
·
verified ·
1 Parent(s): 9312dbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -52,44 +52,37 @@ custom_theme = CustomTheme()
52
  def save_uploaded_file(uploaded_file):
53
  if uploaded_file is None:
54
  return None # Handle cases where no file was uploaded
 
 
 
 
 
55
 
56
- print(f"Received object type: {type(uploaded_file)}") # Debug: Check the object type
57
- print(f"Uploaded file content: {uploaded_file}") # Debug: Inspect the content
 
 
 
58
 
59
- # Handling file content based on its type
60
- if isinstance(uploaded_file, tuple):
61
- # If it's a tuple, it usually contains (filename, filedata)
62
- filename, filedata = uploaded_file
63
- file_path = os.path.join("uploaded_videos", filename)
64
- with open(file_path, "wb") as f:
65
- f.write(filedata)
66
- elif isinstance(uploaded_file, str):
67
- # If it's a string, assuming it's a file path
68
- file_path = uploaded_file
69
- else:
70
- raise ValueError("Unexpected file input type")
71
-
72
- os.makedirs(os.path.dirname(file_path), exist_ok=True)
73
  print(f"File saved to {file_path}, size: {os.path.getsize(file_path)} bytes")
74
  return file_path
75
 
76
-
77
  def display_results(video_url, video_file, description):
78
- final_clip_path = None
79
-
80
  if video_url:
 
81
  final_clip_path = process_video(video_url, description, is_url=True)
82
  elif video_file:
 
83
  video_file_path = save_uploaded_file(video_file)
84
  if video_file_path:
85
  final_clip_path = process_video(video_file_path, description, is_url=False)
86
  else:
87
  return "No file provided or file save error", None
88
-
89
- if final_clip_path:
90
- return final_clip_path, final_clip_path
91
  else:
92
- return "No matching scene found", None
 
 
93
 
94
  css = """
95
  body {
@@ -148,6 +141,7 @@ with gr.Blocks() as demo:
148
  submit_button = gr.Button("Process Video")
149
  video_output = gr.Video(label="Processed Video")
150
  download_output = gr.File(label="Download Processed Video")
 
151
  submit_button.click(
152
  fn=display_results,
153
  inputs=[video_url, video_file, description],
 
52
  def save_uploaded_file(uploaded_file):
53
  if uploaded_file is None:
54
  return None # Handle cases where no file was uploaded
55
+
56
+ filename, filedata = uploaded_file # Unpack the tuple into filename and binary data
57
+ upload_dir = "uploaded_videos"
58
+ os.makedirs(upload_dir, exist_ok=True) # Ensure the directory exists
59
+ file_path = os.path.join(upload_dir, filename)
60
 
61
+ # Write the binary data to a new file in the specified directory
62
+ with open(file_path, "wb") as f:
63
+ f.write(filedata)
64
+ f.flush()
65
+ os.fsync(f.fileno()) # Ensure all file data is flushed to disk
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  print(f"File saved to {file_path}, size: {os.path.getsize(file_path)} bytes")
68
  return file_path
69
 
70
+ # Define a function to process the video and display results
71
  def display_results(video_url, video_file, description):
 
 
72
  if video_url:
73
+ # Process video from URL
74
  final_clip_path = process_video(video_url, description, is_url=True)
75
  elif video_file:
76
+ # Save and process video from file upload
77
  video_file_path = save_uploaded_file(video_file)
78
  if video_file_path:
79
  final_clip_path = process_video(video_file_path, description, is_url=False)
80
  else:
81
  return "No file provided or file save error", None
 
 
 
82
  else:
83
+ return "No input provided", None
84
+
85
+ return final_clip_path, gr.Video.update(value=final_clip_path)
86
 
87
  css = """
88
  body {
 
141
  submit_button = gr.Button("Process Video")
142
  video_output = gr.Video(label="Processed Video")
143
  download_output = gr.File(label="Download Processed Video")
144
+
145
  submit_button.click(
146
  fn=display_results,
147
  inputs=[video_url, video_file, description],