Stardragon2099 commited on
Commit
bbc1ea6
·
verified ·
1 Parent(s): 344db47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -137
app.py CHANGED
@@ -4,13 +4,16 @@ from PIL import Image
4
  from transformers import AutoModelForCausalLM, AutoProcessor
5
  import torch
6
  import gradio as gr
 
7
 
8
- model_name = "Stardragon2099/florencetrial-45e"
9
- # model_name = "arjunanand13/LADP_Florence-40e"
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  print(f"Using device: {device}")
12
 
13
- model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).to(device)
 
 
14
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
15
 
16
  torch.cuda.empty_cache()
@@ -20,31 +23,50 @@ DEFAULT_PROMPT = ("You are a Leg Lift Classifier. There is an image of a through
20
  "if any leg is not completely seated in the hole; return 'False' if the leg is inserted "
21
  "in the hole. Return only the required JSON in this format: {Leg_lift: , Reason: }.")
22
 
23
- def predict(image, question):
24
-
25
- if not isinstance(image, Image.Image):
26
- raise ValueError(f"Expected image to be PIL.Image, but got {type(image)}")
27
 
28
-
29
- encoding = processor(images=image, text=question, return_tensors="pt").to(device)
 
 
 
 
 
 
30
 
31
- with torch.no_grad():
32
- outputs = model.generate(**encoding, max_length=256)
33
 
34
- answer = processor.batch_decode(outputs, skip_special_tokens=True)[0]
35
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def get_prompt(method, component_label=None):
38
  global prompt
39
  if method == "leg_lift":
40
- prompt = """You are a Leg Lift Classifier. Analyze the provided cropped image of a throughput component and determine if the long metal leg tip is inserted in the hole.
 
41
  Focus on the metal leg tip and the hole .
 
42
  Return a JSON object with the following keys:
43
- - 'Leg_lift': 'True' if metal leg tip is away from the hole or if only the hole or leg is visible.and 'False' if the metal leg is inserted in the hole or is connected to the hole surface.
44
- - 'Reason': A brief explanation of your conclusion.
45
 
46
- Format the response exactly as specified:
47
- { "Leg_lift":True/False , "Reason": "Why it is not leg lift or not"}"""
 
 
48
 
49
 
50
  elif method == "polarity":
@@ -52,164 +74,199 @@ def get_prompt(method, component_label=None):
52
  prompt = """You are a Choke Polarity Classifier. Analyze the provided cropped image of a choke component.
53
  Focus on the color pattern and orientation. For choke components, the correct polarity is indicated by the gold color being on the left and the red color being on the right.
54
  Return 'True' if the gold color is on the left and the red color is on the right; return 'False' if the colors are reversed.
55
- Format the response exactly as specified: { "Polarity": True/False, "Reason": "Gold red position" }"""
 
56
 
57
-
58
  elif component_label in ["choke_full_gn_ye"]:
59
  prompt = """You are a Choke Polarity Classifier. Analyze the provided cropped image of a choke component.
60
  Focus on the color pattern and orientation. For choke components, the correct polarity is indicated by the green color being on the left and the yellow color being on the right.
61
  Return 'True' if the green color is on the left and the yellow color is on the right; return 'False' if the colors are reversed.
62
- Format the response exactly as specified: { "Polarity": True/False, "Reason": "Green Yellow position" }"""
63
-
64
- else:
65
- prompt = """You are a Polarity Classifier. Analyze the provided cropped image of capacitor. Consider the capacitor that takes largest portion of the image, ignore others.
66
  Focus on the long white strip on the largest capacitor. Return 'True' if there is a white strip on the left side of the capacitor(correct polarity); return 'False' if the white strip
67
- is on the right side of the capacitor(incorrect polarity). Format the response exactly as specified: { "Polarity": True/False, "Reason": "Your explanation here" }"""
 
 
68
 
69
 
70
  elif method == "angle":
71
  if component_label in ["Y_capacitor","varistor_red","varistor_grey"]:
72
- prompt = """You are an Angle Shift Classifier. Analyze the provided image focusing on the Y capacitor or varistor.
73
  the Y capacitor and varistor can be blue, white or red in color. it is allmost circular in shape.
74
  consider only the largest Y capacitor or varistor.
75
- Determine if the largest component is tilted or on the verge of falling. Use the green boundary as a reference for checking whether the largest capacitor is tilted.
76
- If the largest Y capacitor or varistor is tilted and is outside the green boundary, return 'True'; otherwise, return 'False'.
77
- Format the response exactly as specified:
78
- { "Angle": True/False, "Reason": "Your explanation here" }"""
79
 
 
 
 
 
 
 
80
  else:
81
- prompt = """You are a Capacitor shift classifier. Analyze the provided image of the capacitor and check whether it's touching the green line.
82
- Consider the capacitor that takes largest portion of the image. Donot approximate, take the current green line only.
83
- Use the green line as a reference. if the base of the largest capacitor is under the green line, return 'False'; otherwise, return 'True'.
84
- Format the response exactly as specified:
85
- { "Angle": True/False, "Reason": "Your explanation here." }"""
86
 
87
  elif method == "detection":
88
  if component_label in ["Y_capacitor"]:
89
- prompt = """You are a Y Capacitor Object Detector. Analyze the provided image and focus on identifying the Y capacitor.
90
  The Y capacitor is sort of a circular object in blue color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image portion sometimes.
91
- Determine if a blue object resembling a Y capacitor is present in the center of the image.
92
- Return 'False' if you arent confident about Y capacitor object being present; return 'True' if it is present.
93
- Format the response exactly as specified:
94
- { "Detection": True/False, "Reason": "Explain whether the blue circular Y capacitor is present or missing, accounting for different views or partial visibility." }"""
95
 
96
  elif component_label in ["DC_cap_blue"]:
97
- prompt = """You are a Blue DC Capacitor Object Detector . Analyze the provided cropped image and focus on the object.
98
- DC capacitor is a blue color object which is in cylindrical shape .
99
- Determine if the object is present in the image.
100
- Return 'False' if the object is missing; return 'True' if it is present.
101
- Format the response exactly as specified:
102
- { "Detection": True/False, "Reason": "Explain whether the object is present or missing." }"""
103
 
104
  elif component_label in ["DC_cap_black"]:
105
- prompt = """You are a Black DC Capacitor Object Detector . Analyze the provided cropped image and focus on the object.
106
- DC capacitor is a black color object which is in cylindrical shape .
107
- Determine if the object is present in the image.
108
- Return 'False' if the object is missing; return 'True' if it is present.
109
- Format the response exactly as specified:
110
- { "Detection": True/False, "Reason": "Explain whether the object is present or missing." }"""
111
 
112
  elif component_label in ["choke_full_rd_gd"]:
113
- prompt = """You are a Red-Gold Choke Object Detector. Analyze the provided cropped image and focus on the choke component.
114
- The choke component should have distinct red and gold colors.
115
- Determine if the choke component with red on one side and gold on the other side is present in the image.
116
- Return 'False' if the choke component is missing; return 'True' if it is present.
117
- Format the response exactly as specified:
118
- { "Detection": True/False, "Reason": "Explain whether the red-gold choke component is present or missing." }"""
119
 
120
  elif component_label in ["choke_full_gn_ye"]:
121
- prompt = """You are a Green-Yellow Choke Object Detector. Analyze the provided cropped image and focus on the choke component.
122
- The choke component should have distinct green and yellow colors.
123
- Determine if the choke component with green on one side and yellow on the other side is present in the image.
124
- Return 'False' if the choke component is missing; return 'True' if it is present.
125
- Format the response exactly as specified:
126
- { "Detection": True/False, "Reason": "Explain whether the green-yellow choke component is present or missing." }"""
127
- elif component_label in ["Inductor"]:
128
- prompt = """You are an Inductor Object Detector. Analyze the provided cropped image and focus on the object.
129
- The inductor is typically a component with a dark (black/ brown/grey) body.
130
- Determine if the inductor with the specified color is present in the image.
131
- Return 'False' if the inductor object is missing; return 'True' if it is present.
132
- Format the response exactly as specified:
133
- { "Detection": True/False, "Reason": "Explain whether the dark-colored inductor is present or missing." }"""
134
- elif component_label in ["X2_capacitor"]:
135
- prompt = """You are an X2 Capacitor Object Detector. Analyze the provided cropped image and focus on the object.
136
- An X2 capacitor is typically a blue box-shaped cuboid.
137
- Determine if the X2 capacitor with the specified blue color and box-like shape is present in the image.
138
- Return 'False' if the X2 capacitor object is missing; return 'True' if it is present.
139
- Format the response exactly as specified:
140
- { "Detection": True/False, "Reason": "Explain whether the blue box-shaped X2 capacitor is present or missing." }"""
141
- elif component_label in ["Fuse_red"]:
142
- prompt = """You are a Red Fuse Object Detector. Analyze the provided cropped image and focus on the object.
143
- A red fuse is typically a red cuboid box-shaped object.
144
- Determine if the red fuse with the specified color and box-like shape is present in the image.
145
- Return 'False' if the red fuse object is missing; return 'True' if it is present.
146
- Format the response exactly as specified:
147
- { "Detection": True/False, "Reason": "Explain whether the red box-shaped fuse is present or missing." }"""
148
-
149
- elif component_label in ["varistor_grey"]:
150
- prompt = """You are a Varistor Object Detector. Analyze the provided image and focus on identifying the white/grey varistor.
151
  A varistor is sort of circular-shaped in side view with a white or greyish color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image sometimes.
152
- Determine if a circular object resembling a varistor is present anywhere in the image.
153
- Return 'False' if the white varistor object is missing; return 'True' if it is present.
154
- Format the response exactly as specified:
155
- { "Detection": True/False, "Reason": "Explain whether the varistor_grey is present or missing, accounting for different views or partial visibility." }"""
156
 
157
- elif component_label in ["varistor_red"]:
158
- prompt = """You are a Red Varistor Object Detector. Analyze the provided image and focus on identifying the red varistor.
159
  A varistor is typically almost circular-shaped component with a red color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image sometimes.
160
- Determine if a red circular object resembling a varistor is present in the image.
161
- Return 'False' if the red varistor object is missing; return 'True' if it is present.
162
- Format the response exactly as specified:
163
- { "Detection": True/False, "Reason": "Explain whether the red circular varistor is present or missing, accounting for different views or partial visibility." }"""
164
-
165
- elif component_label in ["transformer"]:
166
- prompt = """You are a Transformer Object Detector. Analyze the provided cropped image and focus on the object.
167
- This particular transformer is characterized by its rectangular shape and metallic contacts.
168
- Determine if this transformer is present in the image.
169
- Return 'False' if the transformer object is missing; return 'True' if it is present.
170
- Format the response exactly as specified:
171
- { "Detection": True/False, "Reason": "Explain whether the rectangular transformer is present or missing." }"""
172
-
173
- elif component_label in ["Transistor"]:
174
- prompt = """You are a Transistor Object Detector. Analyze the provided cropped image and focus on the object.
175
- The transistor has a blue cylindrical shape with metal contacts on the ends.
176
- Determine if this blue cylindrical transistor is present in the image.
177
- Return 'False' if the transistor object is missing; return 'True' if it is present.
178
- Format the response exactly as specified:
179
- { "Detection": True/False, "Reason": "Explain whether the blue cylindrical transistor is present or missing." }"""
180
 
181
  elif component_label in ["Fuse_black"]:
182
- prompt = """You are a Black Fuse Object Detector. Analyze the provided cropped image and focus on the object.
183
- A black fuse is typically a black cuboid box-shaped object.
184
- Determine if the black fuse with the specified color and box-like shape is present in the image.
185
- Return 'False' if the black fuse object is missing; return 'True' if it is present.
186
- Format the response exactly as specified:
187
- { "Detection": True/False, "Reason": "Explain whether the black box-shaped fuse is present or missing." }"""
 
 
188
  return prompt
189
 
190
- def gradio_interface(image, method, component_label):
191
- question = get_prompt(method, component_label)
192
  if image.mode != "RGB":
193
  image = image.convert("RGB")
194
 
195
  answer = predict(image, question)
 
196
  return answer
197
 
 
 
 
 
 
198
  Method =["detection", "angle", "leg_lift", "polarity"]
199
  Component_list = ["choke_full_rd_gd", "choke_full_gn_ye", "Y_capacitor","varistor_red","varistor_grey", "DC_cap_blue", "DC_cap_black", "Inductor", "X2_capacitor", "Fuse_red", "transformer", "Transistor", "Fuse_black"]
200
 
201
- iface = gr.Interface(
202
- fn=gradio_interface,
203
- inputs=[
204
- gr.Image(type="pil", label="Upload Image"), # Ensures image is passed as a PIL object
205
- gr.Dropdown(choices=Method, label="Method"),
206
- gr.Dropdown(choices = Component_list, label="Component label")
207
- # gr.Textbox(label="Enter your question or edit the default prompt", lines=6, value=DEFAULT_PROMPT) # Default prompt pre-filled and editable
208
- ],
209
- outputs=gr.Textbox(label="Answer"),
210
- title="Florence-enphase Leg Lift Classifier",
211
- description=("Upload an image and ask a question about the leg lift. The model will classify whether "
212
- "the leg is inserted in the hole or not based on the image. You can edit the default prompt if needed.")
213
- )
214
-
215
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from transformers import AutoModelForCausalLM, AutoProcessor
5
  import torch
6
  import gradio as gr
7
+ import re
8
 
9
+ # model_name = "arjunanand13/Florence-enphase2"
10
+ model_name = "Stardragon2099/florence-adlp-40e"
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  print(f"Using device: {device}")
13
 
14
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
15
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
16
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch_dtype, trust_remote_code=True).to(device)
17
  processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
18
 
19
  torch.cuda.empty_cache()
 
23
  "if any leg is not completely seated in the hole; return 'False' if the leg is inserted "
24
  "in the hole. Return only the required JSON in this format: {Leg_lift: , Reason: }.")
25
 
26
+ def predict(img, prompt):
27
+ inputs = processor(text=prompt, images=img, return_tensors="pt").to(device, torch_dtype)
 
 
28
 
29
+ generated_ids = model.generate(
30
+ input_ids=inputs["input_ids"],
31
+ pixel_values=inputs["pixel_values"],
32
+ max_new_tokens=1024,
33
+ do_sample=False,
34
+ num_beams=3
35
+ )
36
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
37
 
38
+ parsed_answer = processor.post_process_generation(generated_text, task= prompt, image_size=(img.width, img.height))
 
39
 
40
+ return parsed_answer
41
+
42
+
43
+ # if not isinstance(image, Image.Image):
44
+ # raise ValueError(f"Expected image to be PIL.Image, but got {type(image)}")
45
+
46
+
47
+ # encoding = processor(images=image, text=question, return_tensors="pt").to(device)
48
+
49
+ # with torch.no_grad():
50
+ # outputs = model.generate(**encoding, max_length=725)
51
+
52
+ # answer = processor.batch_decode(outputs, skip_special_tokens=True)[0]
53
+ # return answer
54
 
55
  def get_prompt(method, component_label=None):
56
  global prompt
57
  if method == "leg_lift":
58
+ if component_label in ["Y_capacitor","varistor_red","varistor_grey"]:
59
+ prompt = """You are a Leg Lift length Analyzer. Analyze the provided cropped image of a throughput component and determine the length of the metal tip from the curve in it .
60
  Focus on the metal leg tip and the hole .
61
+ The length will not exceed 2.5 milli meter .
62
  Return a JSON object with the following keys:
63
+ - 'Leg_lift': 'True' if the curve of the metal leg is away from the hole or if only the hole or leg is visible.and 'False' if the metal leg is inserted in the hole or is connected to the hole surface.
64
+ - 'Measurements': A Measumrement value ranging from 1 milli meter to 2.5 milli meter .
65
 
66
+ Format the response exactly as specified:
67
+ { "Leg_lift":True/False , "Measurements": "Measured length"}"""
68
+ else:
69
+ prompt = "Not a valid combination"
70
 
71
 
72
  elif method == "polarity":
 
74
  prompt = """You are a Choke Polarity Classifier. Analyze the provided cropped image of a choke component.
75
  Focus on the color pattern and orientation. For choke components, the correct polarity is indicated by the gold color being on the left and the red color being on the right.
76
  Return 'True' if the gold color is on the left and the red color is on the right; return 'False' if the colors are reversed.
77
+ Format the response exactly as specified: { "Polarity": True/False, "Measurements": None }"""
78
+
79
 
 
80
  elif component_label in ["choke_full_gn_ye"]:
81
  prompt = """You are a Choke Polarity Classifier. Analyze the provided cropped image of a choke component.
82
  Focus on the color pattern and orientation. For choke components, the correct polarity is indicated by the green color being on the left and the yellow color being on the right.
83
  Return 'True' if the green color is on the left and the yellow color is on the right; return 'False' if the colors are reversed.
84
+ Format the response exactly as specified: { "Polarity": True/False, "Measurements": None }"""
85
+
86
+ elif component_label in ["DC_cap_blue", "DC_cap_black"]:
87
+ prompt = """You are a Polarity Classifier. Analyze the provided cropped image of capacitor. Consider the black capacitor that takes largest portion of the image, ignore others.
88
  Focus on the long white strip on the largest capacitor. Return 'True' if there is a white strip on the left side of the capacitor(correct polarity); return 'False' if the white strip
89
+ is on the right side of the capacitor(Incorrect polarity). Format the response exactly as specified: { "Polarity": True/False, "Measurements": None }"""
90
+ else:
91
+ prompt = "Not a valid combination"
92
 
93
 
94
  elif method == "angle":
95
  if component_label in ["Y_capacitor","varistor_red","varistor_grey"]:
96
+ prompt = """You are an Angle Shift Classifier. Analyze the provided image focusing on the Y capacitor or varistor.
97
  the Y capacitor and varistor can be blue, white or red in color. it is allmost circular in shape.
98
  consider only the largest Y capacitor or varistor.
99
+ Determine if the largest component is tilted or on the verge of falling. Use the green boundary as a reference for checking whether the largest capacitor is tilted.
100
+ If the largest Y capacitor or varistor is tilted and is outside the green boundary, return 'True'; otherwise, return 'False'.
101
+ Format the response exactly as specified:
102
+ { "Angle": True/False, "Measurements": None}"""
103
 
104
+ elif component_label in ["DC_cap_blue", "DC_cap_black"]:
105
+ prompt = """You are a Capacitor shift classifier. Analyze the provided image of the capacitor and check whether it's touching the red line.
106
+ Consider the capacitor that takes largest portion of the image and is located inside a green rectangle. Do not approximate, take the current red line only.
107
+ Use the red line as a reference. if the base of the largest capacitor is under the red line, return 'False'; otherwise, return 'True'.
108
+ Format the response exactly as specified:
109
+ { "Angle": True/False, "Measurements": None }"""
110
  else:
111
+ prompt = "Not a valid combination"
 
 
 
 
112
 
113
  elif method == "detection":
114
  if component_label in ["Y_capacitor"]:
115
+ prompt = """You are a Y Capacitor Object Detector. Analyze the provided image and focus on identifying the Y capacitor.
116
  The Y capacitor is sort of a circular object in blue color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image portion sometimes.
117
+ Determine if a blue object resembling a Y capacitor is present in the center of the image.
118
+ Return 'False' if you arent confident about Y capacitor object being present; return 'True' if it is present.
119
+ Format the response exactly as specified:
120
+ { "Detection": True/False, "Measurements": None} """
121
 
122
  elif component_label in ["DC_cap_blue"]:
123
+ prompt = """You are a Blue DC Capacitor Object Detector . Analyze the provided cropped image and focus on the object.
124
+ DC capacitor is a blue color object which is in cylindrical shape .
125
+ Determine if the object is present in the image.
126
+ Return 'False' if the object is missing; return 'True' if it is present.
127
+ Format the response exactly as specified:
128
+ { "Detection": True/False, "Measurements": None} """
129
 
130
  elif component_label in ["DC_cap_black"]:
131
+ prompt = """You are a Black DC Capacitor Object Detector . Analyze the provided cropped image and focus on the object.
132
+ DC capacitor is a black color object which is in cylindrical shape .
133
+ Determine if the object is present in the image.
134
+ Return 'False' if the object is missing; return 'True' if it is present.
135
+ Format the response exactly as specified:
136
+ { "Detection": True/False, "Measurements": None} """
137
 
138
  elif component_label in ["choke_full_rd_gd"]:
139
+ prompt = """You are a Red-Gold Choke Object Detector. Analyze the provided cropped image and focus on the choke component.
140
+ The choke component should have distinct red and gold colors.
141
+ Determine if the choke component with red on one side and gold on the other side is present in the image.
142
+ Return 'False' if the choke component is missing; return 'True' if it is present.
143
+ Format the response exactly as specified:
144
+ { "Detection": True/False, "Measurements": None} """
145
 
146
  elif component_label in ["choke_full_gn_ye"]:
147
+ prompt = """You are a Green-Yellow Choke Object Detector. Analyze the provided cropped image and focus on the choke component.
148
+ The choke component should have distinct green and yellow colors.
149
+ Determine if the choke component with green on one side and yellow on the other side is present in the image.
150
+ Return 'False' if the choke component is missing; return 'True' if it is present.
151
+ Format the response exactly as specified:
152
+ { "Detection": True/False, "Measurements": None} """
153
+ elif component_label in ["Inductor"]:
154
+ prompt = """You are an Inductor Object Detector. Analyze the provided cropped image and focus on the object.
155
+ The inductor is typically a component with a dark (black/ brown/grey) body.
156
+ Determine if the inductor with the specified color is present in the image.
157
+ Return 'False' if the inductor object is missing; return 'True' if it is present.
158
+ Format the response exactly as specified:
159
+ { "Detection": True/False, "Measurements": None} """
160
+ elif component_label in ["X2_capacitor"]:
161
+ prompt = """You are an X2 Capacitor Object Detector. Analyze the provided cropped image and focus on the object.
162
+ An X2 capacitor is typically a blue box-shaped cuboid.
163
+ Determine if the X2 capacitor with the specified blue color and box-like shape is present in the image.
164
+ Return 'False' if the X2 capacitor object is missing; return 'True' if it is present.
165
+ Format the response exactly as specified:
166
+ { "Detection": True/False, "Measurements": None} """
167
+ elif component_label in ["Fuse_red"]:
168
+ prompt = """You are a Red Fuse Object Detector. Analyze the provided cropped image and focus on the object.
169
+ A red fuse is typically a red cuboid box-shaped object.
170
+ Determine if the red fuse with the specified color and box-like shape is present in the image.
171
+ Return 'False' if the red fuse object is missing; return 'True' if it is present.
172
+ Format the response exactly as specified:
173
+ { "Detection": True/False, "Measurements": None} """
174
+
175
+ elif component_label in ["varistor_grey"]:
176
+ prompt = """You are a Varistor Object Detector. Analyze the provided image and focus on identifying the white/grey varistor.
177
  A varistor is sort of circular-shaped in side view with a white or greyish color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image sometimes.
178
+ Determine if a circular object resembling a varistor is present anywhere in the image.
179
+ Return 'False' if the white varistor object is missing; return 'True' if it is present.
180
+ Format the response exactly as specified:
181
+ { "Detection": True/False, "Measurements": None} """
182
 
183
+ elif component_label in ["varistor_red"]:
184
+ prompt = """You are a Red Varistor Object Detector. Analyze the provided image and focus on identifying the red varistor.
185
  A varistor is typically almost circular-shaped component with a red color. The object may appear in different views (top view, side view, etc.) and may occupy only 20-30% of the image sometimes.
186
+ Determine if a red circular object resembling a varistor is present in the image.
187
+ Return 'False' if the red varistor object is missing; return 'True' if it is present.
188
+ Format the response exactly as specified:
189
+ { "Detection": True/False, "Measurements": None} """
190
+
191
+ elif component_label in ["transformer"]:
192
+ prompt = """You are a Transformer Object Detector. Analyze the provided cropped image and focus on the object.
193
+ This particular transformer is characterized by its rectangular shape and metallic contacts.
194
+ Determine if this transformer is present in the image.
195
+ Return 'False' if the transformer object is missing; return 'True' if it is present.
196
+ Format the response exactly as specified:
197
+ { "Detection": True/False, "Measurements": None} """
198
+
199
+ elif component_label in ["Transistor"]:
200
+ prompt = """You are a Transistor Object Detector. Analyze the provided cropped image and focus on the object.
201
+ The transistor has a blue cylindrical shape with metal contacts on the ends.
202
+ Determine if this blue cylindrical transistor is present in the image.
203
+ Return 'False' if the transistor object is missing; return 'True' if it is present.
204
+ Format the response exactly as specified:
205
+ { "Detection": True/False, "Measurements": None} """
206
 
207
  elif component_label in ["Fuse_black"]:
208
+ prompt = """You are a Black Fuse Object Detector. Analyze the provided cropped image and focus on the object.
209
+ A black fuse is typically a black cuboid box-shaped object.
210
+ Determine if the black fuse with the specified color and box-like shape is present in the image.
211
+ Return 'False' if the black fuse object is missing; return 'True' if it is present.
212
+ Format the response exactly as specified:
213
+ { "Detection": True/False, "Measurements": None} """
214
+ else:
215
+ prompt = "Not a valid combination"
216
  return prompt
217
 
218
+ def gradio_interface(image, prompt):
219
+ question = prompt
220
  if image.mode != "RGB":
221
  image = image.convert("RGB")
222
 
223
  answer = predict(image, question)
224
+ answer = post_processing(answer, question)
225
  return answer
226
 
227
+ def post_processing(decoded_output, question):
228
+ if question in decoded_output.keys():
229
+ decoded_output = decoded_output[question]
230
+ return decoded_output
231
+
232
  Method =["detection", "angle", "leg_lift", "polarity"]
233
  Component_list = ["choke_full_rd_gd", "choke_full_gn_ye", "Y_capacitor","varistor_red","varistor_grey", "DC_cap_blue", "DC_cap_black", "Inductor", "X2_capacitor", "Fuse_red", "transformer", "Transistor", "Fuse_black"]
234
 
235
+ with gr.Blocks() as demo:
236
+ gr.Markdown("Florence-enphase Leg Lift, Angle shift, Polarity and detection classifier")
237
+ image = gr.Image(type="pil", label="Upload Image", width=700, height=700) # Ensures image is passed as a PIL object
238
+ method_choice = gr.Dropdown(choices=Method, label="Method")
239
+ componet_choice = gr.Dropdown(choices = Component_list, label="Component label")
240
+ prompt = gr.Textbox(value=get_prompt(Method[0], Component_list[0]), label="Prompt")
241
+ submit_btn = gr.Button("Submit")
242
+
243
+ # @gr.render(inputs=[method_choice, componet_choice])
244
+ def generate_prompt(method, component_label):
245
+ prompts = get_prompt(method, component_label)
246
+ return prompts
247
+ method_choice.change(fn=generate_prompt, inputs=[method_choice, componet_choice], outputs=[prompt])
248
+ componet_choice.change(fn=generate_prompt, inputs=[method_choice, componet_choice], outputs=[prompt])
249
+
250
+
251
+ submit_btn.click(fn=gradio_interface, inputs=[image, prompt], outputs=gr.Textbox(label="Answer"))
252
+
253
+ demo.launch(debug=True)
254
+
255
+ # iface = gr.Interface(
256
+ # fn=gradio_interface,
257
+ # inputs=[
258
+ # gr.Image(type="pil", label="Upload Image"), # Ensures image is passed as a PIL object
259
+ # gr.Dropdown(choices=Method, label="Method"),
260
+ # gr.Dropdown(choices = Component_list, label="Component label")
261
+ # # gr.Textbox(label="Enter your question or edit the default prompt", lines=6, value=DEFAULT_PROMPT) # Default prompt pre-filled and editable
262
+ # ],
263
+ # outputs=gr.Textbox(label="Answer"),
264
+ # title="Florence-enphase Leg Lift Classifier",
265
+ # description=("Upload an image and ask a question about the leg lift. The model will classify whether "
266
+ # "the leg is inserted in the hole or not based on the image. You can edit the default prompt if needed.")
267
+ # )
268
+
269
+ # iface.launch(debug=True)
270
+ # img_path = '/content/choke_gn_1054.jpg'
271
+ # img = Image.open(img_path).convert('RGB')
272
+ # gradio_interface(img, "polarity", "choke_full_rd_gd")