arjunanand13 commited on
Commit
c50a5fd
·
verified ·
1 Parent(s): 9222fae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -45,14 +45,19 @@ def analyze_image(image, prompt):
45
  with torch.no_grad():
46
  output = model.generate(**inputs, max_new_tokens=100)
47
  print(processor.decode(output[0]))
48
- result = processor.decode(output[0], skip_special_tokens=True)
 
49
 
50
- try:
51
- return json.loads(result)
52
- except json.JSONDecodeError:
53
- return {"error": "Failed to parse model output as JSON", "raw_output": result}
54
- except Exception as e:
55
- return {"error": str(e), "traceback": traceback.format_exc()}
 
 
 
 
56
 
57
  default_prompt = """Analyze this image and determine if it contains a data logger. A data logger is typically a small, black electronic device used to monitor and record data over time, such as voltage, temperature, or current, via external sensors.
58
 
 
45
  with torch.no_grad():
46
  output = model.generate(**inputs, max_new_tokens=100)
47
  print(processor.decode(output[0]))
48
+ full_response = processor.decode(output[0])
49
+
50
 
51
+ json_match = re.search(r'\{.*\}', full_response, re.DOTALL)
52
+ if json_match:
53
+ json_str = json_match.group(0)
54
+ try:
55
+ return json.loads(json_str)
56
+ except json.JSONDecodeError:
57
+ return {"error": "Failed to parse model output as JSON", "raw_output": json_str}
58
+ else:
59
+ return {"error": "No JSON found in the response", "raw_output": full_response}
60
+
61
 
62
  default_prompt = """Analyze this image and determine if it contains a data logger. A data logger is typically a small, black electronic device used to monitor and record data over time, such as voltage, temperature, or current, via external sensors.
63