Factor Studios commited on
Commit
1af58ac
·
verified ·
1 Parent(s): be25862

Update vision_analyzer.py

Browse files
Files changed (1) hide show
  1. vision_analyzer.py +78 -18
vision_analyzer.py CHANGED
@@ -23,28 +23,49 @@ import torch
23
  from transformers import AutoProcessor, AutoModelForCausalLM
24
  # from transformers import AutoProcessor, AutoModelForCausalLM
25
 
26
- import os
27
 
28
- # Try to ensure unrar is available
29
- def setup_unrar():
30
- # Try system installation (may not work on all Spaces)
31
- if not os.path.exists("/usr/bin/unrar"):
32
- os.system("apt-get update > /dev/null 2>&1 && apt-get install -y unrar > /dev/null 2>&1 || true")
33
 
34
- # Setup Python alternatives
35
- try:
36
- import rarfile
37
- if os.path.exists("/usr/bin/unrar"):
38
- rarfile.UNRAR_TOOL = "/usr/bin/unrar"
39
- except ImportError:
40
- os.system("pip install rarfile")
41
 
42
- try:
43
- import patoolib
44
- except ImportError:
45
- os.system("pip install patool")
 
 
46
 
47
- setup_unrar()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  # Attempt to install flash-attn
50
  try:
@@ -151,6 +172,45 @@ def log_message(message: str):
151
  if len(processing_status["logs"]) > 100:
152
  processing_status["logs"] = processing_status["logs"][-100:]
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  def log_failed_file(filename: str, error: str):
155
  """Log failed files to persistent file"""
156
  with open(FAILED_FILES_LOG, "a") as f:
 
23
  from transformers import AutoProcessor, AutoModelForCausalLM
24
  # from transformers import AutoProcessor, AutoModelForCausalLM
25
 
26
+ # import os
27
 
28
+ # # Try to ensure unrar is available
29
+ # def setup_unrar():
30
+ # # Try system installation (may not work on all Spaces)
31
+ # if not os.path.exists("/usr/bin/unrar"):
32
+ # os.system("apt-get update > /dev/null 2>&1 && apt-get install -y unrar > /dev/null 2>&1 || true")
33
 
34
+ # # Setup Python alternatives
35
+ # try:
36
+ # import rarfile
37
+ # if os.path.exists("/usr/bin/unrar"):
38
+ # rarfile.UNRAR_TOOL = "/usr/bin/unrar"
39
+ # except ImportError:
40
+ # os.system("pip install rarfile")
41
 
42
+ # try:
43
+ # import patoolib
44
+ # except ImportError:
45
+ # os.system("pip install patool")
46
+
47
+ # setup_unrar()
48
 
49
+
50
+
51
+ # Initialize rarfile with correct paths
52
+ import rarfile
53
+ rarfile.UNRAR_TOOL = "unrar" # Try system unrar if exists
54
+
55
+ # Set alternative paths where unrar might be installed
56
+ possible_unrar_paths = [
57
+ "/usr/bin/unrar",
58
+ "/usr/local/bin/unrar",
59
+ "/bin/unrar",
60
+ "/app/bin/unrar"
61
+ ]
62
+
63
+ for path in possible_unrar_paths:
64
+ if os.path.exists(path):
65
+ rarfile.UNRAR_TOOL = path
66
+ break
67
+ else:
68
+ log_message("⚠️ System unrar not found - using Python implementation")
69
 
70
  # Attempt to install flash-attn
71
  try:
 
172
  if len(processing_status["logs"]) > 100:
173
  processing_status["logs"] = processing_status["logs"][-100:]
174
 
175
+ def extract_with_retry(rar_path: str, output_dir: str, max_retries: int = 2) -> bool:
176
+ """Extract RAR with retry and recovery, handling multi-part archives"""
177
+ filename = os.path.basename(rar_path)
178
+
179
+ for attempt in range(max_retries):
180
+ try:
181
+ # Try with rarfile first
182
+ with rarfile.RarFile(rar_path) as rf:
183
+ rf.extractall(output_dir)
184
+ return True
185
+
186
+ except rarfile.NeedFirstVolume:
187
+ # Handle multi-part RARs
188
+ base_name = filename.split(".part")[0] if ".part" in filename else filename.replace(".rar", "")
189
+ first_part = f"{base_name}.part01.rar"
190
+ first_part_path = os.path.join(os.path.dirname(rar_path), first_part)
191
+
192
+ if not os.path.exists(first_part_path):
193
+ log_message(f"⚠️ Multi-part RAR detected but first part not found: {first_part}")
194
+ return False
195
+
196
+ with rarfile.RarFile(first_part_path) as rf:
197
+ rf.extractall(output_dir)
198
+ return True
199
+
200
+ except Exception as e:
201
+ if attempt == max_retries - 1:
202
+ # Final fallback to patool
203
+ try:
204
+ import patoolib
205
+ patoolib.extract_archive(rar_path, outdir=output_dir)
206
+ return True
207
+ except Exception as patool_error:
208
+ log_message(f"❌ All extraction methods failed: {str(patool_error)}")
209
+ return False
210
+ time.sleep(1)
211
+
212
+ return False
213
+
214
  def log_failed_file(filename: str, error: str):
215
  """Log failed files to persistent file"""
216
  with open(FAILED_FILES_LOG, "a") as f: