Files changed (1) hide show
  1. handler.py +19 -4
handler.py CHANGED
@@ -1,6 +1,13 @@
1
  import os
2
- import cv2
3
  import datetime
 
 
 
 
 
 
 
 
4
  import torch
5
  import numpy as np
6
  import hashlib
@@ -88,6 +95,8 @@ def vote_last_response(state, vote_type, model_selector):
88
  print(f"Failed to upload vote file: {e}")
89
 
90
  def is_valid_video_filename(name):
 
 
91
  video_extensions = ["avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg"]
92
  ext = name.split(".")[-1].lower()
93
  return ext in video_extensions
@@ -98,6 +107,9 @@ def is_valid_image_filename(name):
98
  return ext in image_extensions
99
 
100
  def sample_frames(video_file, num_frames):
 
 
 
101
  video = cv2.VideoCapture(video_file)
102
  total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
103
  interval = total_frames // num_frames
@@ -271,9 +283,12 @@ def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, ma
271
  image.save(filename)
272
 
273
  # Process image for model
274
- image_tensor = process_images([image], our_chatbot.image_processor, our_chatbot.model.config)[0]
275
- image_tensor = image_tensor.half().to(our_chatbot.model.device)
276
- image_tensor = image_tensor.unsqueeze(0)
 
 
 
277
 
278
  # Prepare conversation
279
  inp = DEFAULT_IMAGE_TOKEN + "\n" + message_text
 
1
  import os
 
2
  import datetime
3
+
4
+ # Try to import cv2, but make it optional
5
+ try:
6
+ import cv2
7
+ CV2_AVAILABLE = True
8
+ except ImportError:
9
+ CV2_AVAILABLE = False
10
+ print("Warning: cv2 (OpenCV) not available. Video processing will be disabled.")
11
  import torch
12
  import numpy as np
13
  import hashlib
 
95
  print(f"Failed to upload vote file: {e}")
96
 
97
  def is_valid_video_filename(name):
98
+ if not CV2_AVAILABLE:
99
+ return False # Video processing disabled
100
  video_extensions = ["avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg"]
101
  ext = name.split(".")[-1].lower()
102
  return ext in video_extensions
 
107
  return ext in image_extensions
108
 
109
  def sample_frames(video_file, num_frames):
110
+ if not CV2_AVAILABLE:
111
+ raise ImportError("cv2 (OpenCV) not available. Video processing is disabled.")
112
+
113
  video = cv2.VideoCapture(video_file)
114
  total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
115
  interval = total_frames // num_frames
 
283
  image.save(filename)
284
 
285
  # Process image for model
286
+ try:
287
+ image_tensor = process_images([image], our_chatbot.image_processor, our_chatbot.model.config)[0]
288
+ image_tensor = image_tensor.half().to(our_chatbot.model.device)
289
+ image_tensor = image_tensor.unsqueeze(0)
290
+ except Exception as e:
291
+ return {"error": f"Image processing failed: {str(e)}"}
292
 
293
  # Prepare conversation
294
  inp = DEFAULT_IMAGE_TOKEN + "\n" + message_text