jschwab21 commited on
Commit
939b575
·
verified ·
1 Parent(s): 96c84ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -49
app.py CHANGED
@@ -1,54 +1,146 @@
1
  import gradio as gr
 
 
 
 
2
  import os
3
- from scenedetect import VideoManager, SceneManager
4
- from scenedetect.detectors import ContentDetector
5
-
6
- def save_and_display_video(video_file):
7
- if video_file is None:
8
- return None, "No video uploaded."
9
-
10
- try:
11
- if len(video_file) > 0:
12
- # Save the binary content to a file
13
- file_path = 'uploaded_video.mp4' # Assuming .mp4 for simplicity
14
- with open(file_path, 'wb') as f:
15
- f.write(video_file)
16
-
17
- scenes = find_scenes(file_path)
18
- scene_info = ', '.join([f"Start: {start.get_seconds()}, End: {end.get_seconds()}" for start, end in scenes])
19
- if scenes:
20
- return file_path, f"Video uploaded and displayed successfully. Scenes detected: {scene_info}"
21
- else:
22
- return file_path, "Video uploaded but no scenes were detected."
23
- else:
24
- return None, "Uploaded file is empty."
25
- except Exception as e:
26
- return None, f"An error occurred: {str(e)}"
27
-
28
- def find_scenes(video_path):
29
- video_manager = VideoManager([video_path])
30
- scene_manager = SceneManager()
31
- scene_manager.add_detector(ContentDetector(threshold=30))
32
- video_manager.set_downscale_factor()
33
-
34
- video_manager.start()
35
- scene_manager.detect_scenes(frame_source=video_manager)
36
-
37
- scene_list = scene_manager.get_scene_list(base_timecode=video_manager.get_base_timecode())
38
- video_manager.release()
39
-
40
- return scene_list
41
-
42
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  with gr.Column():
 
 
 
44
  video_file = gr.File(label="Upload Video File", type="binary", file_types=["video"], interactive=True)
45
- output_video = gr.Video()
46
- output_message = gr.Textbox(label="Output Message", lines=4)
47
- submit_button = gr.Button("Display Video")
48
- submit_button.click(
49
- fn=save_and_display_video,
50
- inputs=video_file,
51
- outputs=[output_video, output_message]
52
- )
53
 
54
- demo.launch()
 
1
  import gradio as gr
2
+ from video_processing import process_video, download_video, find_scenes, analyze_scenes
3
+ from gradio.themes.base import Base
4
+ from gradio.themes.utils import colors, fonts, sizes
5
+ from typing import Iterable
6
  import os
7
+
8
+ class CustomTheme(Base):
9
+ def __init__(
10
+ self,
11
+ *,
12
+ primary_hue: colors.Color | str = colors.orange,
13
+ secondary_hue: colors.Color | str = colors.orange,
14
+ neutral_hue: colors.Color | str = colors.gray,
15
+ spacing_size: sizes.Size | str = sizes.spacing_md,
16
+ radius_size: sizes.Size | str = sizes.radius_md,
17
+ text_size: sizes.Size | str = sizes.text_md,
18
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
19
+ fonts.GoogleFont("Sora"),
20
+ "ui-sans-serif",
21
+ "sans-serif",
22
+ ),
23
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
24
+ fonts.GoogleFont("Sora"),
25
+ "ui-monospace",
26
+ "monospace",
27
+ ),
28
+ ):
29
+ super().__init__(
30
+ primary_hue=primary_hue,
31
+ secondary_hue=secondary_hue,
32
+ neutral_hue=neutral_hue,
33
+ spacing_size=spacing_size,
34
+ radius_size=radius_size,
35
+ text_size=text_size,
36
+ font=font,
37
+ font_mono=font_mono,
38
+ )
39
+ super().set(
40
+ body_background_fill="radial-gradient(circle at center, rgba(235, 87, 38, 1) 0%, rgba(235, 87, 38, 0) 70%), radial-gradient(#eb5726 1px, transparent 1px)",
41
+ body_text_color="#282828",
42
+ block_background_fill="#ffffff",
43
+ block_title_text_color="#eb5726",
44
+ block_label_text_color="#eb5726",
45
+ button_primary_background_fill="#eb5726",
46
+ button_primary_text_color="#ffffff",
47
+ )
48
+
49
+ custom_theme = CustomTheme()
50
+
51
+ def display_results(video_url, video_file, description):
52
+ if video_url:
53
+ video_path = download_video(video_url)
54
+ elif video_file:
55
+ video_path = save_uploaded_file(video_file)
56
+ else:
57
+ return "No video provided", None
58
+
59
+ scenes = find_scenes(video_path)
60
+ if not scenes:
61
+ return "No scenes detected", None
62
+
63
+ best_scene = analyze_scenes(video_path, scenes, description)
64
+ final_clip = extract_best_scene(video_path, best_scene)
65
+ if final_clip:
66
+ output_dir = "output"
67
+ os.makedirs(output_dir, exist_ok=True)
68
+ final_clip_path = os.path.join(output_dir, f"{uuid.uuid4()}_final_clip.mp4")
69
+ final_clip.write_videofile(final_clip_path, codec='libx264', audio_codec='aac')
70
+ cleanup_temp_files()
71
+ return final_clip_path, final_clip_path
72
+ else:
73
+ return "No matching scene found", None
74
+
75
+
76
+ # Custom CSS for additional styling
77
+ css = """
78
+ body {
79
+ background-color: #ffffff;
80
+ background-image: radial-gradient(#eb5726 1px, transparent 1px);
81
+ background-size: 10px 10px;
82
+ background-repeat: repeat;
83
+ background-attachment: fixed;
84
+ }
85
+ #video_url {
86
+ background-color: #ffffff;
87
+ color: #282828;
88
+ border: 2px solid #eb5726;
89
+ }
90
+ #description {
91
+ background-color: #ffffff;
92
+ color: #282828;
93
+ border: 2px solid #eb5726;
94
+ }
95
+ #submit_button {
96
+ background-color: #eb5726;
97
+ color: #ffffff;
98
+ border: 2px solid #ffffff;
99
+ }
100
+ #submit_button:hover {
101
+ background-color: #f5986e;
102
+ color: #ffffff;
103
+ border: 2px solid #ffffff;
104
+ }
105
+ label[for="video_url"] {
106
+ color: #eb5726 !important;
107
+ }
108
+ label[for="description"] {
109
+ color: #eb5726 !important;
110
+ }
111
+ h3 {
112
+ color: #eb5726;
113
+ }
114
+ .centered-markdown {
115
+ text-align: center;
116
+ background-color: #ffffff;
117
+ padding: 10px;
118
+ }
119
+ #sickstadium-title {
120
+ font-size: 3em !important;
121
+ font-weight: bold;
122
+ text-transform: uppercase;
123
+ }
124
+ """
125
+
126
+ def save_uploaded_file(uploaded_file):
127
+ upload_dir = "uploaded_videos"
128
+ os.makedirs(upload_dir, exist_ok=True)
129
+ file_path = os.path.join(upload_dir, f"{uuid.uuid4()}.mp4")
130
+ with open(file_path, "wb") as f:
131
+ f.write(uploaded_file)
132
+ return file_path
133
+
134
+ with gr.Blocks(theme=custom_theme, css=css) as demo:
135
  with gr.Column():
136
+ gr.Markdown("# **Sickstadium AI**", elem_classes="centered-markdown", elem_id="sickstadium-title")
137
+ gr.Markdown("### Upload your videos. Find sick clips. Tell your truth.", elem_classes="centered-markdown")
138
+ video_url = gr.Textbox(label="Video URL:", elem_id="video_url")
139
  video_file = gr.File(label="Upload Video File", type="binary", file_types=["video"], interactive=True)
140
+ description = gr.Textbox(label="Describe your clip:", elem_id="description")
141
+ submit_button = gr.Button("Process Video", elem_id="submit_button")
142
+ video_output = gr.Video(label="Processed Video", elem_id="video_output")
143
+ download_output = gr.File(label="Download Processed Video", elem_id="download_output")
144
+ submit_button.click(fn=display_results, inputs=[video_url, video_file, description], outputs=[video_output, download_output])
 
 
 
145
 
146
+ demo.launch()