arjunanand13 commited on
Commit
5d6090e
·
verified ·
1 Parent(s): c64eacb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +215 -0
app.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import AutoModelForCausalLM, AutoProcessor
5
+ import torch
6
+ import gradio as gr
7
+
8
+ # model_name = "arjunanand13/Florence-enphase2"
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()
17
+
18
+ DEFAULT_PROMPT = ("You are a Leg Lift Classifier. There is an image of a throughput component "
19
+ "and we need to identify if the leg is inserted in the hole or not. Return 'True' "
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":
51
+ if component_label in ["choke_full_rd_gd"]:
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)