matinsn2000 commited on
Commit
9f1f56b
·
1 Parent(s): e12473b

Fixed the problem with rapic upload being failed

Browse files
cloudzy/agents/image_analyzer.py CHANGED
@@ -62,16 +62,31 @@ result: {
62
  ],
63
  )
64
 
65
- # Extract message text
66
  message = completion.choices[0].message
67
- text_content = message.content.strip()
 
 
 
 
 
 
 
 
68
 
69
  # Try to extract JSON-like dict from model output
70
  try:
 
 
 
71
  start = text_content.index("{")
72
  end = text_content.rindex("}") + 1
73
  json_str = text_content[start:end]
74
  result = json.loads(json_str)
 
 
 
 
75
  except Exception as e:
76
  raise ValueError(f"Failed to parse model output: {text_content}\nError: {e}")
77
 
 
62
  ],
63
  )
64
 
65
+ # Extract message text with robust type handling
66
  message = completion.choices[0].message
67
+
68
+ # Safely convert to string, handling non-string types
69
+ if message.content is None:
70
+ text_content = ""
71
+ else:
72
+ text_content = str(message.content).strip()
73
+
74
+ if not text_content:
75
+ raise ValueError("Model returned empty response")
76
 
77
  # Try to extract JSON-like dict from model output
78
  try:
79
+ if "{" not in text_content or "}" not in text_content:
80
+ raise ValueError("Response does not contain valid JSON structure (missing braces)")
81
+
82
  start = text_content.index("{")
83
  end = text_content.rindex("}") + 1
84
  json_str = text_content[start:end]
85
  result = json.loads(json_str)
86
+ except ValueError as ve:
87
+ raise ValueError(f"Failed to parse model output: {text_content}\nError: {ve}")
88
+ except json.JSONDecodeError as je:
89
+ raise ValueError(f"Invalid JSON in model output: {text_content}\nError: {je}")
90
  except Exception as e:
91
  raise ValueError(f"Failed to parse model output: {text_content}\nError: {e}")
92
 
cloudzy/agents/image_analyzer_2.py CHANGED
@@ -97,12 +97,15 @@ result: {
97
 
98
  response = self.agent.run(prompt, images=[image])
99
 
 
 
 
100
  # Extract JSON part from response
101
  # Look for the pattern: result: { ... }
102
- match = re.search(r'result:\s*(\{[\s\S]*\})', response)
103
 
104
  if not match:
105
- raise ValueError(f"Could not find JSON in response: {response}")
106
 
107
  json_str = match.group(1)
108
 
 
97
 
98
  response = self.agent.run(prompt, images=[image])
99
 
100
+ # Ensure response is a string
101
+ response_text = str(response) if response is not None else ""
102
+
103
  # Extract JSON part from response
104
  # Look for the pattern: result: { ... }
105
+ match = re.search(r'result:\s*(\{[\s\S]*\})', response_text)
106
 
107
  if not match:
108
+ raise ValueError(f"Could not find JSON in response: {response_text}")
109
 
110
  json_str = match.group(1)
111
 
cloudzy/routes/upload.py CHANGED
@@ -74,10 +74,23 @@ def process_image_in_background(photo_id: int, filepath: str):
74
  """
75
  from cloudzy.database import SessionLocal
76
  from sqlmodel import select
 
77
 
78
  try:
79
  result = None
80
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # --- Primary method: Analyze metadata from local filepath ---
82
  try:
83
  print(f"[Background] Analyzing image metadata locally for photo {photo_id}...")
 
74
  """
75
  from cloudzy.database import SessionLocal
76
  from sqlmodel import select
77
+ import time
78
 
79
  try:
80
  result = None
81
 
82
+ # --- Verify file exists and is readable ---
83
+ file_path = Path(filepath)
84
+ max_retries = 5
85
+ retry_count = 0
86
+ while not file_path.exists() and retry_count < max_retries:
87
+ print(f"[Background] Waiting for file {filepath} to be written (attempt {retry_count + 1}/{max_retries})...")
88
+ time.sleep(0.5)
89
+ retry_count += 1
90
+
91
+ if not file_path.exists():
92
+ raise FileNotFoundError(f"Image file not found at {filepath} after {max_retries} retries")
93
+
94
  # --- Primary method: Analyze metadata from local filepath ---
95
  try:
96
  print(f"[Background] Analyzing image metadata locally for photo {photo_id}...")