Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,87 +42,43 @@ def analyze_image(image, prompt):
|
|
| 42 |
full_response = processor.decode(output[0])
|
| 43 |
print("Full response:", full_response) # Debug print
|
| 44 |
|
| 45 |
-
return full_response
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# ]}
|
| 84 |
-
# ]
|
| 85 |
-
# input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 86 |
-
# inputs = processor(
|
| 87 |
-
# image,
|
| 88 |
-
# input_text,
|
| 89 |
-
# add_special_tokens=False,
|
| 90 |
-
# return_tensors="pt"
|
| 91 |
-
# ).to(model.device)
|
| 92 |
-
# with torch.no_grad():
|
| 93 |
-
# output = model.generate(**inputs, max_new_tokens=100)
|
| 94 |
-
# result = processor.decode(output[0], skip_special_tokens=True)
|
| 95 |
-
# try:
|
| 96 |
-
# return json.loads(result)
|
| 97 |
-
# except json.JSONDecodeError:
|
| 98 |
-
# return {"error": "Failed to parse model output as JSON", "raw_output": result}
|
| 99 |
-
# except Exception as e:
|
| 100 |
-
# return {"error": str(e), "traceback": traceback.format_exc()}
|
| 101 |
-
|
| 102 |
-
# default_prompt = """Analyze this image and determine if it contains a data logger.
|
| 103 |
-
# A data logger is typically a small, black electronic device used to monitor and record data
|
| 104 |
-
# over time, such as voltage, temperature, or current, via external sensors.
|
| 105 |
-
|
| 106 |
-
# If a data logger is present in the image, respond with:
|
| 107 |
-
# {"present": true, "reason": "Brief explanation of why you believe it's a data logger"}
|
| 108 |
-
|
| 109 |
-
# If no data logger is visible, respond with:
|
| 110 |
-
# {"present": false, "reason": "Brief explanation of why you believe there's no data logger"}
|
| 111 |
-
|
| 112 |
-
# Ensure your response is in valid JSON format."""
|
| 113 |
-
|
| 114 |
-
# iface = gr.Interface(
|
| 115 |
-
# fn=analyze_image,
|
| 116 |
-
# inputs=[
|
| 117 |
-
# gr.Image(type="pil", label="Upload Image"),
|
| 118 |
-
# gr.Textbox(label="Prompt", value=default_prompt, lines=10)
|
| 119 |
-
# ],
|
| 120 |
-
# outputs=gr.JSON(label="Analysis Result"),
|
| 121 |
-
# title="Data Logger Detection using Llama 3.2 Vision",
|
| 122 |
-
# description="Upload an image and customize the prompt to check if it contains a data logger.",
|
| 123 |
-
# examples=[
|
| 124 |
-
# ["bad.png", default_prompt]
|
| 125 |
-
# ]
|
| 126 |
-
# )
|
| 127 |
|
| 128 |
-
|
|
|
|
| 42 |
full_response = processor.decode(output[0])
|
| 43 |
print("Full response:", full_response) # Debug print
|
| 44 |
|
| 45 |
+
# return full_response
|
| 46 |
+
try:
|
| 47 |
+
json_match = re.search(r'\{.*?\}', full_response, re.DOTALL)
|
| 48 |
+
if json_match:
|
| 49 |
+
json_str = json_match.group(0)
|
| 50 |
+
try:
|
| 51 |
+
return json.loads(json_str)
|
| 52 |
+
except json.JSONDecodeError as e:
|
| 53 |
+
return full_response
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return full_response
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
default_prompt = """Analyze this image and determine if it contains a data logger.
|
| 59 |
+
A data logger is typically a small, black electronic device used to monitor and record data
|
| 60 |
+
over time, such as voltage, temperature, or current, via external sensors.
|
| 61 |
+
|
| 62 |
+
If a data logger is present in the image, respond with:
|
| 63 |
+
{"present": true, "reason": "Brief explanation of why you believe it's a data logger"}
|
| 64 |
+
|
| 65 |
+
If no data logger is visible, respond with:
|
| 66 |
+
{"present": false, "reason": "Brief explanation of why you believe there's no data logger"}
|
| 67 |
+
|
| 68 |
+
Ensure your response is in valid JSON format."""
|
| 69 |
+
|
| 70 |
+
iface = gr.Interface(
|
| 71 |
+
fn=analyze_image,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 74 |
+
gr.Textbox(label="Prompt", value=default_prompt, lines=10)
|
| 75 |
+
],
|
| 76 |
+
outputs=gr.JSON(label="Analysis Result"),
|
| 77 |
+
title="Data Logger Detection using Llama 3.2 Vision",
|
| 78 |
+
description="Upload an image and customize the prompt to check if it contains a data logger.",
|
| 79 |
+
examples=[
|
| 80 |
+
["bad.png", default_prompt]
|
| 81 |
+
]
|
| 82 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
iface.launch()
|