Factor Studios commited on
Commit
750e393
·
verified ·
1 Parent(s): d352a1e

Update vision_analyzer.py

Browse files
Files changed (1) hide show
  1. vision_analyzer.py +14 -56
vision_analyzer.py CHANGED
@@ -46,6 +46,9 @@ from transformers import AutoProcessor, AutoModelForCausalLM
46
 
47
  # setup_unrar()
48
 
 
 
 
49
  def log_message(message: str):
50
  """Log messages with timestamp"""
51
  timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
@@ -59,33 +62,7 @@ def log_message(message: str):
59
 
60
 
61
 
62
- # Initialize rarfile with correct paths
63
- import os
64
- import shutil
65
- import rarfile
66
-
67
- # Try to detect unrar
68
- unrar_path = shutil.which("unrar")
69
-
70
- # Fallback to common paths
71
- if not unrar_path:
72
- possible_unrar_paths = [
73
- "/usr/bin/unrar",
74
- "/usr/local/bin/unrar",
75
- "/bin/unrar",
76
- "/app/bin/unrar"
77
- ]
78
- for path in possible_unrar_paths:
79
- if os.path.isfile(path) and os.access(path, os.X_OK):
80
- unrar_path = path
81
- break
82
 
83
- # Assign to rarfile
84
- if unrar_path:
85
- rarfile.UNRAR_TOOL = unrar_path
86
- else:
87
- rarfile.UNRAR_TOOL = None # Use built-in (limited) parser
88
- print("⚠️ 'unrar' is not installed. Some RAR files may not extract properly.")
89
 
90
 
91
  # Attempt to install flash-attn
@@ -194,44 +171,25 @@ def log_message(message: str):
194
  processing_status["logs"] = processing_status["logs"][-100:]
195
 
196
  def extract_with_retry(rar_path: str, output_dir: str, max_retries: int = 2) -> bool:
197
- """Extract RAR with retry and recovery, handling multi-part archives"""
198
- filename = os.path.basename(rar_path)
199
-
200
  for attempt in range(max_retries):
201
  try:
202
- # Try with rarfile first
203
- with rarfile.RarFile(rar_path) as rf:
204
- rf.extractall(output_dir)
205
  return True
206
-
207
- except rarfile.NeedFirstVolume:
208
- # Handle multi-part RARs
209
- base_name = filename.split(".part")[0] if ".part" in filename else filename.replace(".rar", "")
210
- first_part = f"{base_name}.part01.rar"
211
- first_part_path = os.path.join(os.path.dirname(rar_path), first_part)
212
-
213
- if not os.path.exists(first_part_path):
214
- log_message(f"⚠️ Multi-part RAR detected but first part not found: {first_part}")
215
- return False
216
-
217
- with rarfile.RarFile(first_part_path) as rf:
218
- rf.extractall(output_dir)
219
- return True
220
-
221
  except Exception as e:
222
- if attempt == max_retries - 1:
223
- # Final fallback to patool
224
- try:
225
- import patoolib
226
- patoolib.extract_archive(rar_path, outdir=output_dir)
227
- return True
228
- except Exception as patool_error:
229
- log_message(f"❌ All extraction methods failed: {str(patool_error)}")
230
- return False
231
  time.sleep(1)
232
 
233
  return False
234
 
 
235
  def log_failed_file(filename: str, error: str):
236
  """Log failed files to persistent file"""
237
  with open(FAILED_FILES_LOG, "a") as f:
 
46
 
47
  # setup_unrar()
48
 
49
+ rarfile.UNRAR_TOOL = None # Forces use of internal Python extractor
50
+ rarfile.PATH_SEP = '/' # Safe default
51
+
52
  def log_message(message: str):
53
  """Log messages with timestamp"""
54
  timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
 
62
 
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
 
 
 
 
 
 
66
 
67
 
68
  # Attempt to install flash-attn
 
171
  processing_status["logs"] = processing_status["logs"][-100:]
172
 
173
  def extract_with_retry(rar_path: str, output_dir: str, max_retries: int = 2) -> bool:
174
+ """Extract RAR file using rarfile module with retry logic"""
175
+ from rarfile import RarFile, BadRarFile
176
+
177
  for attempt in range(max_retries):
178
  try:
179
+ with RarFile(rar_path) as rf:
180
+ rf.extractall(path=output_dir)
181
+ log_message(f"✅ Successfully extracted: {os.path.basename(rar_path)}")
182
  return True
183
+ except BadRarFile as brf:
184
+ log_message(f"❌ Bad RAR file: {brf}")
185
+ return False
 
 
 
 
 
 
 
 
 
 
 
 
186
  except Exception as e:
187
+ log_message(f"⚠️ Extraction attempt {attempt + 1} failed: {str(e)}")
 
 
 
 
 
 
 
 
188
  time.sleep(1)
189
 
190
  return False
191
 
192
+
193
  def log_failed_file(filename: str, error: str):
194
  """Log failed files to persistent file"""
195
  with open(FAILED_FILES_LOG, "a") as f: