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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -36
app.py CHANGED
@@ -20,44 +20,41 @@ if torch.cuda.is_available():
20
  model = model.to('cuda')
21
 
22
  def analyze_image(image, prompt):
23
- try:
24
- messages = [
25
- {"role": "user", "content": [
26
- {"type": "image"},
27
- {"type": "text", "text": prompt}
28
- ]}
29
- ]
30
- input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
31
-
32
- inputs = processor(
33
- image,
34
- input_text,
35
- add_special_tokens=False,
36
- return_tensors="pt"
37
- ).to(model.device)
38
-
39
- # # Separate inputs for generate method
40
- # generate_inputs = {
41
- # k: v for k, v in inputs.items()
42
- # if k not in ['pixel_values', 'aspect_ratio_ids', 'aspect_ratio_mask']
43
- # }
44
-
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
 
 
20
  model = model.to('cuda')
21
 
22
  def analyze_image(image, prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ messages = [
25
+ {"role": "user", "content": [
26
+ {"type": "image"},
27
+ {"type": "text", "text": prompt}
28
+ ]}
29
+ ]
30
+ input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
31
+
32
+ inputs = processor(
33
+ image,
34
+ input_text,
35
+ add_special_tokens=False,
36
+ return_tensors="pt"
37
+ ).to(model.device)
38
+
39
+ # # Separate inputs for generate method
40
+ # generate_inputs = {
41
+ # k: v for k, v in inputs.items()
42
+ # if k not in ['pixel_values', 'aspect_ratio_ids', 'aspect_ratio_mask']
43
+ # }
44
+
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
+ return json.loads(json_str)
55
+ else:
56
+ return full_response
57
+
58
 
59
  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.
60