AtzePengg commited on
Commit
37617ef
·
1 Parent(s): 8343f2c

Add permanent Bear video example with direct URL from Hugging Face

Browse files
Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -34,26 +34,8 @@ load_dotenv()
34
  # Get default API key from environment (will be '' if not set)
35
  DEFAULT_API_KEY = os.getenv("OPENAI_API_KEY", "")
36
 
37
- def download_video_from_url(url):
38
- try:
39
- # Create a temporary file
40
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
41
- temp_filename = temp_file.name
42
- temp_file.close()
43
-
44
- # Download the video
45
- response = requests.get(url, stream=True)
46
- if response.status_code == 200:
47
- with open(temp_filename, 'wb') as f:
48
- for chunk in response.iter_content(chunk_size=8192):
49
- f.write(chunk)
50
- return temp_filename
51
- else:
52
- print(f"Failed to download video: {response.status_code}")
53
- return None
54
- except Exception as e:
55
- print(f"Error downloading video: {e}")
56
- return None
57
 
58
  def process_frame(frame_path, style_prompt, api_key):
59
  """Process a single frame with GPT-4o analysis and DALL-E 3 generation"""
@@ -136,7 +118,13 @@ def stylize_video(video_path, style_prompt, api_key):
136
 
137
  # Save the input video to a temporary file
138
  if isinstance(video_path, str):
139
- if os.path.exists(video_path):
 
 
 
 
 
 
140
  # It's a file path, copy it
141
  shutil.copy(video_path, input_filename)
142
  else:
@@ -219,28 +207,22 @@ def stylize_video(video_path, style_prompt, api_key):
219
  print(f"Error: {str(e)}\n{traceback_str}")
220
  return None, f"Error: {str(e)}"
221
 
222
- def use_sample_bear_video():
223
- """Function to use a sample bear video"""
224
- # Try using a shorter, more reliable sample video URL
225
- bear_url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
226
-
227
- # Download the video to a temporary file
228
- temp_video_path = download_video_from_url(bear_url)
229
- if temp_video_path:
230
- return temp_video_path, "Studio Ghibli animation with Hayao Miyazaki's distinctive hand-drawn art style"
231
- else:
232
- # Return a more descriptive error message
233
- print("Failed to download sample video. Using fallback.")
234
- # If download fails, try a direct URL
235
- return bear_url, "Studio Ghibli animation with Hayao Miyazaki's distinctive hand-drawn art style"
236
 
237
  with gr.Blocks(title="Video-to-Ghibli Style Converter") as iface:
238
  gr.Markdown("# Video-to-Ghibli Style Converter")
239
  gr.Markdown("Upload a video and convert it to Studio Ghibli animation style using GPT-4o and DALL-E 3.")
240
 
241
  with gr.Row():
242
- with gr.Column():
 
243
  video_input = gr.Video(label="Upload Video (up to 15 seconds)")
 
 
 
 
244
  api_key = gr.Textbox(
245
  label="OpenAI API Key (requires GPT-4o and DALL-E 3 access)",
246
  type="password",
@@ -251,13 +233,12 @@ with gr.Blocks(title="Video-to-Ghibli Style Converter") as iface:
251
  value="Studio Ghibli animation with Hayao Miyazaki's distinctive hand-drawn art style"
252
  )
253
 
254
- with gr.Row():
255
- submit_btn = gr.Button("Stylize Video", variant="primary")
256
- example_btn = gr.Button("Use Sample Bear Video")
257
 
258
- with gr.Column():
 
259
  video_output = gr.Video(label="Stylized Video")
260
- status_output = gr.Textbox(label="Status", value="Ready. Upload a video or use the sample video.")
261
 
262
  submit_btn.click(
263
  fn=stylize_video,
@@ -265,19 +246,19 @@ with gr.Blocks(title="Video-to-Ghibli Style Converter") as iface:
265
  outputs=[video_output, status_output]
266
  )
267
 
268
- def load_example_and_update_status():
269
- video, prompt = use_sample_bear_video()
270
- return video, prompt, "Sample video loaded. Enter your OpenAI API key and click 'Stylize Video'."
271
 
272
  example_btn.click(
273
- fn=load_example_and_update_status,
274
  inputs=None,
275
  outputs=[video_input, style_prompt, status_output]
276
  )
277
 
278
  gr.Markdown("""
279
  ## Instructions
280
- 1. Upload a video up to 15 seconds long or use the sample bear video
281
  2. Enter your OpenAI API key with GPT-4o and DALL-E 3 access
282
  3. Customize the style prompt if desired
283
  4. Click "Stylize Video" and wait for processing
 
34
  # Get default API key from environment (will be '' if not set)
35
  DEFAULT_API_KEY = os.getenv("OPENAI_API_KEY", "")
36
 
37
+ # Embed the Bear.mp4 video directly from a guaranteed source
38
+ BEAR_VIDEO_URL = "https://huggingface.co/spaces/gradio/video_component/resolve/main/files/Bear.mp4"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  def process_frame(frame_path, style_prompt, api_key):
41
  """Process a single frame with GPT-4o analysis and DALL-E 3 generation"""
 
118
 
119
  # Save the input video to a temporary file
120
  if isinstance(video_path, str):
121
+ if video_path.startswith('http'):
122
+ # It's a URL, download it
123
+ response = requests.get(video_path, stream=True)
124
+ with open(input_filename, 'wb') as f:
125
+ for chunk in response.iter_content(chunk_size=8192):
126
+ f.write(chunk)
127
+ elif os.path.exists(video_path):
128
  # It's a file path, copy it
129
  shutil.copy(video_path, input_filename)
130
  else:
 
207
  print(f"Error: {str(e)}\n{traceback_str}")
208
  return None, f"Error: {str(e)}"
209
 
210
+ def use_bear_video():
211
+ """Loads the Bear.mp4 sample video"""
212
+ return BEAR_VIDEO_URL, "Studio Ghibli animation with Hayao Miyazaki's distinctive hand-drawn art style"
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  with gr.Blocks(title="Video-to-Ghibli Style Converter") as iface:
215
  gr.Markdown("# Video-to-Ghibli Style Converter")
216
  gr.Markdown("Upload a video and convert it to Studio Ghibli animation style using GPT-4o and DALL-E 3.")
217
 
218
  with gr.Row():
219
+ with gr.Column(scale=2):
220
+ # Main input column
221
  video_input = gr.Video(label="Upload Video (up to 15 seconds)")
222
+ with gr.Row():
223
+ # Add example button in its own row
224
+ example_btn = gr.Button("Use Bear Video Example", variant="secondary")
225
+
226
  api_key = gr.Textbox(
227
  label="OpenAI API Key (requires GPT-4o and DALL-E 3 access)",
228
  type="password",
 
233
  value="Studio Ghibli animation with Hayao Miyazaki's distinctive hand-drawn art style"
234
  )
235
 
236
+ submit_btn = gr.Button("Stylize Video", variant="primary")
 
 
237
 
238
+ with gr.Column(scale=2):
239
+ # Output column
240
  video_output = gr.Video(label="Stylized Video")
241
+ status_output = gr.Textbox(label="Status", value="Ready. Upload a video or use the Bear video example.")
242
 
243
  submit_btn.click(
244
  fn=stylize_video,
 
246
  outputs=[video_output, status_output]
247
  )
248
 
249
+ def load_bear_and_update_status():
250
+ video, prompt = use_bear_video()
251
+ return video, prompt, "Bear video loaded. Enter your OpenAI API key and click 'Stylize Video'."
252
 
253
  example_btn.click(
254
+ fn=load_bear_and_update_status,
255
  inputs=None,
256
  outputs=[video_input, style_prompt, status_output]
257
  )
258
 
259
  gr.Markdown("""
260
  ## Instructions
261
+ 1. Upload a video up to 15 seconds long or use the Bear video example
262
  2. Enter your OpenAI API key with GPT-4o and DALL-E 3 access
263
  3. Customize the style prompt if desired
264
  4. Click "Stylize Video" and wait for processing