arjunanand13 commited on
Commit
32e7c60
·
verified ·
1 Parent(s): ab74558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -83
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
- # json_match = re.search(r'\{.*?\}', full_response, re.DOTALL)
47
- # if json_match:
48
- # json_str = json_match.group(0)
49
- # try:
50
- # return json.loads(json_str)
51
- # except json.JSONDecodeError as e:
52
- # print(f"JSON decode error: {e}")
53
- # return {"error": "Failed to parse JSON", "full_response": full_response}
54
- # else:
55
- # return {"error": "No JSON found in response", "full_response": full_response}
56
-
57
- # import torch
58
- # from PIL import Image
59
- # from transformers import AutoProcessor, AutoModelForCausalLM
60
- # import gradio as gr
61
- # import json
62
- # import traceback
63
- # import os
64
-
65
- # model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
66
- # token = os.getenv("HUGGINGFACE_TOKEN").strip()
67
- # processor = AutoProcessor.from_pretrained(model_name, token=token)
68
- # model = AutoModelForCausalLM.from_pretrained(
69
- # model_name,
70
- # quantization_config={"load_in_4bit": True},
71
- # token=token
72
- # )
73
-
74
- # if torch.cuda.is_available():
75
- # model = model.to('cuda')
76
-
77
- # def analyze_image(image, prompt):
78
- # try:
79
- # messages = [
80
- # {"role": "user", "content": [
81
- # {"type": "image"},
82
- # {"type": "text", "text": prompt}
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
- # iface.launch()
 
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()